Repository navigation
🐛 fix(tree): end tags in constant time - #2564
Conversation
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.
Merging this PR will improve performance by 7.27%2 warnings
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes
|
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/dtstart 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.
address,divandp) and HTML element at or below it. A push computes the links from the entry below.li/dd/dtwalks, 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
mainproduced identical trees, and so did the html5lib tree-construction suite with the index forced on from the first push.</x><i></h4>crossorigin<mi></br>dataparse()on CPython 3.14, release build, best of three runs. Six new rows inamplify_regressions.jsoncover any other end tag (known and unknown names), the heading and default scope checks past an HTML<mi>, thelistart tag and the foreign end tag. They start at 128 KiB, past the depth cap, and score linear here and super-linear onmain.