Skip to content

Avoid quadratic time for duplicate footnote references - #1726

Merged
waylan merged 1 commit into
Python-Markdown:masterfrom
ramykamil:footnotes-quadratic-refs
Nov 24, 2026
Merged

waylan merged 1 commit into
Python-Markdown:masterfrom
ramykamil:footnotes-quadratic-refs

Conversation

@ramykamil

Copy link
Copy Markdown
Contributor

Description

Each duplicate reference to a footnote restarted the search for a unique id at the first one (fnref:a, then fnref2:a, ...), so N references to the same footnote took O(N²) time: "[^a]" * 4000 (16 KB) takes 6.7 s to convert. This resumes from the last id issued for that footnote. Earlier ids are all still in used_refs, so the generated ids are unchanged. With the change the same input takes 0.17 s.

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: Claude (Anthropic) found the slowdown by timing inputs of increasing size, and compared old and new output on randomly generated documents.

Checklist

Each duplicate reference to a footnote restarted the search for a unique
id at the first one, so N references to one footnote took O(N^2) time.
Resume from the last id issued for that footnote instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@waylan
waylan merged commit 4533285 into Python-Markdown:master Nov 24, 2026
14 checks passed
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.

2 participants