Skip to content

🐛 fix(tree): end tags in constant time - #2564

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/tree-end-tags-constant-time
Nov 23, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:fix/tree-end-tags-constant-time

Conversation

@gaborbernat

Copy link
Copy Markdown
Member

Since #2417 the open-element stack keeps every element past the 512-level depth cap, and only the DOM stops there. That made each walk the tree builder runs for an end tag cost the full stack depth: any other end tag walked down to the nearest special element, the heading and default scope checks down to the nearest boundary, and the li/dd/dt start tags and foreign end tags walked down as well. The block summaries #2417 added skipped only the 512-entry blocks that held none of a walk's atoms. They keyed on atoms alone, so an HTML <mi> matched the MathML scope boundary of the same name and every block had to be read. A document of stray end tags over unclosed elements, such as </x><i> or </h4><mi> repeated, therefore parsed in quadratic time.

Once the stack passes 64 entries, an index now answers each of these checks in O(1). Shallower stacks keep the plain walk, so ordinary documents never build the index.

  • Every stack entry links to the nearest special element, default scope boundary, list barrier (a special element other than address, div and p) and HTML element at or below it. A push computes the links from the entry below.
  • Every atom keeps the index of its topmost HTML element, and each entry links to the next one with the same atom below it. HTML elements without an atom and foreign elements go in a name table keyed with the per-process hash key, folded to lowercase for foreign names, so a document cannot pick colliding names.
  • The scope checks (default, list item, button, table, heading), any other end tag, the li/dd/dt walks, the foreign end-tag walk and reset-the-insertion-mode then compare the topmost match with the nearest boundary from the top entry's links. Removing an entry from the middle of the stack (adoption agency, </form>) relinks the entries above it, as the block recount did.

The index replaces the block summaries. A differential run of 400k random deep documents and fragments against main produced identical trees, and so did the html5lib tree-construction suite with the index forced on from the first push.

Shape, repeated Repeats Size 1.15.11 1.16.4 This PR
</x><i> 8,000 55 KiB 3.6 ms 30.6 ms 3.3 ms
16,000 109 KiB 7.3 ms 121.8 ms 6.8 ms
32,000 219 KiB 14.8 ms 499.3 ms 13.8 ms
64,000 438 KiB 29.9 ms 2,030.5 ms 27.7 ms
</h4>crossorigin<mi></br>data 4,000 113 KiB 2.7 ms 73.9 ms 1.8 ms
8,000 227 KiB 5.6 ms 292.2 ms 3.7 ms
16,000 453 KiB 11.2 ms 1,162.6 ms 8.2 ms
32,000 906 KiB 22.6 ms 4,635.7 ms 17.2 ms

parse() on CPython 3.14, release build, best of three runs. Six new rows in amplify_regressions.json cover any other end tag (known and unknown names), the heading and default scope checks past an HTML <mi>, the li start tag and the foreign end tag. They start at 128 KiB, past the depth cap, and score linear here and super-linear on main.

Since the open-element stack stopped capping at 512 entries, the walks
that answer an end tag read the whole stack: any other end tag down to
the nearest special element, the heading and default scope checks down
to the nearest boundary, the li, dd and dt walks, and the foreign end
tag walk. The block summaries skipped only blocks without the atoms a
walk reads, and an HTML mi shares its atom with a MathML boundary, so a
document of stray end tags over unclosed elements parsed in quadratic
time.

Once the stack passes 64 entries an index answers each check in O(1).
Every entry links to the nearest special element, scope boundary, list
barrier and HTML element at or below it. Every atom keeps its topmost
HTML element, and a keyed name table does the same for HTML elements
without an atom and for foreign elements, each entry linking to the
next one of its name below it. The index replaces the block summaries.
@codspeed

codspeed Bot commented Nov 23, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 7.27%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 2 improved benchmarks
✅ 578 untouched benchmarks
⏩ 91 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[linkify-traversal-wide-callbacks] 2.3 ms 2.2 ms +8.38%
⚡ test_feature[parse-nul-clean] 2.1 ms 2 ms +6.17%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:fix/tree-end-tags-constant-time (fad6e73) with main (b1a08a2)

Open in CodSpeed

Footnotes

  1. 91 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 36ea662 into tox-dev:main Nov 23, 2026
58 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.

1 participant