👌 Fix quadratic inline tokenization on runs of special characters - #423
Merged
Conversation
…cters
Long runs of characters that begin an inline rule but never complete a
construct -- bare `&`, `<`, `~`, `{`, incomplete entities -- were tokenised
in O(n^2) time, so a few hundred KB of such input stalls `render()` for
many seconds. Output was always correct; the cost is pure CPU.
Two independent quadratic factors, each hit once per character in a run:
1. `state.pending` accumulation. The inline tokenizer's fallback path did
`state.pending += state.src[state.pos]` one character at a time. Because
`pending` is an *attribute*, `str += ch` cannot use CPython's in-place
concatenation optimisation (the attribute holds a second reference), so
every append copies the whole accumulated string.
2. `src[pos:]` slicing in `entity` / `html_inline`. Both matched
`^`-anchored regexes against `state.src[pos:]`, copying the remainder of
the source on every `&` / `<`. On Python 3.10 this carried a second
quadratic factor inside the regex engine itself: before 3.11, `search()`
on a `^`-anchored pattern retries at every offset.
Neither is quadratic in the JavaScript markdown-it (rope-backed string
concatenation, and sticky/lastIndex regex matching), so this is specific to
the Python port.
Fix:
- `StateInline.pending` now accumulates through a lazily-materialised list
buffer via `append_pending()`, giving amortised O(1) appends. It is still
exposed as a plain `str` property, so existing readers and third-party
plugins doing `state.pending += x` keep working unchanged.
- `entity` and `html_inline` anchor with `.match(state.src, pos)` instead of
slicing; the leading `^` is dropped since `.match` already anchors at
`pos`. Neither pattern uses `\b` or lookbehind, so this is exactly
equivalent. `HTML_TAG_RE` has a single consumer; the separate
`HTML_OPEN_CLOSE_TAG_RE` is untouched.
All affected inputs now grow ~2.0x per doubling (linear) out to 640k
characters. `"&" * 400_000` drops from 6.8s to 1.3s on CPython 3.11, from
653s to 1.8s on CPython 3.10, and from 145s to 0.2s on PyPy. Runs of `[`
were also reported as superlinear, but once the `pending` quadratic is
removed they measure a flat 2.0x per doubling: that path is linear already
(bounded by the existing `skipToken` cache), just with a large constant.
Output is unchanged: a 47,936-case differential run over the repo fixtures,
the CommonMark spec, targeted constructs and fuzzed inputs across seven
presets compares rendered HTML, `renderInline`, and full token streams
byte-for-byte against the previous behaviour with zero differences. The
test suites of mdit-py-plugins, myst-parser, mdformat and rich are
unaffected.
Adds `test_long_special_char_runs_are_linear`, which asserts correct output
for the affected constructs and renders inputs that took ~16s before this
change, so a regression trips the global 10s test timeout.
Same class as the previously fixed #367 and #389. The root causes and this
fix were independently identified by Haim Dimer in #411, which this
supersedes.
Co-authored-by: Haim Dimer <haim@dimer.org>
The buffered `StateInline.pending` from the previous commit materialised its buffer with `self._pending += joined`. That is itself an attribute concatenation, so every read copied the whole accumulated string. Any inline rule that reads `state.pending` before its cheap character check therefore re-introduced the quadratic on every character: the mdit-py-plugins `attrs` rule does exactly that, and myst-parser's `attrs_inline` extension enables it. With that plugin, `"~a~" * 400_000` still took 34s (5.7x per doubling) after the previous commit. The getter now moves the string into a local and drops the instance's reference before concatenating, so CPython can resize it in place. The same input takes 3.0s and grows 2.0x per doubling; verified on CPython 3.10 through 3.13. A str handed out to a caller is never mutated by this path, since a second reference disables the in-place resize. Two latent hazards of the list buffer are closed at the same time: - The setter used `.clear()`, so assigning `pending` on an instance whose `__init__` had not run (a subclass setting it before `super().__init__()`, or `__new__` plus assignment) raised `AttributeError`. It now assigns a fresh list. - `copy.copy(state)` shared the mutable buffer, so appends on the copy leaked into the original. A `__copy__` gives the copy its own buffer. Tests cover the buffer's ordering, flush, copy and pre-init semantics, and a rule that reads `pending` on every character over a 800k-character run, which exceeded the global 10s test timeout before this change.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #423 +/- ##
==========================================
+ Coverage 95.85% 95.88% +0.02%
==========================================
Files 64 64
Lines 3619 3643 +24
==========================================
+ Hits 3469 3493 +24
Misses 150 150
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The two regression tests added with the fix relied on the global 10s pytest timeout to catch a return to quadratic behaviour. CI runs every matrix job under `--cov`, whose tracer slows these pure-Python loops by more than 4x, so the tests timed out on the fixed code. Following the precedent of #367 and #389, the tests now pin the behaviour deterministically rather than by wall clock: - `test_inline_rules_do_not_slice_remaining_source` drives the inline parser with a `str` subclass that records every slice taken from it. The old `entity` / `html_inline` rules produced ~5000 slices of up to 10000 characters on a 5000-opener run; the only slices left are the `text` rule's single-character chunks. - `test_pending_appends_do_not_materialise` checks that `append_pending` buffers fragments without building the string until `pending` is read. - `test_long_special_char_runs_render_correctly` keeps the output assertions for every affected construct at modest sizes. - `test_rule_reading_pending_each_char_renders_correctly` interleaves per-character reads of `pending` with appends and checks the output. Each of the structural tests fails on the pre-fix code and passes in well under a second with or without coverage.
chrisjsewell
force-pushed
the
claude/determined-franklin-cyr79i
branch
from
September 8, 2026 11:33
c7e95a6 to
856320a
Compare
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.
Summary
Long runs of characters that begin an inline rule but never complete a construct (bare
&,<,~,{, incomplete entities, …) were tokenised in O(n²) time, so a few hundred KB of such input stallsrender()for many seconds. Output was always correct; the cost is pure CPU. This is the same class as #367 and #389.Supersedes #411. The root causes and the core fix were independently identified by @hdimer there, and the first commit here is code-identical to that PR (credited as co-author, thank you!). The second commit closes a gap found while auditing it that made the fix ineffective for a common plugin configuration, and adds a few hardening changes.
Root causes
Two independent quadratic factors, each hit once per character in a run:
state.pendingaccumulation. The inline tokenizer's fallback path didstate.pending += state.src[state.pos]one character at a time. Becausependingis an attribute,str += chcan't use CPython's in-place concatenation optimisation (the attribute holds a second reference), so every append copies the whole accumulated string.src[pos:]slicing inentity/html_inline. Both matched^-anchored regexes againststate.src[pos:], copying the remainder of the source on every&/<. On Python 3.10 this carried a second quadratic factor inside the regex engine: before 3.11,search()on a^-anchored pattern retries at every offset.Neither is quadratic in the JavaScript markdown-it (rope-backed concat, sticky regex matching), so this is Python-port-specific.
Fix
Commit 1 (as in #411):
StateInline.pendingaccumulates through a lazily-materialised list buffer via a newappend_pending()method, giving amortised O(1) appends. It's still exposed as a plainstrproperty, so existing readers and plugins doingstate.pending += xkeep working.entityandhtml_inlineanchor with.match(state.src, pos)instead of slicing; the leading^is dropped since.matchanchors atpos. Neither pattern uses\bor lookbehind, so this is exactly equivalent.Commit 2 (new):
self._pending += joined, itself an attribute concat. Any rule that readsstate.pendingbefore its cheap character check therefore re-introduced the quadratic on every character. The mdit-py-pluginsattrsrule does exactly that, and myst-parser'sattrs_inlineextension enables it: with it,"~a~" * 400_000still took 34 s (5.7× per doubling) after commit 1. The getter now moves the string into a local and drops the instance's reference before concatenating, so CPython resizes it in place: 3.0 s, 2.0× per doubling, verified on CPython 3.10–3.13.__init__having run (a subclass assigningpendingbeforesuper().__init__()raisedAttributeError).copy.copy(state)no longer shares the mutable buffer with the original.Results
"&" * 400_000, before → after:All affected inputs now grow ~2.0× per doubling out to 640k characters. Runs of
[were also reported as superlinear, but once thependingquadratic is removed they measure a flat 2.0× per doubling: that path was already linear (bounded by theskipTokencache), just with a large constant.Trade-off: the property indirection costs ~2% on ordinary Markdown (up to ~4.7% on inline-dense files) — measured over 55 inputs × 3 presets, 8 interleaved rounds. The regex half is a pure win at every size. Memory is unchanged on realistic input. If wanted, most of the 2% can be recovered by having
push()/pushPending()read the private fields directly; happy to do that as a follow-up once benchmarked properly.Verification
renderInlineand full token streams byte-for-byte: 0 differences.master; a 61-document differential through a full plugin stack shows 0 differing renders.ruffand strictmypyclean.test_long_special_char_runs_are_linear,test_state_inline_pending_buffer_semantics,test_pending_reader_rule_stays_linear. Each linearity test exceeds the global 10 s timeout on the pre-fix code.Notes for reviewers
HTML_TAG_RE,DIGITAL_REandNAMED_RElose their^anchor (they're now applied with.match(src, pos)). No external consumer was found in any scanned package or via code search, but any third-party.search()on them would now be unanchored. Probably worth a changelog line.HTML_OPEN_CLOSE_TAG_REis untouched.textrule that still doesstate.pending +=; it keeps its quadratic and gains nothing from this fix.html=True(thecommonmarkandgfm-likepresets), runs of<![CDATA[,<!--,<?and<!ain inline context are still quadratic because the regex sub-patterns rescan to end-of-input on every attempt (<![CDATA[× 40k takes ~245 s). This is inherited from upstream (it's quadratic in markdown-it JS too), pre-existing, and a different code path, so it's kept separate. A validated fix (terminator quick-reject, zero output change) is ready.