You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Report pest's slow multi-line errors upstream, then drop Citry's workaround #354
Building a parse error for a span that covers many lines takes time proportional to the number of lines times the length of the whole input. #338 avoids it in Citry; this issue tracks reporting the root cause to pest and removing the avoidance once pest is fixed.
What happens in pest
pest::error::Error::new_from_span (pest 2.8.3) walks every line of the error span through Span::lines(). Each step of that iterator calls find_line_start and find_line_end, which scan the input from the span's position, not from where the previous line ended. So building one error costs about (lines in the span) × (input length).
Measured in Citry before #338, on an expression error whose span covered the whole template:
Lines in the span
Time
1,000
0.1 s
4,000
2.8 s
20,000
75 s
What Citry does today
ParseError::from_span (crates/citry_template_parser/src/error.rs) builds a long span's pest error from a copy that holds only the span's first and last lines, so the error text is byte-identical and the cost is linear. A test compares the output with pest's own for every span of 400 random inputs, and a scaling test checks that a 20,000-line span takes under a second.
Title:Error::new_from_span is quadratic in the number of lines in the span
Body: the explanation above, plus a minimal reproduction: a grammar that fails on input of N lines with a span covering all of it, timed for N = 1,000, 4,000 and 20,000.
Suggested fix: make the Span::lines() iterator (LinesSpan) carry the end of the previous line forward, so each line is found in time proportional to its own length. Alternatively, have new_from_span find only the lines it prints.
When pest ships a fix and Citry upgrades to it, remove the workaround in ParseError::from_span and keep the scaling test.
Activity