Extend zero-copy string slice views to bounded-waste substrings#2518
Merged
Conversation
String.prototype.substring(start, end) clamps a negative end to 0 and swaps, so substring(12000, -1) on a 131072-char string yields a 12000-char result whose pinned backing-string remainder (119072 chars) exceeded the JsString.CreateSliced retention guard (length*2 >= source.Length), forcing a full copy on every call — 116 MB/iter in dromaeo-object-string (89.85% of its allocation), while the equivalent slice(12000, -1) returned a zero-copy view. Add an absolute-waste cap to the guard: a slice also becomes a view when the pinned-but-unused remainder (source.Length - length) stays within SliceViewMaxWastedChars (128K chars), so a moderate slice of a large source avoids the per-call copy without unbounded retention. Small slices still copy. Also corrected StringSliceBenchmarks.SubstringLargeDiscard, which used substring(12000, str.length-1) (positive args, no swap → already a view) and therefore never exercised the dromaeo shape. SubstringLargeDiscard (default job, stash A/B): -99.5% allocated (117776 -> 550 KB), -69% time; SliceSmall/SliceThenRead/SliceLargeDiscard guards flat. End-to-end dromaeo-object-string 129 -> 14.6 MB (-88.7%). Jint.Tests net10 (3067) + net472 (3005), PublicInterface (79/79), Test262 99260/0. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lahma
enabled auto-merge (squash)
June 9, 2026 14:58
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
String.prototype.substring(start, end)clamps a negativeendto 0 and then swaps so the larger index is the end. Sostr.substring(12000, -1)on a 131072-char string yields a 12000-char result whose pinned backing-string remainder (119072 chars) exceededJsString.CreateSliced's zero-copy retention guard (length*2 >= source.Length→ 24000 < 131072), forcing a fullstring.Substringcopy on every call. The visually-identicalstr.slice(12000, -1)resolves-1relative to length without swapping → a 119071-char span → passes the guard → zero-copy view. That asymmetry is whyslicewas ~1% ofdromaeo-object-string's allocation whilesubstringwas 89.85% (116 MB/iter).This adds an absolute-waste cap to the guard: a slice also becomes a
SlicedStringview when the bytes it pins but never uses (source.Length - length) stay withinSliceViewMaxWastedChars(128K chars). So a moderate slice of a large source avoids the per-call copy while retention stays bounded (≤ result + 256 KB). Small slices still copy.The PR also corrects
StringSliceBenchmarks.SubstringLargeDiscard, which usedsubstring(12000, str.length - 1)(positive args, no swap → already a view) and so never measured the dromaeo shape that copies — the reason #2506's benchmark reported substring as a small fraction.Benchmarks (default job, stash A/B)
SubstringLargeDiscard— AllocatedSubstringLargeDiscard— timeSliceSmall(must still copy)SliceThenRead/SliceLargeDiscardEnd-to-end
dromaeo-object-stringallocation 129 MB → 14.6 MB (−88.7%) (substring 116 MB → 1.6 MB), via MemoryProbe. Jint stays rank #1 on object-string by time.Tests
🤖 Generated with Claude Code