diff --git a/CHANGELOG.md b/CHANGELOG.md index 045a86d..566ca16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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). diff --git a/Sources/XPath/AxisNavigation.swift b/Sources/XPath/AxisNavigation.swift index 02768e3..69c77d7 100644 --- a/Sources/XPath/AxisNavigation.swift +++ b/Sources/XPath/AxisNavigation.swift @@ -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] diff --git a/Sources/XPath/EvaluatorSteps.swift b/Sources/XPath/EvaluatorSteps.swift index ebe5807..796ae96 100644 --- a/Sources/XPath/EvaluatorSteps.swift +++ b/Sources/XPath/EvaluatorSteps.swift @@ -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 diff --git a/Tests/XPathFusedAttributeTests.swift b/Tests/XPathFusedAttributeTests.swift new file mode 100644 index 0000000..f6e2c15 --- /dev/null +++ b/Tests/XPathFusedAttributeTests.swift @@ -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 = + #""# + + 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 + } +} diff --git a/docs/benchmarks.md b/docs/benchmarks.md index 7b315a8..b650934 100644 --- a/docs/benchmarks.md +++ b/docs/benchmarks.md @@ -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)