Repository navigation
Fix quadratic parsing with a [^ on every line when footnotes are enabled - #1136
Conversation
|
I've tested this, and can confirm that it has quadratic behavior on the main branch: % python3 -c 'N=100000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null 0.50s user 0.01s system 96% cpu 0.524 total
% python3 -c 'N=1000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null 49.10s user 0.20s system 99% cpu 49.567 totalAnd that the PR seems to fix it: % python3 -c 'N=100000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null 0.02s user 0.01s system 13% cpu 0.176 total
% python3 -c 'N=1000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null 0.09s user 0.03s system 85% cpu 0.147 total
% python3 -c 'N=10000000; print("[^\n" * N)' | time ./target/release/pulldown-cmark --enable-footnotes > /dev/null
./target/release/pulldown-cmark --enable-footnotes > /dev/null 0.91s user 0.43s system 88% cpu 1.505 total |
| // recognize a definition that interrupts a paragraph (no blank line before it), | ||
| // which is exactly the path the #1076 fix touches. | ||
| #[test] | ||
| fn test_footnote_definition_interrupts_paragraph() { |
There was a problem hiding this comment.
This test is redundant. We already have a paragraph interruption test in pulldown-cmark/specs/footnotes.txt.
| // (via `from_utf8(&bytes[2..])`) used to make the UTF-8 validation | ||
| // O(n) per line, which is O(n²) overall for inputs with a `[^` on | ||
| // every line. Limit the slice to the current line; see #1076. | ||
| core::str::from_utf8(&bytes[2..2 + scan_nextline(&bytes[2..])]).unwrap(), |
There was a problem hiding this comment.
I don't really like this fix. Couldn't we make scan_paragraph_interrupt_no_table accept a &str instead of a &[u8], and, then, not have to do UTF-8 validation at all?
|
Dropped the extra test.
|
| // Already a `&str`, so this no longer re-validates the remaining | ||
| // input as UTF-8 on every `[^` line. The linebreak handler | ||
| // rejects newlines, so the probe still only inspects the current | ||
| // line; see #1076. |
There was a problem hiding this comment.
I would leave this comment out. It's just a confusing piece of history.
| // Regression for https://github.com/pulldown-cmark/pulldown-cmark/issues/1076 | ||
| // (superlinear parsing with a `[^` on every line). | ||
| #[test] | ||
| fn test_footnote_like_lines_no_panic() { | ||
| // The original O(n²) trigger: a `[^`-prefixed line repeated. Parsing must | ||
| // complete without panicking. Uses ENABLE_FOOTNOTES (GFM), which is the path | ||
| // in the issue, not ENABLE_OLD_FOOTNOTES. | ||
| let mut opts = Options::empty(); | ||
| opts.insert(Options::ENABLE_FOOTNOTES); | ||
| let input = "[^x]\n".repeat(10_000); | ||
| let _ = Parser::new_ext(&input, opts).count(); | ||
| } |
There was a problem hiding this comment.
This is also not the right way to test a superlinear time regression (the code never panicked, right?). There's a place in dos-fuzzer for regression tests like this.
The footnote-definition probe in scan_paragraph_interrupt_no_table validates the entire remaining input for each [^-prefixed line via from_utf8(&bytes[2..]), making UTF-8 validation O(n) per line and O(n^2) overall. Limit the slice to the current line, matching the probe's &|_| None line-break handler. Fixes pulldown-cmark#1076.
Take &str in scan_paragraph_interrupt_no_table so the [^ probe can use the existing document string instead of from_utf8 on the remaining input. Drop the extra interrupt test already covered by footnotes.txt.
Drop the leftover comment. The errors.rs smoke test was not a superlinear check.
|
Removed the leftover comment and the |
17eafcc to
d8a0044
Compare
Title
Fix quadratic parsing with a
[^on every line when footnotes are enabledBody
scan_paragraph_interrupt_no_tableprobes for a footnote definition on each line whenENABLE_FOOTNOTESis set. For a[^-prefixed line it validated the entire remaining input:bytes[2..]is the rest of the document, so a[^at the start of every line makes this check quadratic (one of the superlinear patterns reported in #1076).The linebreak handler is
&|_| None, so the probe never looks past the current line. Limit the slice to that line:On a release build (Apple M4), parsing 80,000 lines of
[^x]\n(400 KB) went from ~292 ms to ~9 ms.Tests added in
tests/errors.rs: a 10,000-line smoke test withENABLE_FOOTNOTES, and a check that a footnote definition still interrupts a paragraph without a preceding blank line.