Skip to content

perf: speed up _encode_amps_and_angles - #784

Open
g4lb wants to merge 1 commit into
trentm:masterfrom
g4lb:perf/encode-amps-and-angles
Open

g4lb wants to merge 1 commit into
trentm:masterfrom
g4lb:perf/encode-amps-and-angles

Conversation

@g4lb

@g4lb g4lb commented Nov 21, 2026

Copy link
Copy Markdown

_encode_amps_and_angles runs four regexes on each span of text. This change skips each regex when the text does not contain its character (&, < or >). The naked > regex now starts with the >, so the regex engine does not try each position in the text. The output does not change.

Time of markdown2.markdown(), median of 20 interleaved rounds. Before and After are CPython 3.14:

Document Before After CPython 3.14 CPython 3.10
README.md of this repo 2.15 ms 1.97 ms -8.6% -5.0%
CHANGES.md of this repo 27.0 ms 25.2 ms -6.6% -2.7%
Python-Markdown syntax.md 9.66 ms 8.93 ms -7.5% -4.8%
awesome-python README.md 91.1 ms 85.1 ms -6.6% -4.7%
public-apis tables (tables extra) 29.1 ms 25.2 ms -13.2% -9.7%
Rust book chapter (10 extras) 13.2 ms 12.2 ms -7.6% -5.4%
60 Python Cookbook recipes (perf/recipes.pprint) 10.6 ms 9.11 ms -13.7% -12.0%
80 files from test/tm-cases 36.5 ms 33.7 ms -7.5% -8.5%
300 plain paragraphs 11.0 ms 8.72 ms -20.8% -20.9%

Text where every span has &, < and > is not slower. On PyPy 3.11, the results were in the noise. Import time did not change. I used a Mann-Whitney test (Apple M5). I did not measure other architectures.

Checks:

  • make testone and make testredos pass on CPython 3.10, 3.14 and PyPy 3.11.
  • Old and new outputs matched on 1,000,000 random documents.

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