Repository navigation
perf(parser): parse a line of mid-line #!, #+ or % in linear time - #2991
Merged
Merged
Conversation
A #!, #+ or % that is not a column-0 comment is split into its first character (#2928). logos lexed each as a comment token to the line end first, so a line repeating them relexed the rest of the line per repetition. TokenIter::next now splits the character off from the bytes before lexing (split_comment_start), with the old rewrite kept as a counted fallback. name_mid_line_hash_comments then scanned back to each marker's line start and searched the error list from the front, so a file of indented #+ lines was quadratic too. It now carries the line start forward, finds each line's first non-blank once, and walks a start-sorted error list with a cursor (falling back to the old search if unsorted). Tokens, CST and every error are byte-identical to main on 1,130 real ledgers and 400,000 generated inputs. Two byte-counter tests guard both halves and fail when either fix is undone. Closes #2989 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
This was referenced Nov 15, 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.
What
A line that repeats a mid-line
#!,#+or%(1#!1#!...,x #+#+...,x %x %x ...), and a file of many indented#+xlines, now parse in linear time. Output is unchanged: tokens, the CST and every error are byte-identical to main.Why
Two quadratics, both in the handling #2928 added for a
#!/#+/%that is not a column-0 comment (beancount reads it as a#or%and the characters after it):TokenIter::next,crates/rustledger-parser/src/logos_lexer.rs). logos lexed each one as aShebang/EmacsDirective/PercentCommenttoken to the end of its line, thennextkept only the first character and restarted the lexer after it. A line of N such markers lexed the rest of the line N times.name_mid_line_hash_comments,crates/rustledger-parser/src/cst/convert.rs). For each marker it didrfind('\n')back to the line start, a blank check over the same span, anderrors.iter_mut().find(..)from the front of the error list. A line of N markers cost its length N times, and a file of N indented#+lines (one error each) cost N errors per line.The issue saw only the first; with it fixed, timing showed
crate::parsestill quadratic on the same inputs, and a pass-by-pass timing put all of it inname_mid_line_hash_comments.How
TokenIter::split_comment_startdecides from the bytes, before logos runs, whether the lexer stands on a#!/#+/%thatnextwould split (the same conditions as the existing rewrites: not a column-0#!/#+; not a%that starts a column-0 line beancount ignores). If so it emits the one-characterHash/Flag("%")token and bumps the lexer by one byte; the token then goes through every later step ofnext, so an indented one still gets itsIndent. The old post-lex rewrites stay as a fallback and are counted.name_mid_line_hash_commentscarries the line start forward from the previous marker (markers come in order), finds each line's first non-blank once, and, when the error list is sorted by start (asparse_via_cst_innerleaves it; checked on entry), walks it with a cursor. Each marker's window (line_start..=at, oratalone) never starts before the previous one's, and an error moved to a marker's#lies before every later window, so the cursor finds exactly the first match the front-to-back search found. An unsorted list falls back to that search.While in
logos_lexer.rs, the doc comment oferror_later_in_runthat #2988 left abovelexer_atis moved back to its function (no code change).Identity
parse_error_linesoutput and a hash of the full CST debug tree plus the ASTparseresult, main (c434a08110e) vs this branch:*.beancount/*.beanin the repo and the fetched compat corpus#!,#+,#,;,%, tags, links, quotes, BOM,\r, lone capitals, dates, accounts, at line start and mid-line, LF/CRLF, with and without a final newline, a leading BOM#!/#+/ indented#+xpiecescorpus_baseline(strict, against the fetched compat corpus) and the BOM matrix pass unchanged.Timing
Release
parse_error_lines, seconds, one input per shape:1#!runx #!#!run#!Tafter a flag (2024-01-02 P#!T...)x #+#+runx %words#+xlines#+runA#!runA#+runA;xwords (#2988)A%xwords (#2988)T#!words (#2990)"x"T(#2990)#!/%/#+linesGuards
test_tokenize_lossless_split_comment_is_not_lexed_to_its_line_endcounts the comment bytes the fallback rewrites lex (SPLIT_COMMENT_LEXED_BYTES) on ten shapes. Withsplit_comment_startdisabled it fails (1#!run: 600,030,000 bytes for a 60,002-byte input).test_parse_mid_line_markers_are_named_in_linear_stepscounts the source bytes and errorsname_mid_line_hash_commentssteps over (MID_LINE_MARKER_STEPS) on five shapes. It fails with the cursor disabled (indented#+lines: 800,159,996 steps for 120,000 bytes) and with the first-non-blank cache disabled (blanks then#+: 400,059,999 steps for 60,001 bytes).Caches
No bump. Parsed directives, errors and their spans and messages are byte-identical (above), so nothing either cache stores changes.
How to test
Closes #2989
🤖 Generated with Claude Code