Skip to content

[JSC] JSObject::convertFromCopyOnWrite should allocate only what the caller needs - #79650

Open
sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSObject-convertFromCopyOnWrite-should-allocate-what-the-caller-needs
Open

sosukesuzuki wants to merge 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSObject-convertFromCopyOnWrite-should-allocate-what-the-caller-needs

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

2ab7318

[JSC] `JSObject::convertFromCopyOnWrite` should allocate only what the caller needs
https://bugs.webkit.org/show_bug.cgi?id=330160

Reviewed by NOBODY (OOPS!).

convertFromCopyOnWrite allocates a butterfly of nextLength(vectorLength), but it then
copies the IndexingHeader from the old butterfly, so the new vectorLength is the old one
and the extra capacity is never used. A store pays for memory it does not need, and a push
right after the conversion allocates a larger butterfly and copies the elements again.

This patch adds an additionalLength argument, the number of elements the caller is about
to add. With 0, the conversion allocates only what the existing elements need. Otherwise
it allocates a butterfly with room for them and records that in vectorLength.
JSArray::pushInline passes 1, and all the other callers pass 0.

DFG and FTL used to emit Arrayify before ArrayPush, which converted the array before
pushInline could see it. ArrayPush no longer does so for Int32, Double and Contiguous
arrays. Since publicLength equals vectorLength in a copy-on-write butterfly, the inline
path never stores to it and always takes the slow path call.

                                          Baseline                  Patched

array-copy-on-write-push              49.1493+-2.4547     ^     33.1678+-2.1527        ^ definitely 1.4818x faster
array-copy-on-write-first-store       90.8132+-1.9766     ^     72.1276+-1.1986        ^ definitely 1.2591x faster

Tests: JSTests/microbenchmarks/array-copy-on-write-first-store.js
       JSTests/microbenchmarks/array-copy-on-write-push.js
       JSTests/stress/array-push-copy-on-write.js

* JSTests/microbenchmarks/array-copy-on-write-first-store.js: Added.
(test):
* JSTests/microbenchmarks/array-copy-on-write-push.js: Added.
(test):
* JSTests/stress/array-push-copy-on-write.js: Added.
(shouldBe):
(pushInt32):
(pushDouble):
(pushContiguous):
(pushTwo):
(pushTwoDoubles):
(pushMixed):
(pushNothing):
(literalThenPush):
(i.shouldBeArray.Object.keys):
(i.shouldBe):
(i.shouldBeArray):
* Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleIntrinsicCall):
* Source/JavaScriptCore/dfg/DFGOperations.cpp:
(JSC::DFG::JSC_DEFINE_JIT_OPERATION):
* Source/JavaScriptCore/runtime/JSArray.cpp:
(JSC::JSArray::fastFill):
(JSC::JSArray::fastCopyWithin):
(JSC::JSArray::appendMemcpy):
(JSC::JSArray::setLength):
* Source/JavaScriptCore/runtime/JSArrayInlines.h:
(JSC::JSArray::pushInline):
* Source/JavaScriptCore/runtime/JSObject.cpp:
(JSC::JSObject::convertFromCopyOnWrite):
(JSC::JSObject::tryMakeWritableInt32Slow):
(JSC::JSObject::tryMakeWritableDoubleSlow):
(JSC::JSObject::tryMakeWritableContiguousSlow):
(JSC::JSObject::deletePropertyByIndex):
(JSC::JSObject::ensureLengthSlow):
* Source/JavaScriptCore/runtime/JSObject.h:
* Source/JavaScriptCore/runtime/JSObjectInlines.h:
(JSC::JSObject::ensureWritable):

2ab7318

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🛠 ios-sim ✅ 🛠 mac-cmake-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 ios-wk2-wpt ✅ 🧪 api-mac-cmake-debug ✅ 🧪 jsc-wpe
✅ 🧪 jsc-x86-64 ✅ 🧪 api-ios ✅ 🧪 mac-wk2 ✅ 🛠 gtk3-gcc
✅ 🛠 🧪 jsc-debug-arm64 ✅ 🛠 ios-safer-cpp ✅ 🧪 mac-cmake-debug-wk2 ✅ 🛠 gtk
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 vision-sim ✅ 🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 watch
✅ 🛠 watch-sim

…e caller needs

https://bugs.webkit.org/show_bug.cgi?id=330160

Reviewed by NOBODY (OOPS!).

convertFromCopyOnWrite allocates a butterfly of nextLength(vectorLength), but it then
copies the IndexingHeader from the old butterfly, so the new vectorLength is the old one
and the extra capacity is never used. A store pays for memory it does not need, and a push
right after the conversion allocates a larger butterfly and copies the elements again.

This patch adds an additionalLength argument, the number of elements the caller is about
to add. With 0, the conversion allocates only what the existing elements need. Otherwise
it allocates a butterfly with room for them and records that in vectorLength.
JSArray::pushInline passes 1, and all the other callers pass 0.

DFG and FTL used to emit Arrayify before ArrayPush, which converted the array before
pushInline could see it. ArrayPush no longer does so for Int32, Double and Contiguous
arrays. Since publicLength equals vectorLength in a copy-on-write butterfly, the inline
path never stores to it and always takes the slow path call.

                                          Baseline                  Patched

array-copy-on-write-push              49.1493+-2.4547     ^     33.1678+-2.1527        ^ definitely 1.4818x faster
array-copy-on-write-first-store       90.8132+-1.9766     ^     72.1276+-1.1986        ^ definitely 1.2591x faster

Tests: JSTests/microbenchmarks/array-copy-on-write-first-store.js
       JSTests/microbenchmarks/array-copy-on-write-push.js
       JSTests/stress/array-push-copy-on-write.js

* JSTests/microbenchmarks/array-copy-on-write-first-store.js: Added.
(test):
* JSTests/microbenchmarks/array-copy-on-write-push.js: Added.
(test):
* JSTests/stress/array-push-copy-on-write.js: Added.
(shouldBe):
(pushInt32):
(pushDouble):
(pushContiguous):
(pushTwo):
(pushTwoDoubles):
(pushMixed):
(pushNothing):
(literalThenPush):
(i.shouldBeArray.Object.keys):
(i.shouldBe):
(i.shouldBeArray):
* Source/JavaScriptCore/dfg/DFGByteCodeParser.cpp:
(JSC::DFG::ByteCodeParser::handleIntrinsicCall):
* Source/JavaScriptCore/dfg/DFGOperations.cpp:
(JSC::DFG::JSC_DEFINE_JIT_OPERATION):
* Source/JavaScriptCore/runtime/JSArray.cpp:
(JSC::JSArray::fastFill):
(JSC::JSArray::fastCopyWithin):
(JSC::JSArray::appendMemcpy):
(JSC::JSArray::setLength):
* Source/JavaScriptCore/runtime/JSArrayInlines.h:
(JSC::JSArray::pushInline):
* Source/JavaScriptCore/runtime/JSObject.cpp:
(JSC::JSObject::convertFromCopyOnWrite):
(JSC::JSObject::tryMakeWritableInt32Slow):
(JSC::JSObject::tryMakeWritableDoubleSlow):
(JSC::JSObject::tryMakeWritableContiguousSlow):
(JSC::JSObject::deletePropertyByIndex):
(JSC::JSObject::ensureLengthSlow):
* Source/JavaScriptCore/runtime/JSObject.h:
* Source/JavaScriptCore/runtime/JSObjectInlines.h:
(JSC::JSObject::ensureWritable):
@sosukesuzuki
sosukesuzuki requested a review from a team as a code owner November 4, 2026 20:49
@sosukesuzuki sosukesuzuki self-assigned this Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants