You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
perf: streamline HTML serializer and cache inline pattern metadata per run - #1730
Profiling markdown.Markdown.convert on multi-section documents (tables, fenced code, TOC, inline formatting) pointed to two avoidable hot-loop costs:
markdown/serializers.py (_serialize_html): used a deque as a LIFO stack (popleft/appendleft/extendleft), maintained an unused count variable, called tag.lower() three times per element, constructed ["script", "style"] on each element, called el.items() + sorted() even when an element had no attributes, and pushed closing tags and tails onto the stack for childless leaf elements. Switching to a plain list stack, hoisting tag.lower() and _RAW_TEXT_TAGS, fast-pathing empty/single attribute dicts, and emitting leaf elements directly removes those allocations and extra stack passes.
markdown/treeprocessors.py (InlineProcessor): popped from the front of a list (stack.pop(0)) instead of a deque (popleft()), and re-ran Registry.__getitem__, isinstance(pattern, InlineProcessor), pattern.getCompiledRegExp(), and exclude.lower() inside __applyPattern on every text node. Snapshotting the pattern tuple once at the start of run() (and clearing it alongside parent_map / stashed_nodes on exit) avoids repeated lookups while preserving the .finditer() call path for custom patterns.
Benchmark on a 30x repeated mixed document (tables, fenced_code, toc extensions, 8 convert() runs, 20 interleaved A/B pairs with exact stdout SHA-256 verification via oa):
@waylan, to be honest, the removal of deque matters little; keeping the deque is a matter of style in this specific case, not a bug. In short, it is perfectly appropriate to keep it, but it hurts nothing to use a plain list in the serializer either. With that said, the serializer rewrite is safe but saves little. The serializer is only about 3% of total conversion time. The rewrite trims about 17% of that, and the gain comes from writing childless elements directly and calling tag.lower() once. The deque swap contributes nothing.
The usage of deque in the treeprocessor is where we actually get some real gains. Generally, the treeprocessor changes are the part that is really worth having. Caching pattern metadata per run and replacing stack.pop(0) with deque.popleft() both help.
Thanks @facelessuser, that breakdown makes a lot of sense. I reverted the markdown/serializers.py changes in b80fac6 so #1644 stays untouched and the PR stays focused on InlineProcessor in markdown/treeprocessors.py (pattern metadata snapshot per run() and deque.popleft()). Re-ran the suite (1,230 tests pass) and checked master vs the narrowed diff (184.2 -> 167.4 ms, 1.10x, -2.6% peak RSS, identical output SHA-256).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
Profiling
markdown.Markdown.converton multi-section documents (tables, fenced code, TOC, inline formatting) pointed to two avoidable hot-loop costs:markdown/serializers.py(_serialize_html): used adequeas a LIFO stack (popleft/appendleft/extendleft), maintained an unusedcountvariable, calledtag.lower()three times per element, constructed["script", "style"]on each element, calledel.items()+sorted()even when an element had no attributes, and pushed closing tags and tails onto the stack for childless leaf elements. Switching to a plainliststack, hoistingtag.lower()and_RAW_TEXT_TAGS, fast-pathing empty/single attribute dicts, and emitting leaf elements directly removes those allocations and extra stack passes.markdown/treeprocessors.py(InlineProcessor): popped from the front of alist(stack.pop(0)) instead of adeque(popleft()), and re-ranRegistry.__getitem__,isinstance(pattern, InlineProcessor),pattern.getCompiledRegExp(), andexclude.lower()inside__applyPatternon every text node. Snapshotting the pattern tuple once at the start ofrun()(and clearing it alongsideparent_map/stashed_nodeson exit) avoids repeated lookups while preserving the.finditer()call path for custom patterns.Benchmark on a 30x repeated mixed document (
tables,fenced_code,tocextensions, 8convert()runs, 20 interleaved A/B pairs with exact stdout SHA-256 verification via oa):master(27c01419)d2f546dea9d57569p = 0.0000Verification:
python -m unittest discover testsran 1,230 tests (OK, 111 skipped).AI Assistance Disclosure
AI tools were use to assist with the following (Antigravity coding assistant):
Checklist