Repository navigation
Conversation
`LinkInlineProcessor.getText` resolved the closing square bracket from a table that `_findBracketEnds` built for every index of the text, cached against the identity of that text. The inline treeprocessor replaces each match with a placeholder and passes on a newly built string, so the cache missed once per match and the whole text was indexed again for every link, image or reference in it. Scanning to the closing bracket costs no more than the length of the link text, so only index the brackets that follow once a scan has run to the end of the text without finding a close. That is the case the table was added for in Python-Markdown#1664, and the text is not rebuilt while no match is found, so one index still serves every later bracket there. Returned text and matches are unchanged.
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) resolves the closing]from a table that_findBracketEndsbuilds for every index of the text, cached against the identity of the text it was built from. The inline treeprocessor replaces each match with a placeholder and hands the next call a newly built string, so the cache misses once per match and the whole text is indexed again for every link in it. Conversion now grows with the square of the number of links, which is new in 3.11.4. Timings formarkdown.markdown(s * n)on CPython 3.11, as 3.11.3 -> 3.11.4 -> this branch:The last two have a matching
[1]: /uor[a]: /udefinition appended. No crafted input is needed for this one: a document with a few thousand ordinary links is enough.Scanning to the closing bracket costs no more than the length of the link text, so this only indexes the brackets after the opening one once a scan has run to the end of the text without finding a close. That is the case the table was added for in #1664, and the text is not rebuilt while no match is found, so one index still serves every later bracket there:
An unclosed
[ahead of each link, as in that last line, still costs one scan per link, so that shape stays quadratic. It is faster here than in either release, but I have not fixed it.I compared the new
getTextwith the pre-3.11.4 forward scan on every bracket index of all 1,398,101 strings up to length 10 over[,],aandx, and of 20,000 random strings, in both increasing and decreasing index order and with the instance shared across texts: 7.5M calls, every result identical. Rendering 90,000 random documents gave output identical tomasterwith no extensions, withextra, and withfootnotes,attr_list,tocandsmarty.test_many_linkstakes about 0.6s with the change and several minutes without it.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