Skip to content

perf: deep nesting over an open <p> parsed in quadratic time - #126

Merged
jamesconwaydev merged 1 commit into
mainfrom
sync-deep-p-scope
Nov 20, 2026
Merged

jamesconwaydev merged 1 commit into
mainfrom
sync-deep-p-scope

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

Netweir's final boss (netweir/netweir#349) found three costs that Parsec shares:

  • Deep nesting over an open <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.
  • The node list growing mid-parse. It was sized for one node per 24 bytes. A 27.6 MB table has one per 12, so the list grew twice, holding the old and new copies at once each time.
  • XPath results over large node-sets. Each item was 48 bytes. //x[pred] also grouped, merged, sorted and memoised every match.

Change

Syncs Netweir d2796e6, netweir/netweir#352:

  • Button scope: the tree builder keeps the answer for each depth of the stack, so the check is one lookup. Debug builds still walk the stack and assert the two answers agree, so every html5lib tree test checks it.
  • Node list: sized for one node per 10 bytes. Space a page doesn't use is never touched, so it costs address space, not memory.
  • XPath:
    • A predicate that can't depend on position filters in place, already in document order.
    • A set already in document order isn't sorted again.
    • The index stores tag ids as u32.
  • Item::Attr: now {owner, index, ns}, 16 bytes, with Doc::attr_at for its name and value. This changes parsec-xpath's public API. Nothing is released yet, so no deprecation is needed.
  • Bindings: the Python and WASM bindings read an attribute item's value through the new shape.

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.
  • Clippy -D warnings is clean for the workspace and both bindings, including wasm32-unknown-unknown.
  • Python binding: 38 passed, 1 skipped (pytest on a fresh wheel). //div/@class covers attribute items.
  • WASM: wasm-pack test --node passes 10, and node --test tests/package.test.mjs passes 12, with //@id | //@class covering attribute items.

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 foothillsdigital left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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, name and contains.

    Item::Attr changed shape, so each result is compared as its scalar: a count, string or number.

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.

@jamesconwaydev
jamesconwaydev merged commit 8c7b9cf into main Nov 20, 2026
33 checks passed
@jamesconwaydev
jamesconwaydev deleted the sync-deep-p-scope branch November 20, 2026 10:19
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.

2 participants