Zero-copy SlicedString search (indexOf/startsWith/endsWith/includes)#2547
Merged
Conversation
…ith/includes) SlicedString is a zero-copy view over a backing string, but it inherited the base JsString search methods, which route through ToString() — materializing (allocating) the whole substring on every indexOf/startsWith/endsWith/includes call. A fresh large slice that is searched and then discarded therefore allocated its full content each time. Make the four base search methods virtual and override them in SlicedString to search directly over AsSpan(). Ordinal char comparison is binary/sequence equality, so the parameterless span overloads produce results identical to the base StringComparison.Ordinal paths (verified across all target frameworks). Benchmark (StringSliceBenchmarks.SearchOnSlice — fresh slice + search per iteration): Allocation: 1,163,559 KB -> 395 KB (-99.97%) Time: 378 ms -> 27.5 ms (13.7x faster) The flat-string search guard (SearchOnFlat, dispatch-bound) and the existing slice benchmarks show no regression — the virtual keyword is below the noise floor because dynamic PGO devirtualizes the monomorphic call sites. Adds a differential correctness test (SlicedStringSearchMatchesFlatString) comparing sliced-view search against an identical flat string across many needles and positions, both before and after the view materializes. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
lahma
enabled auto-merge (squash)
June 27, 2026 10:14
This was referenced Jul 6, 2026
This was referenced Jul 13, 2026
This was referenced Jul 20, 2026
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.
Summary
SlicedStringis a zero-copy view over a backing string (produced byslice/substring/substrfor large results). It already overridesEquals/GetHashCode/AsSpanfor zero-copy, but the search methodsIndexOf/StartsWith/EndsWith/Containswere inherited from the baseJsString, which routes throughToString()— materializing (allocating) the whole substring on every search. A fresh large slice that is searched and then discarded therefore allocated its full content on eachindexOf/startsWith/endsWith/includescall.This makes the four base search methods
virtualand overrides them inSlicedStringto search directly overAsSpan(). Ordinalcharcomparison is binary/sequence equality, so the parameterless span overloads produce results identical to the baseStringComparison.Ordinalpaths (verified across all target frameworks).Benchmark
Jint.Benchmark/StringSliceBenchmarks.cs—SearchOnSlicecreates a fresh large slice and searches it each iteration (the shape that previously materialized on every call). Default BDN job, .NET 10, Ryzen 9 5950X, same-runtime worktree A/B:SearchOnFlatis a dispatch-bound guard searching a plain (non-view)JsString; making the methodsvirtualis below the noise floor (dynamic PGO devirtualizes the monomorphic call sites).Correctness
StringTests.SlicedStringSearchMatchesFlatStringcompares sliced-view search against an identical flat string across many needles and positions, both before and after the view materializes.dotnet test Jint.Tests: 3138 passed, 0 failed.🤖 Generated with Claude Code