Repository navigation
perf: build a parse error over many lines in time proportional to the template length - #338
Merged
Merged
Conversation
… template length A parse error whose span covered many lines took time that grows with the number of lines times the length of the template. Pest's `Error::new_from_span` finds the span's last line by walking every line, and each step of `Span::lines()` searches from the ends of the whole input. A 20,000-line invalid expression took more than a minute to report (134 s in a debug build, now about 0.2 s in the same build). `ParseError::from_span` now builds a span over three or more lines from a copy that holds only its first and last lines, which is all the error text depends on. It then sets the real byte range and the start and end line and column, which pest computes in one pass each. Spans over one or two lines still go straight to pest. The result is identical to pest's own, field for field: a test compares `Display` and `Debug` for every span of 400 random inputs with `\r`, `\n`, tabs and multi-byte characters, and the errors for nine multi-line templates, observed before the change, read the same after it. The root cause is in pest's `Span::lines()`. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
A parse error whose span covers many lines is now built in time proportional to the template length. A 20,000-line invalid expression took more than a minute to report (134 s in a debug build) and now takes about 0.2 s in the same build.
What was wrong
ParseError::from_span(crates/citry_template_parser/src/error.rs) builds every Citry error that has a range by calling pest'sError::new_from_span.Span::lines().Position::find_line_startandfind_line_endin pest 2.8.3), so the cost is the line count times the input length.The root cause is upstream, in pest's
Span::lines(). This PR works around it in Citry.What changes
The error text depends only on the span's first and last lines and on its first and last characters. So for a span over three or more lines,
from_spannow:locationandline_col, which pest computes in one pass each.Spans over one or two lines still go straight to pest, because they already cost only a pass or two.
Evidence
tests/error_spans.rs: for every span of 400 random inputs with\r,\n, tabs and multi-byte characters,DisplayandDebugmatch pest's ownnew_from_spanexactly. More than 1,000 of those spans take the new path.The same file has a scaling test: a 20,000-line span, built directly and through
parse_template, must finish in under a second. With pest's path put back, it took 134 s and failed.Before changing anything, I recorded
Display,Debuganddiagnostic()for nine multi-line template errors on main, including CRLF and multi-byte cases. After the change they are byte-identical. So the messages users see and the ranges the LSP andcitry checkread are unchanged.Focused checks:
cargo testfor citry_template_parser and citry_template_formatter (638), plus clippy and fmt;citry check, format and render tests.By agreement, the full local gate was not run, because disk is short. PR CI runs it.
🤖 Generated with Claude Code