perf: speed up read_span with bulk memchr line scanning#211
Draft
connorshea wants to merge 1 commit into
Draft
Conversation
context_info now scans the region before the span in bulk with memchr2 instead of a byte-at-a-time loop, deriving line count, context line starts, and column from newline positions. The original byte-loop still handles the span itself and the trailing context. Measured on a 3MB source with the span near EOF: read_span goes from 2.17ms to 260us per call (8.4x, LF endings) / 453us (4.8x, CRLF); end-to-end GraphicalReportHandler render of a single-label diagnostic drops from 3.43ms to 390us (8.8x). Verified against the previous implementation with 300k+ differential fuzz cases (0 mismatches). Also fixes a panic found by that fuzzing: a zero-length span starting exactly one byte past EOF sliced out of bounds instead of returning MietteError::OutOfBounds. Adds a regression test. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Member
Author
|
I need to review/do more testing before marking this ready |
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
context_info(the engine behind every built-inSourceCode::read_spanimpl) walked the source byte-at-a-time from offset 0 for every call. This PR scans the region before the span in bulk withmemchr2instead, deriving line count, context line starts, and column arithmetically from newline positions. The original byte-loop is kept verbatim for the span itself and the trailing context, so behavior is unchanged.It also fixes a panic found while validating the change: a zero-length span starting exactly one byte past EOF (
"a".read_span(&(2, 0).into(), 0, 0)) sliced out of bounds instead of returningErr(MietteError::OutOfBounds). This was reachable fromGraphicalReportHandler, which callsread_span(label, 0, 0)on the primary label, so an off-by-one zero-length label span could panic report rendering. A regression test is included.memchrmoves from transitive to direct dependency.Performance
Measured on a 3MB / 40k-line source, span near EOF,
--release:read_span, LF endingsread_span, CRLF endingsRender cost previously scaled linearly with file size (3 full-file scans per rendered diagnostic); those scans are now ~8x cheaper. Heap usage is unchanged: one 32-byte
VecDequeallocation per call at default context settings, independent of file size (verified with a counting allocator).Verification
Err(OutOfBounds).color_formatfailures are pre-existing/environmental — they fail identically onmainin a non-tty sandbox).🤖 Generated with Claude Code