Skip to content

👌 Make StateBase.src a plain attribute (~6% faster parsing) - #450

Open
deadstrobe5 wants to merge 3 commits into
executablebooks:masterfrom
deadstrobe5:perf/state-src-plain-attribute
Open

deadstrobe5 wants to merge 3 commits into
executablebooks:masterfrom
deadstrobe5:perf/state-src-plain-attribute

Conversation

@deadstrobe5

@deadstrobe5 deadstrobe5 commented Oct 16, 2026 •

Copy link
Copy Markdown

StateBase.src was a property whose setter only reset the cache of the deprecated srcCharCode. Rules read state.src constantly (~120k reads per render of benchmarking/samples/spec.md), so the property call alone was a measurable share of parse time.

This makes src a plain attribute. The srcCharCode cache is now a class-level (src, codes) pair, rebuilt whenever src is not the string it was built from. It is class-level because StateBlock, StateInline and StateCore don't call StateBase.__init__.

Benchmark

pytest benchmarking/bench_core.py, Apple M5, Python 3.11, 3 runs each side:

Before After
median 62.0–63.4 ms 57.7–60.7 ms
min 58.8–60.1 ms 55.1–55.9 ms

Interleaved A/B, 15 processes per side: median 61.1 → 57.0 ms (-6.7%), faster in 14 of 15 pairs.

Safety

  • Output is byte-identical (HTML and token dicts) across all test fixtures and benchmark samples for the commonmark, default, gfm-like, zero and js-default presets.
  • Full test suite passes (1033), plus the mdit-py-plugins suite (511). ruff and mypy --strict at the pre-commit pinned versions are clean.
  • Added a test that srcCharCode follows reassignment of src.

Independent of #449 (different file); the two should stack.

Thanks for maintaining markdown-it-py!

Re-measured on CPython 3.14.7 (7 alternating processes per side, median): rendering benchmarking/samples/spec.md went from 26.6 ms to 25.3 ms (about 5%).

This branch has not been deployed

No deployments
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