Skip to content

Get the sorted inline patterns once per document - #1723

Open
g4lb wants to merge 1 commit into
Python-Markdown:masterfrom
g4lb:inline-pattern-snapshot
Open

g4lb wants to merge 1 commit into
Python-Markdown:masterfrom
g4lb:inline-pattern-snapshot

Conversation

@g4lb

@g4lb g4lb commented Nov 20, 2026

Copy link
Copy Markdown

Description

InlineProcessor.__handleInline gets each inline pattern with an integer index into the Registry. Each index checks the sort order and the key type, and finds the item by name. This change gets the sorted list of patterns one time in InlineProcessor.run. Then __handleInline uses an index into that list. The output does not change.

markdown.markdown() input Before After Change
awesome-python README.md (85 kB) 25.08 ms 22.80 ms -9.1%
requests HISTORY.md (65 kB) 17.62 ms 16.26 ms -7.8%
docs/changelog.md 10.12 ms 9.52 ms -5.9%
docs/contributing.md, extra + toc 8.25 ms 7.86 ms -4.8%
long document, extra + toc + smarty 42.85 ms 36.41 ms -15.0%
README.md 0.50 ms 0.47 ms -6.1%
Hello *world* paragraph 85.9 µs 83.4 µs -2.9%

I measured no slowdown on 23 inputs. These include a single word, documents with only code blocks, and documents with only headings. I used 20 interleaved rounds and a Mann-Whitney test (Apple M5, CPython 3.14). CPython 3.11 and 3.15 gave the same results. On PyPy 3.11, the change was in the noise range of old against old. Import time did not change (30 rounds). I did not measure other architectures.

Checks:

  • python -m unittest discover tests passes on CPython 3.11, 3.14, 3.15 and PyPy 3.11, and with Pygments 2.7.1.
  • flake8 markdown tests passes.
  • Old and new outputs matched on 1,000,000 random documents and on all documents in docs/ and tests/. I used 10 sets of extensions and both output formats.

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: the benchmarks and the output comparison test, which are not in this PR. The tool is Claude Code.

Checklist

InlineProcessor.__handleInline got each pattern with an integer index
into the Registry. Each index checks the sort order and the key type,
and looks up the item by name. InlineProcessor.run now gets the sorted
list once, and __handleInline indexes that list.
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