Bug 236124 - cache the result of JSString::toIdentifier
Summary: cache the result of JSString::toIdentifier
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Cameron McCormack (:heycam)
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-02-03 21:05 PST by Cameron McCormack (:heycam)
Modified: 2022-02-06 12:39 PST (History)
8 users (show)

See Also:


Attachments
patch (5.81 KB, patch)
2022-02-03 21:09 PST, Cameron McCormack (:heycam)
no flags Details | Formatted Diff | Diff
Patch (4.42 KB, patch)
2022-02-05 20:22 PST, Cameron McCormack (:heycam)
ews-feeder: commit-queue-
Details | Formatted Diff | Diff
Patch (4.40 KB, patch)
2022-02-05 20:51 PST, Cameron McCormack (:heycam)
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Cameron McCormack (:heycam) 2022-02-03 21:05:03 PST
JSString::toIdentifier does not store the result of atomizing its string value, except when it is a rope.  We can often end up atomizing the same JSString a number of times.

Forthcoming patch caches the last atomized string produced from JSString::toIdentifier in a given VM.  From local testing, this is a 0.5% Speedometer2 improvement on an M1 MacBook Air, although surprisingly is neutral on a recent Intel MacBook Pro.
Comment 1 Cameron McCormack (:heycam) 2022-02-03 21:09:37 PST
Created attachment 450862 [details]
patch
Comment 2 Radar WebKit Bug Importer 2022-02-03 21:10:06 PST
<rdar://problem/88472619>
Comment 3 Yusuke Suzuki 2022-02-03 21:37:42 PST
Comment on attachment 450862 [details]
patch

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

Nice, I have some questions.

> Source/JavaScriptCore/runtime/JSString.h:785
> +    if (vm.lastAtomizedString.ptr() != valueInternal().impl()) {
> +        vm.lastAtomizedString = *valueInternal().impl();
> +        vm.lastAtomizedStringAtom = AtomStringImpl::add(valueInternal().impl()).releaseNonNull();
> +    }

Is there any reasons why we do not perform it in toAtomString?
If we should do this cache only in `toIdentifier` call and we should do that only when converting existing non-atom-string to atom-string, then can we change  lastAtomizedString / lastAtomizedStringAtom names to more specific ones?
Comment 4 Cameron McCormack (:heycam) 2022-02-03 21:39:00 PST
(In reply to Yusuke Suzuki from comment #3)
> Is there any reasons why we do not perform it in toAtomString?

No, I think I can move it down to toAtomString.
Comment 5 Saam Barati 2022-02-03 21:59:21 PST
Comment on attachment 450862 [details]
patch

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

> Source/JavaScriptCore/ChangeLog:8
> +        JSString::toIdentifier does not store the result of atomizing its string

I wonder if this is worth doing.
Comment 6 Yusuke Suzuki 2022-02-03 22:10:39 PST
Comment on attachment 450862 [details]
patch

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

>> Source/JavaScriptCore/ChangeLog:8
>> +        JSString::toIdentifier does not store the result of atomizing its string
> 
> I wonder if this is worth doing.

This is because string is concurrently accessed from GC thread and concurrent compilers.
If we replace it without stopping them, then these threads will touch the old String => UAF.
If we would like to do that, we need some protocol to allow this.
Comment 7 Cameron McCormack (:heycam) 2022-02-05 19:42:58 PST
(In reply to Cameron McCormack (:heycam) from comment #4)
> No, I think I can move it down to toAtomString.

I forgot that's actually what I tried first, but didn't get good results.  I will rename the variables.
Comment 8 Yusuke Suzuki 2022-02-05 20:13:40 PST
(In reply to Cameron McCormack (:heycam) from comment #7)
> (In reply to Cameron McCormack (:heycam) from comment #4)
> > No, I think I can move it down to toAtomString.
> 
> I forgot that's actually what I tried first, but didn't get good results.  I
> will rename the variables.

Sounds good to me!!
Comment 9 Cameron McCormack (:heycam) 2022-02-05 20:22:16 PST
Created attachment 451022 [details]
Patch
Comment 10 Cameron McCormack (:heycam) 2022-02-05 20:51:28 PST
Created attachment 451023 [details]
Patch
Comment 11 EWS 2022-02-06 12:39:10 PST
Committed r289177 (246871@main): <https://commits.webkit.org/246871@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 451023 [details].