Skip to content

Skip the container scan on consecutive blank lines - #1148

Merged
Martin1887 merged 2 commits into
mainfrom
blank-line-containers
Sep 28, 2026
Merged

Martin1887 merged 2 commits into
mainfrom
blank-line-containers

Conversation

@notriddle

@notriddle notriddle commented Sep 25, 2026 •

Copy link
Copy Markdown
Collaborator

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

% 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

% 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

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
notriddle force-pushed the blank-line-containers branch 3 times, most recently from f94c0cd to 9866ec9 Compare September 27, 2026 04:22
@notriddle
notriddle force-pushed the blank-line-containers branch from 9866ec9 to 366a6e5 Compare September 27, 2026 04:27
@Martin1887
Martin1887 merged commit 2098dd9 into main Sep 28, 2026
12 checks passed
@notriddle
notriddle deleted the blank-line-containers branch September 28, 2026 12:01
@notriddle notriddle mentioned this pull request Oct 20, 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