Repository navigation
perf: deep nesting over an open <p> parsed in quadratic time - #126
Conversation
Syncs Netweir d2796e6 (netweir/netweir#352). - The tree builder keeps whether a <p> is in button scope for each depth of the stack of open elements, so the check every new <div> makes is one lookup instead of a walk down the whole stack. A fragment with 3,500 <div>s stacked over a <p> left open took 13.5 ms; debug builds still walk the stack and assert the answers agree. - The node list is sized for one node per 10 bytes of input instead of 24, so a page dense with elements doesn't grow it twice mid-parse. - XPath: a predicate that can't depend on position filters in place, already in document order, and a set in document order isn't sorted again. Item::Attr now names its attribute by owner and place ({owner, index, ns}) and is 16 bytes instead of 48; Doc::attr_at reads its name and value. The Python and WASM bindings read an attribute item's value through the new shape.
foothillsdigital
left a comment
There was a problem hiding this comment.
Checked b1552e3.
The sync. NETWEIR_COMMIT moves 304eb4a → d2796e6. Of the three commits in between, only #352 touches netweir-html and netweir-xpath, in seven files. Parsec's diff matches netweir's line for line in all seven: dom.rs, tree.rs, and parsec-xpath's eval.rs, index.rs, lib.rs, parser.rs and tests/xpath.rs.
A differential against main, for both changes. I generated 20,000 seeded documents of tag soup heavy in what button scope decides: a p left open, div, button, li, headings, address, table, select and formatting elements. I added 60 deep ones, 100 to 3,000 <div>s over an open <p>, a <p><button> or a <button><p>. Each was parsed with errors on by upstream/main and by this PR, and I recorded:
-
the serialized tree and every error with its offset;
-
the result of 28 XPath queries, chosen to cross the new fast paths:
- positional and non-positional predicates (
[@class][1],[position() < 3][last()],[position() mod 2 = 0],[not(@id)],[p]); - unions taken by position (
(//p | //div)[3]); - attribute sets (
(//div/@*)[2],(//*/@class)[last()]); - axes (
following,preceding-sibling,ancestor); sum,nameandcontains.
Item::Attrchanged shape, so each result is compared as its scalar: a count, string or number. - positional and non-positional predicates (
The two outputs are byte-identical over all 20,060 documents, with no query errors, across 21,670 distinct query and answer pairs.
The rest. cargo clippy --workspace --all-targets -D warnings is clean, and cargo test --workspace passes (320, with the debug-build assertion that the cached button scope agrees with walking the stack, over every html5lib tree test). The Python suite passes against a freshly built wheel (38 passed, 1 skipped), with the new attribute shape. CI is green. Approving.
Problem
Netweir's final boss (netweir/netweir#349) found three costs that Parsec shares:
<p>. Every new<div>walked the whole stack of open elements to ask whether a<p>was in button scope. A fragment with about 3,500<div>s stacked over a<p>left open took 13.5 ms.//x[pred]also grouped, merged, sorted and memoised every match.Change
Syncs Netweir d2796e6, netweir/netweir#352:
u32.Item::Attr: now{owner, index, ns}, 16 bytes, withDoc::attr_atfor its name and value. This changesparsec-xpath's public API. Nothing is released yet, so no deprecation is needed.Tests
cargo test --workspace(release): 319 test groups, 0 failed. html5lib tokenizer 7,042 and tree construction 3,898 pass; the chunked tests show 0 differing.-D warningsis clean for the workspace and both bindings, includingwasm32-unknown-unknown.//div/@classcovers attribute items.wasm-pack test --nodepasses 10, andnode --test tests/package.test.mjspasses 12, with//@id | //@classcovering attribute items.