Skip to content

[JSC] Optimize DFG String#indexOf with dynamic one-character search string - #80416

Merged
webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSC-Search-for-the-character-directly-in-DFG-String-indexOf-when-the-search-string-has-one-character-at-run-time
Nov 16, 2026
Merged

webkit-commit-queue merged 1 commit into
WebKit:mainfrom
sosukesuzuki:eng/JSC-Search-for-the-character-directly-in-DFG-String-indexOf-when-the-search-string-has-one-character-at-run-time

Conversation

@sosukesuzuki

@sosukesuzuki sosukesuzuki commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

ff1c34e

[JSC] Optimize DFG `String#indexOf` with dynamic one-character search string
https://bugs.webkit.org/show_bug.cgi?id=331036

Reviewed by Yusuke Suzuki.

operationStringIndexOf and operationStringIndexOfWithIndex are used when the search string is not a
constant. They build an AdaptiveStringSearcher even when the search string has only one character,
unless the base string is a long rope, where they first try a one-character search.

If the search string has one character and both strings are resolved, this patch searches for that
character directly. This path cannot allocate, GC, or throw. Other searches pay for the added checks.

                                               Baseline                  Patched

string-index-of-dynamic-one-character      29.36ms                   17.99ms      1.63x faster
string-index-of-dynamic-two-characters     29.56ms                   29.91ms      1.01x slower

Tests: JSTests/microbenchmarks/string-index-of-dynamic-one-character.js
       JSTests/microbenchmarks/string-index-of-dynamic-two-characters.js
       JSTests/stress/string-index-of-dynamic-one-character.js

* JSTests/microbenchmarks/string-index-of-dynamic-one-character.js: Added.
(indexOf):
(indexOfFrom):
* JSTests/microbenchmarks/string-index-of-dynamic-two-characters.js: Added.
(indexOf):
(indexOfFrom):
* JSTests/stress/string-index-of-dynamic-one-character.js: Added.
(shouldBe):
(indexOfFrom):
(expectedIndexOf):
(flat):
(ropeSubstring):
(wide):
* Source/JavaScriptCore/dfg/DFGOperations.cpp:
(JSC::DFG::tryStringIndexOfOneCharWithoutGC):
(JSC::DFG::JSC_DEFINE_JIT_OPERATION):

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

63efb93

Misc iOS, visionOS, tvOS & watchOS macOS Linux Windows
✅ 🧪 style ✅ 🛠 ios ✅ 🛠 mac ✅ 🛠 wpe ✅ 🛠 win
✅ 🛠 ios-sim-cmake ✅ 🛠 mac-cmake-debug ✅ 🧪 wpe-wk2 ✅ 🧪 win-tests
✅ 🧪 webkitperl ✅ 🧪 ios-cmake-wk2 ✅ 🧪 api-mac ✅ 🧪 api-wpe
✅ 🧪 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
✅ 🛠 vision ✅ 🧪 gtk-wk2
✅ 🛠 🧪 merge ✅ 🛠 vision-sim   🧪 mac-intel-wk2 ✅ 🧪 api-gtk
✅ 🧪 vision-wk2 ✅ 🛠 mac-safer-cpp ✅ 🛠 playstation
✅ 🛠 tv ✅ 🧪 mac-site-isolation
✅ 🛠 tv-sim
✅ 🛠 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 force-pushed the eng/JSC-Search-for-the-character-directly-in-DFG-String-indexOf-when-the-search-string-has-one-character-at-run-time branch from e67ba1c to 510cd77 Compare November 11, 2026 01:20
@sosukesuzuki sosukesuzuki changed the title [JSC] Search for the character directly in DFG String#indexOf when the search string has one character at run time [JSC] Optimize DFG String#indexOf with dynamic one-character search string 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

VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this matter so much in perf? If not, it is preferable to be placed after this to follow to the basic structure of the operation.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!


VM& vm = globalObject->vm();
CallFrame* callFrame = DECLARE_CALL_FRAME(vm);
JITOperationPrologueCallFrameTracer tracer(vm, callFrame);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@sosukesuzuki sosukesuzuki added merge-queue Applied to send a pull request to merge-queue and removed merge-queue Applied to send a pull request to merge-queue labels Nov 14, 2026
@sosukesuzuki
sosukesuzuki force-pushed the eng/JSC-Search-for-the-character-directly-in-DFG-String-indexOf-when-the-search-string-has-one-character-at-run-time branch from 510cd77 to 63efb93 Compare November 15, 2026 03:40
@sosukesuzuki sosukesuzuki added the merge-queue Applied to send a pull request to merge-queue label Nov 16, 2026
… string

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

Reviewed by Yusuke Suzuki.

operationStringIndexOf and operationStringIndexOfWithIndex are used when the search string is not a
constant. They build an AdaptiveStringSearcher even when the search string has only one character,
unless the base string is a long rope, where they first try a one-character search.

If the search string has one character and both strings are resolved, this patch searches for that
character directly. This path cannot allocate, GC, or throw. Other searches pay for the added checks.

                                               Baseline                  Patched

string-index-of-dynamic-one-character      29.36ms                   17.99ms      1.63x faster
string-index-of-dynamic-two-characters     29.56ms                   29.91ms      1.01x slower

Tests: JSTests/microbenchmarks/string-index-of-dynamic-one-character.js
       JSTests/microbenchmarks/string-index-of-dynamic-two-characters.js
       JSTests/stress/string-index-of-dynamic-one-character.js

* JSTests/microbenchmarks/string-index-of-dynamic-one-character.js: Added.
(indexOf):
(indexOfFrom):
* JSTests/microbenchmarks/string-index-of-dynamic-two-characters.js: Added.
(indexOf):
(indexOfFrom):
* JSTests/stress/string-index-of-dynamic-one-character.js: Added.
(shouldBe):
(indexOfFrom):
(expectedIndexOf):
(flat):
(ropeSubstring):
(wide):
* Source/JavaScriptCore/dfg/DFGOperations.cpp:
(JSC::DFG::tryStringIndexOfOneCharWithoutGC):
(JSC::DFG::JSC_DEFINE_JIT_OPERATION):

Canonical link: https://commits.webkit.org/326839@main
@webkit-commit-queue
webkit-commit-queue force-pushed the eng/JSC-Search-for-the-character-directly-in-DFG-String-indexOf-when-the-search-string-has-one-character-at-run-time branch from 63efb93 to ff1c34e Compare November 16, 2026 01:09
@webkit-commit-queue

Copy link
Copy Markdown
Collaborator

Committed 326839@main (ff1c34e): https://commits.webkit.org/326839@main

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

@webkit-commit-queue
webkit-commit-queue merged commit ff1c34e into WebKit:main Nov 16, 2026
@webkit-commit-queue webkit-commit-queue removed the merge-queue Applied to send a pull request to merge-queue label Nov 16, 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.

4 participants