Skip to content

perf: build a parse error over many lines in time proportional to the template length - #338

Merged
JuroOravec merged 1 commit into
mainfrom
fix/pest-error-span
Oct 24, 2026
Merged

JuroOravec merged 1 commit into
mainfrom
fix/pest-error-span

Conversation

@JuroOravec

Copy link
Copy Markdown
Member

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's Error::new_from_span.

  • That function finds the span's last line by walking every line with Span::lines().
  • Each step of that walk searches from the ends of the whole input (Position::find_line_start and find_line_end in pest 2.8.3), so the cost is the line count times the input length.
  • Any error over a long multi-line range paid it, whatever the cause of the error.

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_span now:

  • builds the pest error from a copy that holds only those two lines, with the span mapped onto it;
  • then sets the error's real location and line_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, Display and Debug match pest's own new_from_span exactly. 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, Debug and diagnostic() 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 and citry check read are unchanged.

  • Focused checks:

    • cargo test for citry_template_parser and citry_template_formatter (638), plus clippy and fmt;
    • Python: the citry_core suite (499), and 655 citry_lsp, 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

… 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]>
@JuroOravec
JuroOravec merged commit 53183a0 into main Oct 24, 2026
25 of 32 checks passed
@JuroOravec
JuroOravec deleted the fix/pest-error-span branch October 24, 2026 20:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant