Skip to content

feat(ecmascript): fold indexOf, lastIndexOf, and charCodeAt on strings with lone surrogates - #26758

Merged
Dunqing merged 1 commit into
jsstr-estree-escape-tablefrom
jsstr-surrogate-numeric-folds
Sep 27, 2026
Merged

Dunqing merged 1 commit into
jsstr-estree-escape-tablefrom
jsstr-surrogate-numeric-folds

Conversation

@Dunqing

@Dunqing Dunqing commented Sep 17, 2026 •

Copy link
Copy Markdown
Member

Continued in #27097. GitHub marked this PR indirectly merged during stack reordering; the change has not landed in main.

The indexOf, lastIndexOf, and charCodeAt folds read the receiver through JSStr::as_str, so any string literal containing a lone surrogate declined to fold. These methods work in UTF-16 code units, where a lone surrogate is an ordinary unit: "a\uD800b".indexOf("b") is 2 and "a\uD800b".charCodeAt(1) is 55296, but both survived minification as calls. Both esbuild and terser fold the charCodeAt case.

Implement the existing StringIndexOf, StringLastIndexOf, and StringCharCodeAt traits for JSStr and drop the receiver gate at those three fold sites. A value without lone surrogates delegates to the &str implementation unchanged. Otherwise the search compares UTF-16 code units through the encode_utf16 iterator, which allocates nothing and needs no boundary rounding; this path only runs on the rare literals that contain a lone surrogate. The search value still arrives as UTF-8 through ConstantValue, so a search value containing a lone surrogate keeps declining; supporting those is the follow-up that moves ConstantValue itself to JSStr.

Verified against node on a generated corpus of 4,382 calls over lone-surrogate, astral, and mixed receivers: minified and original programs produce identical output, minify-twice is byte-identical, and minifier conformance and size snapshots are unchanged. On that corpus charCodeAt now folds completely and indexOf/lastIndexOf fold every case whose search value is representable.

The next PR restructures these implementations when the search value itself becomes JSStr; taking the receiver side first keeps each behavior change reviewable on its own.

Tenth PR in the JSStr stack, based on #26752. Part of #26242.

Implemented with AI assistance.

Dunqing commented Sep 17, 2026 •

Copy link
Copy Markdown
Member Author

How to use the Graphite Merge Queue

Add either label to this PR to merge it via the merge queue:

  • 0-merge - adds this PR to the back of the merge queue
  • hotfix - for urgent changes, fast-track this PR to the front of the merge queue

You must have a Graphite account in order to use the merge queue. Sign up using this link.

An organization admin has enabled the Graphite Merge Queue in this repository.

Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue.

This stack of pull requests is managed by Graphite. Learn more about stacking.

@codspeed

codspeed Bot commented Sep 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 87 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing jsstr-surrogate-numeric-folds (cada6b1) with jsstr-utf16-string-folds (f6d2e81)

Open in CodSpeed

Footnotes

  1. 9 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 1e90e0d to d29d07b Compare September 18, 2026 02:20
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 41783f8 to fcd7b6a Compare September 18, 2026 15:15
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from d29d07b to ca3e24e Compare September 18, 2026 15:15
@github-actions github-actions Bot added the A-ast Area - AST definitions and visitors, oxc_ast / oxc_ast_visit label Sep 18, 2026
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from d47b34a to aa3edfe Compare September 21, 2026 01:14
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from ee9b3b7 to 3df1998 Compare September 21, 2026 01:14
@github-actions github-actions Bot added the A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details label Sep 21, 2026
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from caada83 to 871acbe Compare September 21, 2026 01:48
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 9b3e65b to 9dbc8c1 Compare September 21, 2026 01:48
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 871acbe to bbae803 Compare September 21, 2026 01:54
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from ba93a56 to d1f0d04 Compare September 21, 2026 02:54
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from d1f0d04 to e97456f Compare September 21, 2026 07:04
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 5aeddda to c7e9085 Compare September 21, 2026 07:04
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 699e62c to d00abc0 Compare September 21, 2026 07:58
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from a4e8230 to cdb3abb Compare September 21, 2026 08:23
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 89d5b6b to afcd5cc Compare September 22, 2026 02:41
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from cdb3abb to 4013fd9 Compare September 22, 2026 02:41
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from afcd5cc to 3ff125e Compare September 22, 2026 04:12
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 761e789 to 5c24f47 Compare September 22, 2026 08:20
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 3ff125e to 34cb13e Compare September 22, 2026 08:20
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 5c24f47 to a92bf91 Compare September 23, 2026 01:55
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 569c770 to f4a3d1f Compare September 23, 2026 02:19
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 805fbe7 to fde800f Compare September 23, 2026 02:24
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from f4a3d1f to fcfc5de Compare September 23, 2026 02:24
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from fde800f to 5c35c20 Compare September 23, 2026 02:55
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 0e99bb0 to b9bffc3 Compare September 23, 2026 03:14
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 68650e3 to f6d2e81 Compare September 23, 2026 07:22
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from b9bffc3 to cada6b1 Compare September 23, 2026 07:22
…s with lone surrogates

A string literal containing a lone surrogate has no UTF-8
representation, so every string method fold declined it and calls like
"a\uD800b".indexOf("b") or "a\uD800b".charCodeAt(1) survived
minification. The numeric folds do not need a UTF-8 receiver: indexOf,
lastIndexOf, and charCodeAt return positions and code units, which are
defined for any sequence of UTF-16 code units.

Implement StringIndexOf, StringLastIndexOf, and StringCharCodeAt for
JSStr. A value without lone surrogates delegates to the existing UTF-8
implementations. Otherwise the search compares UTF-16 code units
directly, the domain the specification defines, so a lone surrogate
counts as one unit and no boundary rounding is needed. JSStr keeps its
WTF-8 bytes private, which rules out the byte search the UTF-8 path
uses; the unit walk iterates the encoded units without allocating and
only runs on the rare lone-surrogate literals. A search value with a
lone surrogate still declines, since constant string values remain
UTF-8. charAt also still declines: its result is a string, which may
have no UTF-8 form.

Verified against node on a generated corpus of 4382 string method
calls over lone and paired surrogate values, with identical program
output, byte-identical double minification, and unchanged minifier
conformance snapshots.

Part of #26242.

Implemented with AI assistance.
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from f6d2e81 to 8d1a725 Compare September 27, 2026 06:37
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from cada6b1 to 5980384 Compare September 27, 2026 06:37
Base automatically changed from jsstr-utf16-string-folds to jsstr-estree-escape-table September 27, 2026 06:37
@Dunqing
Dunqing merged commit 5980384 into jsstr-estree-escape-table Sep 27, 2026
@Dunqing
Dunqing deleted the jsstr-surrogate-numeric-folds branch September 27, 2026 06:37
@Dunqing
Dunqing restored the jsstr-surrogate-numeric-folds branch September 27, 2026 06:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-ast Area - AST definitions and visitors, oxc_ast / oxc_ast_visit A-ecmascript Area - ECMAScript spec operations, oxc_ecmascript A-linter Area - Linter (oxlint): rules, config, diagnostics, fixes. Add L-* labels for details A-minifier Area - Minification, compression and name mangling, oxc_minifier / oxc_mangler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant