Repository navigation
Fix quadratic processing time in fenced_code preprocessor (up to ~10x faster) - #1660
Open
deadstrobe5 wants to merge 2 commits into
Open
deadstrobe5 wants to merge 2 commits into
deadstrobe5 wants to merge 2 commits into
Conversation
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.
The
FencedBlockPreprocessorrebuilt the whole document text after every fenced code block it stashed (text = f'{text[:m.start()]}\n{placeholder}\n{text[m.end():]}'), so its cost grew with document size × number of blocks. This collects the output in pieces and joins them once.Matching is unchanged: the search now resumes at
m.end()in the original text instead of just after the placeholder in the rewritten text. The two differ only by the inserted\n, and since the fence pattern ends in[ ]*$,text[m.end()]is always\nor the end of the text, so no fence can begin at that position either way.Benchmark
The project's own
docs/*.mdconcatenated (250 KB, ~220 fenced blocks), and the same text ×4 (1 MB). Python 3.12, Apple M5, 7 alternating processes per side:markdown(text, extensions=['extra']), 250 KBmarkdown(text, extensions=['extra']), 1 MBextraon a 10 KB documentChecks
tests/**/*.txtfixtures anddocs/**/*.md) × 4 extension sets, plus 12,000 randomized documents with fence edge cases (unterminated fences, invalid attribute lists, missing trailing newline, fences inside blockquotes and lists) underfenced_code,fenced_code+attr_list, andextra.python -m unittest discover tests: 1127 OK; flake8 clean on the changed file.Re-measured on CPython 3.14.7 (7 alternating processes per side, median): the docs concatenated with the extra extensions went from 89.3 ms to 78.7 ms (1.14x).