perf(vm): cache string character indexes with char_index 馃У - #223
Open
timfennis wants to merge 1 commit into
Open
perf(vm): cache string character indexes with char_index 馃У#223timfennis wants to merge 1 commit into
timfennis wants to merge 1 commit into
Conversation
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.
Repeated string indexing currently scans UTF-8 codepoints both to check bounds and to find the requested character. Cache character offsets with
char_index0.1.5 so repeated reads reuse that work. Builds on the benchmarks in #222.Code changes
Rc<RefCell<String>>storage withRc<VmString>. The new wrapper owns either a plainStringorOwnedIndexedChars; the first indexed read moves the existing string allocation into the indexed representation.borrow_mut(), which recovers the backingString. Aliases share the same storage, and ordinary appends do not rebuild an index. Existing native string conversions still borrow Rust strings..lenwhen available. Length-only queries do not construct an index. Both indexing overloads use cached character lookup; slices copy between indexed UTF-8 byte boundaries.The first indexed read after creation or mutation remains O(n). Cached ASCII lookup is O(1); Unicode lookup is O(log n) in the worst case. The wrapper adds fixed metadata to every runtime string, and indexed Unicode strings allocate roughly one byte per codepoint plus rollover entries. ASCII needs no per-character index allocation.
char_indexis MPL-2.0 licensed and introduces no transitive runtime dependencies.Measurements
Same baseline commit (
faaae70) and release configuration before and after the integration, using rustc 1.93.0 on an Intel i7-1360P. These are complete program timings, including setup and initial cache construction.4-fixed.ndc)19-new.ndc, part 1)The three repository benchmarks used Criterion with 20 samples, 1 s warmup, and a 3 s measurement target. AoC runs used the user's existing solutions and actual inputs, with 30 runs per binary after 3 warmups, sequentially pinned to CPU 0. All measured puzzle outputs matched. The 2018 day 5 measurement used an exact extract of the existing part 1; it is not a whole-puzzle result. Short-string gains are modest, and 2024 day 19 showed no measurable change.
Validation
cargo fmt --allcargo test --locked --workspace --quietcargo build --locked --no-default-featurescargo bench --locked -p benches --bench benchmark -- 'string_(index|concat)' --sample-size 20 --warm-up-time 1 --measurement-time 3 --baseline before-char-index2500.Added tests cover UTF-8 index rollovers, preserving the string allocation, invalidation after writes through aliases, character and slice assignment, append, reverse, self-concatenation, copies, generic indexing, and empty strings. Workspace tests retain the pre-existing LSP unused-qualification warning.
AI disclosure: Codex implemented the integration and tests, ran the comparisons, and drafted this PR description.