Skip to content

Skip the container scan on blank lines - #1145

Closed
mishan wants to merge 1 commit into
pulldown-cmark:mainfrom
mishan:blank-line-containers
Closed

mishan wants to merge 1 commit into
pulldown-cmark:mainfrom
mishan:blank-line-containers

Conversation

@mishan

@mishan mishan commented Sep 10, 2026

Copy link
Copy Markdown
Contributor

Deeply nested lists followed by blank lines parse in quadratic time. scan_containers runs 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:

let d = 16384;
let s = format!("{}a\n{}", "- ".repeat(d), "\n".repeat(2 * d));
Parser::new_ext(&s, Options::empty()).count(); // 64 KB input

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. Tree now keeps the spine positions of its block quotes (updated in push, pop, remove_node and reset), which makes that a constant-time lookup; scan_containers counts 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/reset move from impl<T> Tree<T> to impl Tree<Item>, since push has to look at the item; the crate only uses Tree<Item>. Happy to reshape that if you would rather keep them generic.

Timing (release, parse only; d levels, then 2d blank lines, 32765 at d = 16384)

shape d = 2000 4000 8000 16384
- × d 40.3 → 0.29 ms 159 → 0.48 642 → 0.86 2581 → 1.69
1. × d 37.8 → 0.20 155 → 0.40 616 → 0.81 2602 → 1.90
-\t × d 38.1 → 0.21 154 → 0.42 616 → 0.82 2586 → 1.68
fence in the deep list 38.0 → 0.21 150 → 0.43 607 → 0.85 2554 → 1.75
> then - × d, > lines 37.8 → 0.23 155 → 0.45 614 → 0.90 2576 → 1.87

Testing

  • The full suite passes unchanged on stable with each feature set CI tests, plus fmt, the bench build and the dos-fuzzer regressions. I had no 1.71.1 toolchain locally, so the MSRV check was clippy's incompatible_msrv lint (clean); the MSRV, nightly and wasm32 jobs are CI's.
  • Two regression examples in specs/regression.txt cover the partially-consumed-tab case; the existing suite does not notice if that guard is removed, and these do.
  • A pathological_blank_lines_in_nested_lists bench group.
  • A differential harness found no event or offset differences from main on 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 --regressions entry; the bench group and the regression examples guard it instead. Glad to open an issue first if you'd prefer, as CONTRIBUTING suggests.

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
```
@notriddle

Copy link
Copy Markdown
Collaborator

Superseded by #1148

@notriddle notriddle closed this Sep 29, 2026
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.

2 participants