diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cf1b56..55c069a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/Sources/XPath/AxisNavigation.swift b/Sources/XPath/AxisNavigation.swift index d0ebfee..56d1ef9 100644 --- a/Sources/XPath/AxisNavigation.swift +++ b/Sources/XPath/AxisNavigation.swift @@ -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 diff --git a/docs/benchmarks.md b/docs/benchmarks.md index d893b84..12223c7 100644 --- a/docs/benchmarks.md +++ b/docs/benchmarks.md @@ -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)