Bug 225948

Summary: Speed up debug validation in CacheableIdentifier::createFromIdentifierOwnedByCodeBlock().
Product: WebKit Reporter: Mark Lam <mark.lam>
Component: JavaScriptCoreAssignee: Mark Lam <mark.lam>
Status: RESOLVED FIXED    
Severity: Normal CC: ews-watchlist, keith_miller, msaboff, saam, tzagallo, webkit-bug-importer, ysuzuki
Priority: P2 Keywords: InRadar
Version: WebKit Nightly Build   
Hardware: Unspecified   
OS: Unspecified   
Attachments:
Description Flags
proposed patch.
none
proposed patch.
ysuzuki: review+
proposed patch. none

Description Mark Lam 2021-05-18 17:50:57 PDT
rdar://78175542
Comment 1 Mark Lam 2021-05-18 18:07:38 PDT
Created attachment 429011 [details]
proposed patch.
Comment 2 Yusuke Suzuki 2021-05-18 18:11:57 PDT
Comment on attachment 429011 [details]
proposed patch.

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

> Source/JavaScriptCore/bytecode/CodeBlock.cpp:2936
> +        createRareDataIfNecessary();
> +        auto& cachedIdentifierUids = m_rareData->m_cachedIdentifierUids;
> +        if (UNLIKELY(cachedIdentifierUids.size() != numberOfIdentifiers)) {
> +            cachedIdentifierUids.reserveInitialCapacity(numberOfIdentifiers);
> +            for (unsigned index = 0; index < unlinkedIdentifiers; ++index) {
> +                const Identifier& identifier = unlinkedCode->identifier(index);
> +                cachedIdentifierUids.add(identifier.impl());
> +            }
> +            if (numberOfDFGIdentifiers) {
> +                ASSERT(JITCode::isOptimizingJIT(jitType()));
> +                auto& dfgIdentifiers = m_jitCode->dfgCommon()->m_dfgIdentifiers;
> +                for (unsigned index = 0; index < numberOfDFGIdentifiers; ++index) {
> +                    const Identifier& identifier = dfgIdentifiers[index];
> +                    cachedIdentifierUids.add(identifier.impl());
> +                }
> +            }
> +        }
> +        return cachedIdentifierUids.contains(uid);

This function can be called from DFG compilation thread. So we need to have a lock.
Comment 3 Mark Lam 2021-05-18 18:51:49 PDT
Created attachment 429018 [details]
proposed patch.
Comment 4 Yusuke Suzuki 2021-05-18 19:08:19 PDT
Comment on attachment 429018 [details]
proposed patch.

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

> Source/JavaScriptCore/bytecode/CodeBlock.cpp:2919
> +        if (!m_rareData || m_rareData->m_cachedIdentifierUids.size() != numberOfIdentifiers) {

m_cachedIdentifierUids.size() is dangerous without taking a lock. If it is in the middle of growing, then it can be SEGV since HashSet stores its size into the backing memory.
The other part looks good to me.
Comment 5 Mark Lam 2021-05-18 19:26:02 PDT
(In reply to Yusuke Suzuki from comment #4)
> Comment on attachment 429018 [details]
> proposed patch.
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=429018&action=review
> 
> > Source/JavaScriptCore/bytecode/CodeBlock.cpp:2919
> > +        if (!m_rareData || m_rareData->m_cachedIdentifierUids.size() != numberOfIdentifiers) {
> 
> m_cachedIdentifierUids.size() is dangerous without taking a lock. If it is
> in the middle of growing, then it can be SEGV since HashSet stores its size
> into the backing memory.
> The other part looks good to me.

I don't think the number of identifiers can grow by the time CacheableIdentifier::createFromIdentifierOwnedByCodeBlock() is called.  The original code was iterating over existing identifiers and asserting that the specified identifier is found among the CodeBlock ones.
Comment 6 Mark Lam 2021-05-18 19:34:43 PDT
Created attachment 429025 [details]
proposed patch.
Comment 7 Mark Lam 2021-05-18 19:37:07 PDT
Comment on attachment 429025 [details]
proposed patch.

Taking out of review first.  Need to check one more thing.
Comment 8 Mark Lam 2021-05-18 19:42:44 PDT
Comment on attachment 429025 [details]
proposed patch.

OK, all is well.  Just wanted to verify that m_jitCode->dfgCommon()->m_dfgIdentifiers is only set once, and not appended to.  It is indeed only set once in DesiredIdentifiers::reallyAdd().  Hence, the number of identifiers are fixed and will not grow.
Comment 9 Mark Lam 2021-05-18 19:46:13 PDT
Comment on attachment 429025 [details]
proposed patch.

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

> Source/JavaScriptCore/bytecode/CodeBlock.cpp:2919
> +        if (m_cachedIdentifierUids.size() != numberOfIdentifiers) {

There is no issue with growing this because the table is fixed size.  Since we create it on the side and WTFMove into m_cachedIdentifierUids only after it is fully initialized, m_cachedIdentifierUids.size() will only ever see 0 or the real size.
Comment 10 Mark Lam 2021-05-19 09:31:07 PDT
Comment on attachment 429025 [details]
proposed patch.

Thanks for the review.  Landing now.
Comment 11 EWS 2021-05-19 09:49:37 PDT
Committed r277727 (237904@main): <https://commits.webkit.org/237904@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 429025 [details].
Comment 12 Mark Lam 2021-05-19 15:41:01 PDT
Also landed CLoop build fix in r277759: <http://trac.webkit.org/r277759>.