Bug 215830

Summary: HashMap<Ref<T>>::take should return RefPtr<T>
Product: WebKit Reporter: Ryosuke Niwa <rniwa>
Component: Web Template FrameworkAssignee: Ryosuke Niwa <rniwa>
Status: RESOLVED FIXED    
Severity: Normal CC: aboxhall, alecflett, apinheiro, beidson, benjamin, cdumez, cfleizach, cmarcelo, darin, dmazzoni, eric.carlson, esprehn+autocc, ews-watchlist, glenn, hi, hta, jcraig, jdiggs, jer.noble, jiewen_tan, joepeck, jsbell, kangil.han, mjs, philipj, samuel_white, sam, sergio, tommyw, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: All   
OS: All   
See Also: https://bugs.webkit.org/show_bug.cgi?id=215562
Attachments:
Description Flags
Patch
none
Patch for landing none

Description Ryosuke Niwa 2020-08-25 16:50:36 PDT
HashMap<Ref<T>>::take should return RefPtr<T> instead of Optional<Ref<T>>.
Comment 1 Ryosuke Niwa 2020-08-25 17:33:59 PDT
Created attachment 407253 [details]
Patch
Comment 2 Darin Adler 2020-08-25 18:05:30 PDT
Comment on attachment 407253 [details]
Patch

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

Looks like an improvement at almost every call site.

> Source/WTF/wtf/HashTraits.h:215
> +    typedef RefPtr<P> TakeType;

Since we’re touching it we could use using instead of typedef.

> Source/WebCore/dom/ScriptRunner.cpp:102
> -        ASSERT(m_pendingAsyncScripts.contains(pendingScript));
> -        m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(pendingScript)->ptr());
> +        auto script = m_pendingAsyncScripts.take(pendingScript);
> +        ASSERT(script);
> +        m_scriptsToExecuteSoon.append(script.releaseNonNull());

This is an example where the "after" looks worse because releaseNonNull is such a long function name. But I see that there was reference count churn in the old version!

No need to assert non-null separately before calling releaseNonNull, because that asserts. I would write this as a one-liner, I think.
Comment 3 Ryosuke Niwa 2020-08-25 18:40:54 PDT
(In reply to Darin Adler from comment #2)
> Comment on attachment 407253 [details]
> Patch
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=407253&action=review
> 
> Looks like an improvement at almost every call site.
> 
> > Source/WTF/wtf/HashTraits.h:215
> > +    typedef RefPtr<P> TakeType;
> 
> Since we’re touching it we could use using instead of typedef.

Fixed. I'm gonna also fix PeekType.

> > Source/WebCore/dom/ScriptRunner.cpp:102
> > -        ASSERT(m_pendingAsyncScripts.contains(pendingScript));
> > -        m_scriptsToExecuteSoon.append(m_pendingAsyncScripts.take(pendingScript)->ptr());
> > +        auto script = m_pendingAsyncScripts.take(pendingScript);
> > +        ASSERT(script);
> > +        m_scriptsToExecuteSoon.append(script.releaseNonNull());
> 
> This is an example where the "after" looks worse because releaseNonNull is
> such a long function name. But I see that there was reference count churn in
> the old version!
> 
> No need to assert non-null separately before calling releaseNonNull, because
> that asserts. I would write this as a one-liner, I think.

Sure, I'd use the one liner.
Comment 4 Ryosuke Niwa 2020-08-25 18:41:24 PDT
Created attachment 407256 [details]
Patch for landing
Comment 5 EWS 2020-08-25 19:25:10 PDT
Committed r266157: <https://trac.webkit.org/changeset/266157>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 407256 [details].
Comment 6 Radar WebKit Bug Importer 2020-08-25 19:26:17 PDT
<rdar://problem/67774006>