Parents: #473, #493 Related: #460, #765
Problem
ECMAScript string indices address UTF-16 code units, while zig-js stores canonical strings as WTF-8. String.prototype.charAt, charCodeAt, at, and codePointAt currently call stringCodeUnitAt, which creates a new WTF-8 iterator at byte zero for every indexed read. Primitive string .length similarly walks the complete WTF-8 payload on every access. A conventional for (i < text.length) sequential scan of an ASCII string therefore performs O(n²) decoding even though the string cell already caches that every byte is one ASCII UTF-16 code unit.
This is observable on real tooling, not only a synthetic microbenchmark. In main CI run 32905159766, #765's pinned Home benchmark validator spent 47m44s in algorithmic-growth before the 60-minute job cancellation. An exact pinned local reproduction remained inside the first JSON artifact hash after 368.86 seconds. The first sample showed repeated charCodeAt rescans. After routing indexed reads through the cached ASCII path, a second 3-second sample attributed 1,729/1,746 stacks to getPrimitiveMember -> utf16LenOfValue -> utf16LenOfString while evaluating the encoder's loop condition, proving the remaining repeated .length scan.
Scope
- Make indexed code-unit reads and UTF-16 length queries O(1) whenever the receiver's cached
StringCell.isAscii()classification proves byte offset equals UTF-16 index. - Route
charAt,charCodeAt,at,codePointAt, primitive string property indexing, and primitive.length/index-bound queries through the representation-aware path where the source cell identity is available. - Preserve the canonical WTF-8 iterator and exact surrogate/code-point semantics for non-ASCII strings and generic coerced byte slices whose representation is not known.
- Add focused semantic and large sequential-scan regression coverage, then verify the exact pinned Home workload and String Test262 subtree.
Security/performance invariant
A valid ASCII input must not amplify n indexed reads or n loop-bound length queries into quadratic engine work. The optimization must rely only on the immutable cached string-cell classification, require no input-specific precomputation, and add no mutable cache or synchronization surface.
No-workaround rules
- No benchmark timeout increase, reduced fixture, skipped validator, alternate engine, native hashing escape hatch, source-text matching, or swallowed failure.
- No incorrect byte indexing for Latin-1/non-ASCII/WTF-8, no changed lone-surrogate behavior, and no unbounded index cache.
Acceptance
- All affected indexed String operations and length queries use constant-time byte access for known-ASCII cells.
- Existing astral and lone-surrogate UTF-16 semantics remain covered and green.
- A large sequential ASCII
charCodeAt/property-index scan with a.lengthloop bound completes with the expected checksum. - The exact pinned zig-js-backed
algorithmic-growth --self-testcompletes successfully with measured before/after evidence. - Focused unit/TSan and Test262 String coverage show no regressions.