Bug 155470 - [GTK] Remove duplicate HashMap traversal and unneeded reference count churn in DataObjectGtk::forClipboard
Summary: [GTK] Remove duplicate HashMap traversal and unneeded reference count churn i...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: WebKitGTK (show other bugs)
Version: WebKit Local Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Joonghun Park
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2016-03-14 15:11 PDT by Joonghun Park
Modified: 2016-03-15 07:21 PDT (History)
2 users (show)

See Also:


Attachments
Patch (1.87 KB, patch)
2016-03-15 05:25 PDT, Joonghun Park
no flags Details | Formatted Diff | Diff
Patch (1.87 KB, patch)
2016-03-15 05:39 PDT, Joonghun Park
no flags Details | Formatted Diff | Diff
Patch (1.77 KB, patch)
2016-03-15 06:28 PDT, Joonghun Park
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joonghun Park 2016-03-14 15:11:49 PDT
Remove duplicate HashMap traversal and unneeded reference count churn in DataObjectGtk::forClipboard.
Comment 1 Joonghun Park 2016-03-15 05:25:08 PDT
Created attachment 274088 [details]
Patch
Comment 2 Joonghun Park 2016-03-15 05:39:19 PDT
Created attachment 274090 [details]
Patch
Comment 3 Carlos Garcia Campos 2016-03-15 05:56:51 PDT
Comment on attachment 274090 [details]
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=274090&action=review

> Source/WebCore/platform/gtk/DataObjectGtk.cpp:163
> +    auto it = objectMap.find(clipboard);
>  
> -    if (!objectMap.contains(clipboard)) {
> -        Ref<DataObjectGtk> dataObject = DataObjectGtk::create();
> -        objectMap.set(clipboard, dataObject.copyRef());
> -        return dataObject.ptr();
> +    if (it == objectMap.end()) {
> +        auto dataObject = DataObjectGtk::create();
> +        DataObjectGtk* dataObjectPtr = dataObject.ptr();
> +        objectMap.set(clipboard, WTFMove(dataObject));
> +        return dataObjectPtr;
>      }
>  
> -    HashMap<GtkClipboard*, RefPtr<DataObjectGtk> >::iterator it = objectMap.find(clipboard);
>      return it->value.get();
>  }

Or even better you could use HashMap::add and check the result. Something like this:

auto addResult = objectMap.add(clipboard, nullptr);
if (addResult.isNewEntry)
    addResult.iterator->value = DataObjectGtk::create();
return addResult.iterator->value.get();
Comment 4 Joonghun Park 2016-03-15 06:16:22 PDT
(In reply to comment #3)
> Comment on attachment 274090 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=274090&action=review
> 
> > Source/WebCore/platform/gtk/DataObjectGtk.cpp:163
> > +    auto it = objectMap.find(clipboard);
> >  
> > -    if (!objectMap.contains(clipboard)) {
> > -        Ref<DataObjectGtk> dataObject = DataObjectGtk::create();
> > -        objectMap.set(clipboard, dataObject.copyRef());
> > -        return dataObject.ptr();
> > +    if (it == objectMap.end()) {
> > +        auto dataObject = DataObjectGtk::create();
> > +        DataObjectGtk* dataObjectPtr = dataObject.ptr();
> > +        objectMap.set(clipboard, WTFMove(dataObject));
> > +        return dataObjectPtr;
> >      }
> >  
> > -    HashMap<GtkClipboard*, RefPtr<DataObjectGtk> >::iterator it = objectMap.find(clipboard);
> >      return it->value.get();
> >  }
> 
> Or even better you could use HashMap::add and check the result. Something
> like this:
> 
> auto addResult = objectMap.add(clipboard, nullptr);
> if (addResult.isNewEntry)
>     addResult.iterator->value = DataObjectGtk::create();
> return addResult.iterator->value.get();

Indeed it is. Thank you for your comment. I will change this as you wrote.
Comment 5 Joonghun Park 2016-03-15 06:28:22 PDT
Created attachment 274091 [details]
Patch
Comment 6 WebKit Commit Bot 2016-03-15 07:21:36 PDT
Comment on attachment 274091 [details]
Patch

Clearing flags on attachment: 274091

Committed r198209: <http://trac.webkit.org/changeset/198209>
Comment 7 WebKit Commit Bot 2016-03-15 07:21:41 PDT
All reviewed patches have been landed.  Closing bug.