Skip to content

[JSC] Optimize String.fromCharCode with multiple arguments - #80412

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSC-Optimize-String-fromCharCode-with-multiple-arguments
Nov 25, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSC-Optimize-String-fromCharCode-with-multiple-arguments

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

cf5b4b6

[JSC] Optimize `String.fromCharCode` with multiple arguments
https://bugs.webkit.org/show_bug.cgi?id=331034

Reviewed by Yusuke Suzuki.

String.fromCharCode with two or more arguments always goes to the host
function. It optimistically allocated an 8-bit buffer, and when it found
a non-Latin1 character, threw it away and allocated a 16-bit one. It also
converted each argument with an out-of-line toUInt32 call followed by an
exception check.

Converting Int32 values has no side effects. So this patch first scans
the arguments: the AND of their encodings tells whether all of them are
Int32, and the OR tells whether all of the characters are Latin1. If all
of them are Int32, we allocate a buffer of the right width once, and fill
it by narrowing the encoded values with WTF::copyElements. Otherwise, we
convert them into a temporary buffer, and create the string from it.

                                          Baseline                  Patched

string-from-char-code-two-latin1      27.8257+-0.2125     ^     21.8677+-0.2164        ^ definitely 1.2725x faster
string-from-char-code-surrogate-pair  42.8458+-0.8424     ^     23.2908+-0.2916        ^ definitely 1.8396x faster
string-from-char-code-apply           32.1576+-0.2703     ^     10.5591+-0.2206        ^ definitely 3.0455x faster
string-from-char-code-apply-double    32.1350+-0.1259     ^     19.7438+-0.1864        ^ definitely 1.6276x faster

Tests: JSTests/microbenchmarks/string-from-char-code-apply-double.js
       JSTests/microbenchmarks/string-from-char-code-apply.js
       JSTests/microbenchmarks/string-from-char-code-surrogate-pair.js
       JSTests/microbenchmarks/string-from-char-code-two-latin1.js
       JSTests/stress/string-from-char-code-multiple-arguments.js

* JSTests/microbenchmarks/string-from-char-code-apply-double.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-apply.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-surrogate-pair.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-two-latin1.js: Added.
(test):
* JSTests/stress/string-from-char-code-multiple-arguments.js: Added.
(shouldBe):
(reference):
(check):
(thrownBy):
(two):
(three):
(1024.codes.1.valueOf):
* Source/JavaScriptCore/runtime/StringConstructor.cpp:
(JSC::classifyCharCodes):
(JSC::createStringImplFromInt32CharCodes):
(JSC::stringFromCharCodesSlow):
(JSC::JSC_DEFINE_HOST_FUNCTION):

Canonical link: https://commits.webkit.org/327767@main

7e19480

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

@sosukesuzuki sosukesuzuki self-assigned this Nov 11, 2026
@sosukesuzuki sosukesuzuki added the JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues. label Nov 11, 2026
@sosukesuzuki
sosukesuzuki marked this pull request as ready for review November 11, 2026 03:45
@sosukesuzuki
sosukesuzuki requested a review from a team as a code owner November 11, 2026 03:45
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

macOS Safer C++ Build #148131 (d258bd3)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@webkit-ews-buildbot webkit-ews-buildbot added the merging-blocked Applied to prevent a change from being merged label Nov 11, 2026
@webkit-ews-buildbot

Copy link
Copy Markdown
Collaborator

iOS Safer C++ Build #67869 (d258bd3)

❌ Found 1 failing file with 1 issue. Please address these issues before landing. See WebKit Guidelines for Safer C++ Programming.
(cc @rniwa)

@sosukesuzuki
sosukesuzuki force-pushed the eng/JSC-Optimize-String-fromCharCode-with-multiple-arguments branch from d258bd3 to 7e19480 Compare November 11, 2026 05:14
@sosukesuzuki sosukesuzuki added merge-queue Applied to send a pull request to merge-queue and removed merging-blocked Applied to prevent a change from being merged labels Nov 25, 2026
https://bugs.webkit.org/show_bug.cgi?id=331034

Reviewed by Yusuke Suzuki.

String.fromCharCode with two or more arguments always goes to the host
function. It optimistically allocated an 8-bit buffer, and when it found
a non-Latin1 character, threw it away and allocated a 16-bit one. It also
converted each argument with an out-of-line toUInt32 call followed by an
exception check.

Converting Int32 values has no side effects. So this patch first scans
the arguments: the AND of their encodings tells whether all of them are
Int32, and the OR tells whether all of the characters are Latin1. If all
of them are Int32, we allocate a buffer of the right width once, and fill
it by narrowing the encoded values with WTF::copyElements. Otherwise, we
convert them into a temporary buffer, and create the string from it.

                                          Baseline                  Patched

string-from-char-code-two-latin1      27.8257+-0.2125     ^     21.8677+-0.2164        ^ definitely 1.2725x faster
string-from-char-code-surrogate-pair  42.8458+-0.8424     ^     23.2908+-0.2916        ^ definitely 1.8396x faster
string-from-char-code-apply           32.1576+-0.2703     ^     10.5591+-0.2206        ^ definitely 3.0455x faster
string-from-char-code-apply-double    32.1350+-0.1259     ^     19.7438+-0.1864        ^ definitely 1.6276x faster

Tests: JSTests/microbenchmarks/string-from-char-code-apply-double.js
       JSTests/microbenchmarks/string-from-char-code-apply.js
       JSTests/microbenchmarks/string-from-char-code-surrogate-pair.js
       JSTests/microbenchmarks/string-from-char-code-two-latin1.js
       JSTests/stress/string-from-char-code-multiple-arguments.js

* JSTests/microbenchmarks/string-from-char-code-apply-double.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-apply.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-surrogate-pair.js: Added.
(test):
* JSTests/microbenchmarks/string-from-char-code-two-latin1.js: Added.
(test):
* JSTests/stress/string-from-char-code-multiple-arguments.js: Added.
(shouldBe):
(reference):
(check):
(thrownBy):
(two):
(three):
(1024.codes.1.valueOf):
* Source/JavaScriptCore/runtime/StringConstructor.cpp:
(JSC::classifyCharCodes):
(JSC::createStringImplFromInt32CharCodes):
(JSC::stringFromCharCodesSlow):
(JSC::JSC_DEFINE_HOST_FUNCTION):

Canonical link: https://commits.webkit.org/327767@main
@webkit-commit-queue
webkit-commit-queue force-pushed the eng/JSC-Optimize-String-fromCharCode-with-multiple-arguments branch from 7e19480 to cf5b4b6 Compare November 25, 2026 15:15
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 327767@main (cf5b4b6): https://commits.webkit.org/327767@main

Reviewed commits have been landed. Closing PR #80412 and removing active labels.

@webkit-commit-queue
webkit-commit-queue merged commit cf5b4b6 into WebKit:main Nov 25, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Nov 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

JavaScriptCore For bugs in JavaScriptCore, the JS engine used by WebKit, other than kxmlcore issues.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants