Repository navigation
Fix quadratic-time parsing of consecutive reference definitions - #1678
Merged
waylan merged 3 commits intoOct 29, 2026
Merged
Conversation
ReferenceProcessor and FootnoteBlockProcessor record one definition per pass and return the rest of the block to be parsed again, so every earlier block processor rescans the shrinking block once per definition. Record a run of definitions in a single pass, leaving the last one for the next pass when other content follows it so the earlier processors still see the same blocks they do today. Add regression tests and a changelog entry.
waylan
requested changes
Oct 27, 2026
Use finditer for footnote definitions as for reference definitions, and no longer leave the last definition of a run, or an indented one, for the next pass. A setext underline after consecutive reference definitions is now a paragraph rather than turning the last definition into a header.
waylan
reviewed
Oct 28, 2026
Record each reference definition as it is matched rather than collecting them first, and add tests covering runs of reference and footnote definitions next to split definitions, paragraphs, headers, rules, blockquotes and code blocks.
waylan
approved these changes
Oct 29, 2026
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.
Description
ReferenceProcessor.runrecords one reference definition, puts the rest of the block back at the front ofblocksand returns. The next pass runs every block processor'stestover the remainder again, andBlockQuoteProcessor,HashHeaderProcessorandHRProcessorsearch the whole block, so a block of n consecutive definitions costs O(n²). On CPython 3.12, 8000 definitions (247 KiB) take 18 s to convert with no extensions.FootnoteBlockProcessor.runin thefootnotesextension has the same one-definition-per-pass shape, and 8000 footnote definitions (177 KiB) take 14 s.Both processors now record a run of definitions in one pass. Only a definition that is directly followed by another definition is taken early. The last one, and any definition that is indented or spans lines, is left for the next pass as before, because the block starting at it can belong to another processor: an indented code block when
tab_lengthis below 4, or a setext header such as[b]: /urlfollowed by===(covered by a new test). The same inputs now take 0.01 s and 0.23 s.Output is identical to master over about 1.2 million randomly generated documents rendered with no extensions,
extra,footnotes,tables,def_list,admonition, andtab_lengthof 2 and 3. Definitions separated by other text are still handled one per pass, since what follows a definition can change how the earlier processors treat the block.Timing script used:
AI Assistance Disclosure
No AI tools were used in preparing this PR.
AI tools were used and I have disclosed which ones, and have fully reviewed and verified their output.
AI tools were use to assist with the following:
Checklist