Skip to content

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

Closed
Dunqing wants to merge 8 commits into
jsstr-utf16-string-foldsfrom
jsstr-surrogate-numeric-folds
Closed

Dunqing wants to merge 8 commits into
jsstr-utf16-string-foldsfrom
jsstr-surrogate-numeric-folds

Conversation

@Dunqing

@Dunqing Dunqing commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Fold indexOf, lastIndexOf, and charCodeAt for literal receivers containing lone surrogates. Ordinary UTF-8 receivers keep their existing implementation. Search values containing lone surrogates remain for #27098, which changes string constants to JSStr.

Nonempty UTF-8 needles use bounded memmem searches over WTF-8 bytes, with bounds and results translated to UTF-16 code unit indices. This avoids repeated needle decoding without allocating. Empty searches retain their code unit clamping. Tests cover mixed byte widths, overlapping matches, bounds inside surrogate pairs, and lastIndexOf matches extending beyond the start bound.

On the repetitive 32,000-unit receiver and 16,001-unit nonmatching needle, optimized end-to-end minification decreased from about 880 ms to 2.8 ms. All 10,032 differential cases preserve the previous implementation's runtime results, including the existing UTF-8 behavior; the 5,520 surrogate-receiver cases match Node. Rust tests and lint pass, minify-twice is identical, and minifier conformance and size/allocation snapshots are unchanged.

Based on #27096. Part of #26242. Continues #26758, which GitHub marked indirectly merged during stack reordering.

Implemented with AI assistance.

@Dunqing Dunqing added 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 A-ast Area - AST definitions and visitors, oxc_ast / oxc_ast_visit A-ecmascript Area - ECMAScript spec operations, oxc_ecmascript labels Sep 27, 2026

Dunqing commented Sep 27, 2026 •

Copy link
Copy Markdown
Member Author

Warning

This pull request is not mergeable via GitHub because a downstack PR is open. Once all requirements are satisfied, merge this PR as a stack on Graphite.
Learn more


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 27, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 87 untouched benchmarks
⏩ 9 skipped benchmarks1


Comparing jsstr-surrogate-numeric-folds (0b71e51) with jsstr-utf16-string-folds (8326520)

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-surrogate-numeric-folds branch from 5980384 to e323ae4 Compare September 27, 2026 13:45
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 8d1a725 to b72091b Compare September 27, 2026 13:45
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from e323ae4 to ff48d91 Compare September 27, 2026 15:47
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from b72091b to 29749b4 Compare September 27, 2026 15:47
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from ff48d91 to ca1b278 Compare September 27, 2026 23:45
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 29749b4 to bd447e8 Compare September 27, 2026 23:45
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from ca1b278 to a78c670 Compare September 28, 2026 00:21
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 14a8889 to a595fb2 Compare September 28, 2026 15:42
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 80ff5db to 9bf4b35 Compare September 28, 2026 16:15
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 33634e7 to c0a3e5d Compare September 28, 2026 17:17
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 58f5cb9 to 4473584 Compare September 29, 2026 01:38
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from c0a3e5d to 27e8c7e Compare September 29, 2026 01:38
A decoded property name can be static without being valid Rust UTF-8.
Return JSStr from member-name helpers so comparisons retain the actual
UTF-16 value and consumers no longer mistake these names for dynamic
expressions.

Keep explicit UTF-8 conversions at fixed-name lookups and existing
UTF-8-only APIs. Cover AST helpers, self-assignment and console diagnostics,
React display names, and declaration enum references with regressions.
Object/class key APIs and module-record storage remain separate work.

Part of #26242.

Implemented with AI assistance.
Static property names containing lone surrogates were treated as dynamic
because the helpers required Rust UTF-8. This skipped duplicate-key,
accessor-pair, and class-overload checks.

Borrow AST names as JSStr and own numeric display text in CompactStr,
preserving property identity and the existing numeric spelling. Use one
natural comparison for ordinary and surrogate keys and retain numeric
render-prop exemptions. Escape lone surrogates in diagnostic names.

BREAKING CHANGE: PropertyKey::static_name, PropertyKey::name,
ClassElement::static_name, and semantic class element names now use
StaticPropertyName instead of Cow<str>.

Based on #26677. Part of #26242. Continues #26720.

Implemented with AI assistance.
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from ee09742 to e7fc537 Compare October 6, 2026 00:42
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 05b64b5 to a2be12f Compare October 6, 2026 00:42
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from e7fc537 to d96e2af Compare October 8, 2026 01:54
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from a2be12f to 1b27b18 Compare October 8, 2026 01:54
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from d96e2af to d39a063 Compare October 8, 2026 03:35
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch 2 times, most recently from 0536f83 to 708ef15 Compare October 8, 2026 03:50
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch 2 times, most recently from 595668a to d763931 Compare October 8, 2026 06:28
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 708ef15 to 9fb6e70 Compare October 8, 2026 06:28
A lone surrogate anywhere in a string value made UTF-8 conversions
skip searches for otherwise valid text. Add contains, starts_with,
ends_with, find, and rfind to JSStr through a sealed pattern trait.
Search canonical WTF-8 without allocating. ASCII character searches use
memchr, text find uses memmem, and leading predicates inspect one code
point. Preserve the existing reverse-search paths. Predicates skip lone
surrogates, and find offsets count WTF-8 bytes, as documented.

Use the helpers in seven linter rules and JSX attribute layout checks.
Render diagnostic values with JSStr::display and share the existing
ASCII case-insensitive prefix comparison.

Guard lowercase-title fixes against differences between source spelling
and decoded values. Replace a first letter only when it is literal ASCII,
and rewrite a whole title only when the decoded value equals the source.
Escaped or surrogate-containing titles still report when no safe fix is
available.

Cover search semantics against str and a UTF-16 model, and test escaped
letters, line continuations, both title options, and Jest/Vitest callers.

Part of #26242.

Implemented with AI assistance.
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from d763931 to cf9622d Compare October 8, 2026 07:30
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 9fb6e70 to 476b6b4 Compare October 8, 2026 07:30
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from cf9622d to e071622 Compare October 8, 2026 08:47
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 476b6b4 to 5695623 Compare October 8, 2026 08:47
Index string literals by UTF-16 code unit and construct the result with
JSStrBuilder. This folds either half of a supplementary character and
lone surrogates without allocating a temporary heap string. Retain the
native UTF-8 decoder for ordinary strings.

Clear the computed flag once before normalizing string property keys.
Existing guards for __proto__, constructor, and prototype remain in place.

Part of #26242.

Implemented with AI assistance.
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 5695623 to 3e34649 Compare October 8, 2026 09:23
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from e071622 to 8326520 Compare October 8, 2026 09:23
…s with lone surrogates

Support JSStr receivers for indexOf, lastIndexOf, and charCodeAt.
Use bounded byte searches for UTF-8 needles and translate match offsets
to UTF-16 indices, avoiding repeated needle decoding and allocations.
Keep the existing ordinary UTF-8 behavior unchanged.

Cover mixed-width offsets, overlapping matches, surrogate-pair bounds,
and matches extending beyond the lastIndexOf start bound.
@Dunqing
Dunqing force-pushed the jsstr-surrogate-numeric-folds branch from 3e34649 to 0b71e51 Compare October 10, 2026 03:13
@Dunqing
Dunqing force-pushed the jsstr-utf16-string-folds branch from 8326520 to 3bb462c Compare October 12, 2026 08:07

Dunqing commented Oct 12, 2026

Copy link
Copy Markdown
Member Author

Closing in favour of #27098. The stack rework squashes this PR into #27098: its memmem-based search for UTF-8 needles is kept there, and indexOf, lastIndexOf, charCodeAt, substring and slice now fold through one UTF-16 code-unit path for every receiver, lone surrogates included, so the separate surrogate-receiver path is no longer needed. #27098 is now based on #27096.


Generated by Claude Code

@Dunqing Dunqing closed this Oct 12, 2026
graphite-app Bot pushed a commit that referenced this pull request Oct 20, 2026
String constant evaluation used `Cow<str>`, so folds stopped at any string with a lone surrogate. `ConstantValue::String` now holds a `JSStr`, so these strings fold too, and results are built with `JSStrBuilder`, which joins surrogate halves the way the runtime does.

`indexOf`, `lastIndexOf`, `substring` and `slice` now share one UTF-16 code-unit implementation for every string. This also fixes miscompiles on main: `'éx'.indexOf('x')` folded to 2, `'abc'.indexOf('a', 4294967296)` to 0, `'abab'.lastIndexOf('b', NaN)` to -1, and `'abc'.substring(1, void 0)` to `''`. It includes #27097, which is closed.

String `concat` calls and chains fold into a template only when no argument with side effects follows one that may be an object, so `''.concat(a, (a.push(1), 'x'))` no longer becomes `` `${a}${(a.push(1), 'x')}` ``.

## Breaking changes

- `ConstantValue::String` holds `JSStr<'a>`, and `ConstantValue::into_string`, `evaluate_value_to_string` and `get_side_free_string_value` return `JSStr<'a>`.
- `StringIndexOf` and `StringLastIndexOf` take the search value as `JSStr` and use UTF-16 code unit positions.
- `StringSubstring` is removed.

Closes #15524. Closes #25158. Closes #26053. Based on #27096. Part of #26242. Continues #26759.

Implemented with AI assistance.
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