Skip to content

Fix quadratic rendering time for text with many links - #1735

Open
naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:link-bracket-index-rebuild
Open

naaz234 wants to merge 1 commit into
Python-Markdown:masterfrom
naaz234:link-bracket-index-rebuild

Conversation

@naaz234

@naaz234 naaz234 commented Dec 6, 2026

Copy link
Copy Markdown
Contributor

Description

LinkInlineProcessor.getText (used by the link, image and reference processors) resolves the closing ] from a table that _findBracketEnds builds 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 for markdown.markdown(s * n) on CPython 3.11, as 3.11.3 -> 3.11.4 -> this branch:

'[a](/b)'   x2000  (14000 bytes):  0.04s ->  8.53s -> 0.05s
'[a](/b)'   x4000  (28000 bytes):  0.10s -> 45.22s -> 0.11s
'![a](/b)'  x4000  (32000 bytes):  0.07s -> 43.70s -> 0.08s
'[a][1]'    x4000  (24000 bytes):  0.09s -> 42.60s -> 0.11s
'[a]'       x10000 (30000 bytes):  0.12s -> 67.23s -> 0.15s

The last two have a matching [1]: /u or [a]: /u definition 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:

'[[' x10000 + '[Text](http://link.com)': 79.07s ->  0.06s -> 0.05s
'[' x10000 + ']':                         17.93s ->  0.03s -> 0.02s
'[[a](b)' x2000:                           6.73s -> 12.01s -> 1.78s

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 getText with the pre-3.11.4 forward scan on every bracket index of all 1,398,101 strings up to length 10 over [, ], a and x, 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 to master with no extensions, with extra, and with footnotes, attr_list, toc and smarty. test_many_links takes 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:

    • Code
    • Code Comments
    • Documentation
    • Changelog Entry
    • Commit Message
    • The Description Above
    • Other: tests, and the timing and differential scripts used to find and check this

Checklist

`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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant