perf(xpath): fuse the node test into the attribute traversal (#287)

The attribute axis built an XPath node for every attribute of the element
and then filtered by the step's node test. A predicate like [@name='value']
runs that per matched element, so for a wide document it wrapped many
attributes only to discard all but one, and each wrap retains the owner
element and copies the attribute's qualified name and value.

Apply the same fusion the descendant axes use: test the raw attribute during
the walk and wrap only the ones that pass. matches delegates, for an
attribute node, to a new matchesAttribute (the attribute axis always carries
the attribute principal kind, so the reduction is exact), and the attribute
walk applies it as a keep filter. The node-set, declaration order, and
uniqueness are identical to build-then-filter.

Measured xpath ~20% faster on the 20k-item generated corpus via a same-state
A/B (count unchanged at 10000); xpath is now ~2x libxml2, down from ~13x at
the start of this work. XPathFusedAttributeTests covers every node-test kind
through the fused path, and an adversarial equivalence review found no query
whose result changes. Full suite, Apache Xalan XSLT gold output, and the WASM
build stay green.
This commit is contained in:
Mihaela Mihaljevic 2026-06-25 07:51:29 +02:00 committed by GitHub
parent 52eee325bd
commit 720263ee58
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 96 additions and 3 deletions

View File

@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- The XPath attribute axis fuses the step's node test into the walk, the same way the descendant axes do, so an attribute the test rejects is never wrapped in an `XPath.Node`. A predicate like `[@name='value']` previously built an `XPath.Node` for every attribute of the element (each wrap retains the owner element and copies the attribute's qualified name and value) and then filtered to the one named attribute; it now wraps only the matching attribute. `matches` delegates, for an attribute node, to a new `matchesAttribute` (the attribute axis always has the attribute principal kind, so this is exact), and the attribute walk applies it. The node-set, declaration order, and uniqueness are identical to the build-then-filter path. Measured `xpath` ~20% faster on the 20k-item generated corpus via a same-machine-state A/B. Guarded by `XPathFusedAttributeTests` (name and namespaced-name tests, the principal-kind wildcard, `node()`, the kind tests that never select an attribute, and a predicate over the selection) and confirmed by an adversarial equivalence review.
- The XPath descendant and descendant-or-self axes fuse the step's node test into the traversal, so a node the test rejects is never wrapped in an `XPath.Node` (and so never reference-counted). Previously the axis built the full subtree node list and then filtered it; for a `//name` query over a wide document, most of the wrapped nodes were immediately discarded. The node test now runs against the raw tree node during the walk: `matches(_:_:on:namespaces:)` delegates to a new tree-node `matchesTree` (one source of truth, exercised by the full XPath suite), and the descendant walk applies it. The node-set, document order, and uniqueness are identical to the build-then-filter path; only non-matching nodes are no longer materialized. Measured `xpath` ~11% faster on the 20k-item generated corpus via a same-machine-state A/B. Guarded by `XPathFusedDescendantTests` (every node-test kind through the fused path: element and namespaced names, the principal-kind wildcard, `text()`/`comment()`/`processing-instruction()`/`node()`, and a predicate on the result) and confirmed by an adversarial equivalence review that found no query whose result the change alters.
- The XPath descendant and descendant-or-self axes build their node list through a single shared accumulator instead of concatenating a freshly built array at every node. `descendants(of:)` recursed on wrapped nodes and did `result.append(contentsOf: descendants(of: child))` at each level, allocating and copying an array per node (roughly O(n x depth) work over the subtree); it now recurses on the raw tree node and appends into one `inout` accumulator (O(n)). For a `//`-style query over a wide document this is the dominant cost: measured `xpath` ~46% faster on the 20k-item generated corpus via a same-machine-state A/B, with the identical node-set in the identical order (the change is purely how the array is built).

View File

@ -147,6 +147,21 @@ extension PureXML.XPath {
.map { Node.attribute(owner: tree, $0) }
}
/// The attributes of `tree` that satisfy `keep`, in declaration order. An
/// attribute `keep` rejects is never wrapped in a ``Node``, so it is never
/// retained (each wrap retains the owner and copies the attribute's
/// qualified name and value). This fuses a step's node test into the
/// attribute walk, the common case being `@name` selecting one of several
/// attributes (as in a `[@x='y']` predicate).
static func attributes(of tree: PureXML.Model.TreeNode, where keep: (PureXML.Model.Attribute) -> Bool) -> [Node] {
guard tree.kind == .element else { return [] }
var result: [Node] = []
for attribute in tree.attributes where !isNamespaceDeclaration(attribute) && keep(attribute) {
result.append(.attribute(owner: tree, attribute))
}
return result
}
private static func namespaces(of node: Node) -> [Node] {
guard case let .tree(tree) = node, tree.kind == .element else { return [] }
var bindings: [String: String] = ["xml": xmlNamespaceURI]

View File

@ -54,10 +54,13 @@ extension PureXML.XPath.Evaluator {
/// builds the axis node list and filters it.
private static func matchedAxisNodes(_ step: Step, from contextNode: Node, _ context: EvaluationContext) -> [Node] {
if case let .tree(tree) = contextNode {
let keep: (PureXML.Model.TreeNode) -> Bool = { matchesTree($0, step.test, on: step.axis, context.namespaces) }
switch step.axis {
case .descendant: return PureXML.XPath.AxisNavigation.descendants(of: tree, where: keep)
case .descendantOrSelf: return PureXML.XPath.AxisNavigation.descendantsOrSelf(of: tree, where: keep)
case .descendant:
return PureXML.XPath.AxisNavigation.descendants(of: tree) { matchesTree($0, step.test, on: step.axis, context.namespaces) }
case .descendantOrSelf:
return PureXML.XPath.AxisNavigation.descendantsOrSelf(of: tree) { matchesTree($0, step.test, on: step.axis, context.namespaces) }
case .attribute:
return PureXML.XPath.AxisNavigation.attributes(of: tree) { matchesAttribute($0, step.test, context.namespaces) }
default: break
}
}
@ -113,6 +116,21 @@ extension PureXML.XPath.Evaluator {
}
}
/// The node test applied directly to an attribute, the single-node core of
/// ``matches`` for an `.attribute` node on the attribute axis (whose
/// principal kind is always attribute). Taking the raw ``Attribute`` lets the
/// attribute axis test a candidate without wrapping (and so retaining) it, so
/// an attribute the test rejects is never materialized. A name test matches
/// by qualified name; a wildcard or `node()` matches any attribute; the kind
/// tests never match an attribute.
static func matchesAttribute(_ attribute: PureXML.Model.Attribute, _ test: NodeTest, _ namespaces: [String: String]) -> Bool {
switch test {
case let .name(name): qualifiedMatches(attribute.name, name, namespaces)
case .wildcard, .node: true
case .text, .comment, .processingInstruction: false
}
}
/// The node test applied directly to a tree node, the single-node core of
/// ``matches`` for a `.tree` node. Taking the ``TreeNode`` rather than a
/// wrapped ``Node`` lets the descendant traversal test a candidate without

View File

@ -0,0 +1,57 @@
import Testing
@testable import PureXML
/// The attribute axis over a tree context fuses the node test into the walk
/// (`matchesAttribute`), so an attribute the test rejects is never wrapped.
/// These tests pin that every node-test kind selects exactly what the unfused
/// `matches` path would: a name test (plain and namespaced), the principal-kind
/// wildcard, `node()`, and the kind tests (which never select an attribute).
@Suite("XPath fused attribute node tests")
struct XPathFusedAttributeTests {
private static let source =
#"<r xmlns:k="urn:k"><e a="1" b="2" k:c="3"/><e a="4"/></r>"#
private func values(_ path: String) throws -> [String] {
try PureXML.XPath.Query(path).evaluate(over: PureXML.parse(Self.source)).compactMap {
if case let .attribute(attribute) = $0 { return attribute.value }
return nil
}
}
private func count(_ path: String) throws -> Int {
try Int(PureXML.XPath.Query("count(\(path))").value(at: PureXML.parseTree(Self.source)).number)
}
@Test("a name test selects the matching attribute only")
func test_nameTest() throws {
#expect(try values("//e/@a") == ["1", "4"])
#expect(try values("//e/@b") == ["2"])
#expect(try values("//e/@missing") == [])
}
@Test("a namespaced attribute name test resolves the prefix")
func test_namespacedName() throws {
#expect(try values("//e/@k:c") == ["3"])
}
@Test("the wildcard selects every attribute (excluding namespace declarations)")
func test_wildcard() throws {
// First e has a, b, k:c (3); second has a (1). xmlns:k is not an attribute node.
#expect(try count("//e/@*") == 4)
#expect(try count("/r/@*") == 0) // r has only the xmlns:k declaration
}
@Test("kind tests never select an attribute; node() does")
func test_kindAndNode() throws {
#expect(try count("//e/attribute::text()") == 0)
#expect(try count("//e/attribute::comment()") == 0)
// attribute::node() selects every attribute.
#expect(try count("//e/attribute::node()") == 4)
}
@Test("a predicate over a fused attribute selection still filters")
func test_predicate() throws {
#expect(try values("//e[@a='4']/@a") == ["4"])
#expect(try count("//e[@b]") == 1) // only the first e has a b attribute
}
}

View File

@ -66,6 +66,7 @@ offsets throughout; libxml2's xmlReadMemory takes an int byte count).
| 12 | skip the document-order sort for a single forward-axis step from one context: `AxisNavigation` produces child/descendant/descendant-or-self/self/following/following-sibling already in document order with no duplicates, so the sort is redundant (adversarially reviewed for order correctness; same-state A/B, ~7%) | unchanged | 0.055 s (~6x) | unchanged |
| 13 | single-accumulator descendant traversal: `descendants(of:)` recursed on wrapped nodes and concatenated a fresh array at every node (`append(contentsOf: descendants(of: child))`), O(n x depth) allocation churn; rewritten to recurse on the raw tree node with one shared `inout` accumulator, O(n). Same output (same-state A/B, ~46%) | unchanged | 0.032 s (~3x, from ~6x) | unchanged |
| 14 | fuse the node test into the descendant walk: a node the step's test rejects is no longer wrapped in an `XPath.Node` (so never retained); `matches` delegates to a tree-node `matchesTree` so there is one source of truth, and the traversal applies it during the walk. Identical node-set (adversarially equivalence-reviewed; same-state A/B, ~11%) | unchanged | 0.028 s (~2.5x) | unchanged |
| 15 | the same test fusion for the attribute axis (`matchesAttribute`): a `[@name='...']` predicate built every attribute node per element and filtered to one; it now wraps only the matching attribute, saving the owner retain and the qualified-name/value copies for the rest. Identical node-set (adversarially equivalence-reviewed; same-state A/B, ~20%) | unchanged | 0.022 s (~2.0x) | unchanged |
## Profile findings (first sample, parse path)