Skip to content

fix(xpath): quadratic attribute name tests, and XPath over any tree - #197

Open
jamesconwaydev wants to merge 3 commits into
mainfrom
sync-366
Open

jamesconwaydev wants to merge 3 commits into
mainfrom
sync-366

Conversation

@jamesconwaydev

@jamesconwaydev jamesconwaydev commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Syncs Netweir 0196e9e: netweir/netweir#366 and netweir/netweir#369. Part of #171.

Problem.

  • XPath read an attribute's name and value through Doc::attr_at, which called tree.attrs(owner).nth(index) and walked every attribute before the one it wanted. A name test does that once per attribute, so /a/@same on one element with 20,001 attributes took 0.24 s at 5,000, 0.97 s at 10,000 and 3.89 s at 20,000. A page built to stall a crawler's XPath could use it.
  • parsec-xpath could only query parsec-html-core's Document, so parsec-xml (feat(xml): Parsec had no XML parser, and XML can't go through HTML's #198) had no XPath.

Change.

  • Document::attr_at and Document::attr_count read attributes in constant time. /a/@same at 20,000 now takes 4.2 ms.
  • parsec-xpath reads trees through a Tree trait (src/tree.rs). The impl for parsec::Document, and XmlTree for an XML document built into the HTML tree, sit behind a default html feature (src/html.rs). With default-features = false, the crate depends on stacker alone.
  • The manifest is Parsec's own, so this PR adds what the sync can't: the html feature, parsec-html-core made optional, and required-features on the xpath and libxml2 tests.
  • CI's lint job runs clippy and the tests for parsec-xpath --no-default-features.
  • Breaking, so this is 0.3.0, not 0.2.1: Doc::xml and Namespaces::id_of are gone and Item::Attr lost its ns field. Parsec's own uses (the C API, Python and WebAssembly) already matched Item::Attr { owner, index, .. } and compile unchanged. In XML, local-name() of a processing instruction is now its whole target, as libxml2 has it. CHANGELOG has both.

Tests. tests/tree.rs queries a tree written by hand, covering names, namespaces, attributes (xmlns left out), text, processing instructions, id() and lang(), so the trait is proven apart from the HTML tree. Netweir's many_declarations_and_prefixed_attributes_stay_linear fails without the attr_at fix. Here: the drift check against 0196e9e passes; cargo test for parsec-xpath (with and without default features), parsec-html-core, parsec-html and parsec-capi passes; workspace clippy, fmt and the header check are clean.

@jamesconwaydev jamesconwaydev added this to the 0.2.1 milestone Nov 21, 2026
@jamesconwaydev jamesconwaydev added the xpath XPath and XML label Nov 21, 2026
@jamesconwaydev jamesconwaydev modified the milestones: 0.2.1, 0.3.0 Nov 22, 2026
@jamesconwaydev jamesconwaydev changed the title fix(xpath): an attribute name test took quadratic time on wide elements fix(xpath): quadratic attribute name tests, and XPath over any tree Nov 22, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

xpath XPath and XML

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant