perf(xpath): build the descendant axis through a single accumulator (#285)

`descendants(of:)` recursed on wrapped XPath nodes and, at every node, built
a fresh array for that node's subtree and concatenated it
(`result.append(contentsOf: descendants(of: child))`). That is an array
allocation and copy per node, roughly O(n x depth) work over the subtree, and
the parse profile showed it dominating XPath evaluation for a `//`-style query
over a wide document.

Recurse on the raw tree node instead, appending into one shared inout
accumulator. The result is the identical node-set in the identical document
order; only the way the array is built changes, from O(n x depth) to O(n).

Measured xpath ~46% faster on the 20k-item generated corpus via a same-state
A/B (count unchanged at 10000). Full suite, the Apache Xalan XSLT gold-output
corpus, and the WASM build stay green.
This commit is contained in:
Mihaela Mihaljevic 2026-06-25 06:30:23 +02:00 committed by GitHub
parent f561ddbcef
commit cf8246a1cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 26 additions and 5 deletions

View File

@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Changed
- 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).
## [0.4.0] - 2026-06-25
### Fixed

View File

@ -15,7 +15,7 @@ extension PureXML.XPath {
switch axis {
case .child: children(of: context)
case .descendant: descendants(of: context)
case .descendantOrSelf: [context] + descendants(of: context)
case .descendantOrSelf: descendantsOrSelf(of: context)
case .parent: context.parent.map { [$0] } ?? []
case .ancestor: ancestors(of: context)
case .ancestorOrSelf: [context] + ancestors(of: context)
@ -43,14 +43,30 @@ extension PureXML.XPath {
}
static func descendants(of node: Node) -> [Node] {
guard case let .tree(tree) = node else { return [] }
var result: [Node] = []
for child in children(of: node) {
result.append(child)
result.append(contentsOf: descendants(of: child))
}
appendDescendants(of: tree, into: &result)
return result
}
static func descendantsOrSelf(of node: Node) -> [Node] {
guard case let .tree(tree) = node else { return [node] }
var result: [Node] = [node]
appendDescendants(of: tree, into: &result)
return result
}
/// Appends every descendant of `tree` in document order through a single
/// shared accumulator. Recursing on the raw ``TreeNode`` rather than on a
/// wrapped ``Node`` means the traversal builds no intermediate per-level
/// arrays and wraps each node exactly once, when it is appended.
private static func appendDescendants(of tree: PureXML.Model.TreeNode, into result: inout [Node]) {
for child in tree.children {
result.append(.tree(child))
appendDescendants(of: child, into: &result)
}
}
static func ancestors(of node: Node) -> [Node] {
var result: [Node] = []
var current = node.parent

View File

@ -64,6 +64,7 @@ offsets throughout; libxml2's xmlReadMemory takes an int byte count).
| 10 | `//` descendant fusion (`descendant-or-self::node()/child::X[P]` compiled to `descendant::X[P]` when `P` is non-positional), avoiding the whole-subtree intermediate context; guarded so positional predicates keep their per-parent meaning, adversarially reviewed for semantic transparency | unchanged | 0.063 s (~7x, from 10x) | unchanged |
| 11 | drop the redundant `Set` de-duplication when sorting a path result: `evaluateSteps` already yields a duplicate-free node-set, so only the document-order sort is needed (each skipped insert was a copy of a node wrapping a side-table-refcounted tree node); same-state A/B, ~9% | unchanged | 0.058 s (~6x, from ~7x) | unchanged |
| 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 |
## Profile findings (first sample, parse path)