Repository navigation
Conversation
`LinkInlineProcessor.getText` scanned forward from each `[` to find its matching `]`. The inline treeprocessor calls it for every `[` in the text, so when the brackets never close each call scanned to the end of the text and conversion became quadratic in the number of brackets. Find the closing bracket for every index in a single pass and cache the result for the current text. Matches and returned text are unchanged.
waylan
reviewed
Oct 19, 2026
| ) | ||
|
|
||
| def test_many_unclosed_square_brackets(self): | ||
| # Each unclosed bracket used to rescan the rest of the text. |
Member
There was a problem hiding this comment.
The spell checker seems to not like the word "rescan." It should probably be added to .spell-dict.
Member
|
I am going to let @facelessuser review this as he is more familiar with the affected code. |
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
LinkInlineProcessor.getText(used by the link, image and reference processors) scans forward from a[until the brackets balance. The treeprocessor calls it for every[in the text, and when the brackets never close each call scans to the end, so conversion time grows with the square of the number of unclosed brackets. Timings formarkdown.markdown(s * n)on CPython 3.11:This now finds the closing bracket for every index in one pass and caches the result for the current text, so repeated calls on the same text are a list lookup. I compared the new
getTextwith the old loop on every index of about 30,000 random strings made of[,],aand((about 400,000 calls) and the results were identical. The new tests pass with and without the change.test_many_unclosed_square_bracketstakes about 27 seconds without the fix and finishes instantly with it.getLinkhas a similar rescan for unclosed(but its quote/title handling is more involved, so I left it out of this change.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:
Tool used: Claude Code (Claude Opus 5.5). wrote the patch, tests, changelog entry, commit message and this description. I reviewed and checked all of it before submitting.
Checklist