Repository navigation
Conversation
`LinkInlineProcessor.getText` rescanned the whole remaining document on every call to find the matching `]`, while the inline patterns call it once per candidate position. Documents with many unclosed `[` therefore parsed in O(n^2): the input profiled in Python-Markdown#798 (4000 unclosed brackets) took ~1.8 s to parse, and time grows with the square of the length. Precompute, in one linear right-to-left pass, the end index of the bracket pair starting at each position, and cache the result per input string. Each `getText` call is then a single dict lookup. Output is unchanged for every input: verified against the old implementation with a brute-force differential over ~1.4M (string, index) pairs, plus the full suite (807 passed, 110 skipped, and 343 for md_in_html run separately). Fixes Python-Markdown#798 Assisted-by: ZCode (AI coding agent) Signed-off-by: simpleqt <2015900818@qq.com>
| RE_LINK = re.compile(r'''\(\s*(?:(<[^<>]*>)\s*(?:('[^']*'|"[^"]*")\s*)?\))?''', re.DOTALL | re.UNICODE) | ||
| RE_TITLE_CLEAN = re.compile(r'\s') | ||
|
|
||
| # Cache of the bracket pairing computed for the last `data` string seen by |
Collaborator
There was a problem hiding this comment.
Why is this shared across all instances, even across different Markdown instances?
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.
Problem
LinkInlineProcessor.getTextrescanned the entire remaining document on every call to find the matching](counting nested brackets), while the inline pattern machinery calls it once per candidate position. Documents with many unclosed[therefore parse in O(n^2):'[' * 1000'[' * 2000'[' * 4000This is the case @andersk confirmed on 2026-09-16 as still quadratic after #1631 closed a different case (that one, like the merged #1621, addressed other quadratic paths).
Fix
Compute the end index of the bracket pair starting at each position in one linear right-to-left pass (maintaining the "nearest position with balance b-1" map — an open bracket at
imatches the first later position whose running balance drops belowbalance[i]), and cache the resulting dict on the processor keyed by identity of the input string.getTextbecomes a single dict lookup.This follows the approach @facelessuser sketched in the issue ("probably not a bad idea to cache some stuff").
Verification
getTextover ~1.4M(string, index)pairs (random strings from[]ab\up to 12 chars, every start index) — every result identical.tests/test_syntax/extensions/test_md_in_html.pyrun separately (pre-existingTestSuitecollection quirk): 343 passed.TestUnclosedBracketPerformanceintests/test_syntax/inline/test_links.py): fail on master (the 20,000-bracket case takes ~50 s there vs a generous 5 s bound; that path renders wrong), pass in 0.15 s with the fix.flake8clean on both touched files (max-line-length = 119).AI assistance disclosure: this patch was produced by an AI coding agent (ZCode) operating under the account owner's direction and review, per the project's AI-assistance policy.
Closes #798