Bug 235996 - Speed-up JSON.stringify() by avoiding "toJSON" property lookups
Summary: Speed-up JSON.stringify() by avoiding "toJSON" property lookups
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: JavaScriptCore (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Enhancement
Assignee: Alexey Shvayka
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2022-02-01 18:42 PST by Alexey Shvayka
Modified: 2022-02-02 17:06 PST (History)
7 users (show)

See Also:


Attachments
Patch (6.23 KB, patch)
2022-02-01 18:45 PST, Alexey Shvayka
no flags Details | Formatted Diff | Diff
Patch (7.95 KB, patch)
2022-02-02 08:52 PST, Alexey Shvayka
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Alexey Shvayka 2022-02-01 18:42:07 PST
Speed-up JSON.stringify() by avoiding "toJSON" property lookups
Comment 1 Alexey Shvayka 2022-02-01 18:45:49 PST
Created attachment 450601 [details]
Patch
Comment 2 Alexey Shvayka 2022-02-01 18:46:11 PST
(In reply to Alexey Shvayka from comment #1)
> Created attachment 450601 [details]
> Patch

                                              r288790                   patch

json-stringify-empty-array               109.4195+-0.3423     ^     89.3711+-0.2926        ^ definitely 1.2243x faster
json-stringify-array-replacer             26.5174+-0.0323           26.5100+-0.0257
json-stringify-many-objects              243.2980+-2.4751     ^    228.2883+-0.8872        ^ definitely 1.0657x faster
json-stringify-many-objects-to-json      170.5229+-0.3290     ^    152.2288+-0.4103        ^ definitely 1.1202x faster
vanilla-todomvc-json-stringify           192.3345+-0.4409     ^    185.5248+-0.2852        ^ definitely 1.0367x faster
Comment 3 Alexey Shvayka 2022-02-01 18:47:48 PST
Comment on attachment 450601 [details]
Patch

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

> Source/JavaScriptCore/ChangeLog:9
> +        it accounts for 10-15% of running time. EmberJS* subtests rely on JSON.stringify() as

MacBookPro17,1:

VanillaJS-TodoMVC: 50ms
Vanilla-ES2015-TodoMVC: 65ms

JSON.stringify() takes 5-10ms, way slower on MiniBrowser than JSC shell for some reason.
Comment 4 Saam Barati 2022-02-01 18:53:23 PST
Comment on attachment 450601 [details]
Patch

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

> Source/JavaScriptCore/ChangeLog:20
> +        While we could come up with a solution that doesn't involve creating StructureRareData
> +        for all structures we stringify, like keeping a list of StructureIDs w/o "toJSON" method,
> +        which will be correct as long as `m_hasFastObjectProperties || m_isJSArray` is true for all
> +        seen objects, that would probably miss some edge case, won't persist between JSON.stringify()
> +        calls, and won't speed-up structures with "toJSON" methods like Dates.

Can you also include what your solution actually was, instead of potential alternate solutions? And how it works
Comment 5 Alexey Shvayka 2022-02-02 08:52:02 PST
Created attachment 450648 [details]
Patch

Expand ChangeLog on actual solution and handle CachedSpecialPropertyKey::ToJSON in more places.
Comment 6 Saam Barati 2022-02-02 09:09:28 PST
Comment on attachment 450648 [details]
Patch

r=me
Comment 7 Alexey Shvayka 2022-02-02 16:13:09 PST
Comment on attachment 450648 [details]
Patch

Thank you Saam!
Comment 8 EWS 2022-02-02 17:05:07 PST
Committed r289020 (246728@main): <https://commits.webkit.org/246728@main>

All reviewed patches have been landed. Closing bug and clearing flags on attachment 450648 [details].
Comment 9 Radar WebKit Bug Importer 2022-02-02 17:06:24 PST
<rdar://problem/88409665>