Repository navigation
Conversation
Once a line has nothing left but its line ending, the rest of scan_containers no longer depends on it: every list item, definition and footnote definition matches without consuming anything, and the first remaining block quote fails. Jump straight to that block quote instead of walking the rest of the spine. Walking it made every blank line cost time proportional to the nesting depth, so deeply nested lists followed by blank lines parsed in quadratic time. The tree now tracks where block quotes sit on the spine, so the jump is constant time.
notriddle
added a commit
that referenced
this pull request
Sep 25, 2026
This commit accomplishes the same thing as <#1145>, but without allocating more memory. The fix is shamelessly "borrowed" from <commonmark/cmark#520>, which fixes the same bug. Deeply nested lists followed by blank lines parsed in quadratic time. `scan_containers` runs once per line and walked the whole spine, and on a blank line every list item matched without consuming anything, so each blank line cost time proportional to the nesting depth. List levels cost two bytes each and blank lines one, so a small input goes a long way. Before ------ ```console % python3 -c 'N=800; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 16% cpu 0.076 total % python3 -c 'N=8_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.34s user 0.00s system 88% cpu 0.390 total % python3 -c 'N=80_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 34.42s user 0.18s system 99% cpu 34.770 total % python3 -c 'N=800; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.00s system 15% cpu 0.073 total % python3 -c 'N=8_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.34s user 0.00s system 87% cpu 0.386 total % python3 -c 'N=80_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 33.39s user 0.09s system 99% cpu 33.562 total ``` After ----- ```console % python3 -c 'N=800; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 7% cpu 0.113 total % python3 -c 'N=8_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 11% cpu 0.077 total % python3 -c 'N=80_000; print("- " * N + "a" + "\n" * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 21% cpu 0.087 total % python3 -c 'N=800; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.00s system 7% cpu 0.075 total % python3 -c 'N=8_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.00s user 0.01s system 13% cpu 0.075 total % python3 -c 'N=80_000; print(">" + "- " * N + "a" + "\n> " * N * 2)' | time ./target/release/pulldown-cmark > /dev/null ./target/release/pulldown-cmark > /dev/null 0.01s user 0.01s system 28% cpu 0.067 total ```
Collaborator
|
Superseded by #1148 |
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.
Deeply nested lists followed by blank lines parse in quadratic time.
scan_containersruns once per line and walks the whole spine, and on a blank line every list item matches without consuming anything, so each blank line costs time proportional to the nesting depth. List levels cost two bytes each and blank lines one, so a small input goes a long way:The same happens with
1.markers, tabs, a fence or indented code inside the deep list, and a list inside a block quote continued with>lines. Lazy continuation lines never had this cost: the scan stops at the first list item they don't satisfy.The change
Once the line has nothing left but its ending, the rest of the scan no longer depends on it: every remaining list item, definition and footnote definition matches without consuming anything, other nodes match unconditionally, and the first block quote fails. So the result is the position of the next block quote on the spine, or the spine length.
Treenow keeps the spine positions of its block quotes (updated inpush,pop,remove_nodeandreset), which makes that a constant-time lookup;scan_containerscounts the block quotes it has matched to index it. A partially consumed tab keeps the existing path until its columns are used, so whitespace-only lines behave as before.push/pop/remove_node/resetmove fromimpl<T> Tree<T>toimpl Tree<Item>, sincepushhas to look at the item; the crate only usesTree<Item>. Happy to reshape that if you would rather keep them generic.Timing (release, parse only;
dlevels, then2dblank lines, 32765 at d = 16384)-× d1.× d-\t× d>then-× d,>linesTesting
fmt, the bench build and the dos-fuzzer regressions. I had no 1.71.1 toolchain locally, so the MSRV check was clippy'sincompatible_msrvlint (clean); the MSRV, nightly and wasm32 jobs are CI's.specs/regression.txtcover the partially-consumed-tab case; the existing suite does not notice if that guard is removed, and these do.pathological_blank_lines_in_nested_listsbench group.mainon about 2.9 million generated documents (these shapes at many depths, plus whitespace and tab lines,\r\n, quotes, fences, tables, HTML, footnotes, definition lists,:::containers, task lists and math) under 13 option sets.The dos-fuzzer's patterns (a prefix, one repeated unit, a suffix) can't grow depth and blank lines together, so this has no
--regressionsentry; the bench group and the regression examples guard it instead. Glad to open an issue first if you'd prefer, as CONTRIBUTING suggests.