From 4bd547d4b18a253f4fac4812d11a03e6016954bf Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Thu, 25 Jun 2026 04:18:50 +0200 Subject: [PATCH] perf(xpath): drop redundant node-set de-duplication when sorting a path (#282) A path expression sorted its result into document order through orderUnique, which first ran a Set-based de-duplication. But evaluateSteps already returns a duplicate-free node-set: each step de-duplicates across its context nodes, or uses an axis whose per-context results cannot overlap. So that Set copied every node for nothing, and each copy retains the wrapped tree node, whose weak parent reference forces a side table and the slow reference-counting path. Path and trailing-path evaluation now sort the steps' result directly. Unions and filter primaries, which genuinely can carry duplicates, keep the de-duplicating pass. Measured xpath ~9% faster on the 20k-item generated corpus via a same-state A/B, identical results. The existing node-set de-duplication and union tests cover that a path still yields each node once. --- CHANGELOG.md | 2 ++ Sources/XPath/Evaluator.swift | 11 +++++++++-- docs/benchmarks.md | 1 + 3 files changed, 12 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 117c8de..afd1b6f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +- A path expression's result is no longer re-de-duplicated before sorting. `evaluateSteps` already returns a duplicate-free node-set (each step de-duplicates across its context nodes, or uses an axis whose per-context results cannot overlap), so the `Set`-based de-duplication that ran before the document-order sort copied every node for nothing; each copy retains the wrapped tree node, whose `weak parent` reference forces a side table and the slow reference-counting path. Path and trailing-path evaluation now sort the steps' result directly; unions and filter primaries (which genuinely can carry duplicates) keep the de-duplicating pass. Measured `xpath` ~9% faster on the 20k-item generated corpus via a same-machine-state A/B, identical results. Covered by the existing node-set de-duplication and union tests (a path still yields each node once). + - XPath compiles `//` (the abbreviation for `descendant-or-self::node()/child::X`) to a single `descendant::X` step when the child step's predicates are non-positional, avoiding the materialization of every node in the subtree as an intermediate context (measured `xpath` ~10x -> ~7x slower than libxml2 on the 20k-item generated corpus via a same-machine-state A/B, identical results; ~54% faster than before this cycle's two XPath changes combined). The two node-sets are equal, so the fusion is transparent, EXCEPT for a positional predicate: `//x[1]` is the first `x` child of EACH parent, not the first `x` in document order, which `descendant::x[1]` would wrongly select. The rewrite is therefore applied only when every predicate is provably non-positional: its value is a boolean, node-set, or string (never a number, which XPath reads as `position() = value`) and it nowhere references `position()` or `last()`. Anything not provably both is left unfused, so a result can never change. Guarded by `XPathDescendantFusionTests` (positional forms keep their per-parent meaning, non-positional forms select across the subtree, composition with trailing and nested steps) and confirmed by an adversarial differential review (fused versus a spec-literal unfused reference) that found no query whose result the fusion changes. - XPath location-step evaluation skips cross-context node-set de-duplication when no duplicate can arise (measured `xpath` ~13x -> ~10x slower than libxml2 on the 20k-item generated corpus via a same-machine-state A/B, ~28% faster, identical results). A step accumulated each context node's axis results into a `Set` to drop nodes reached from more than one context, which hashed and copied every node (the wrapped `TreeNode` reference is reference-counted, so each copy is ARC traffic). De-duplication is needed only when two distinct context nodes can reach the same node, so it is now skipped on a single context and on the axes whose results are disjoint per context (`child`, `attribute`, `namespace`, `self`); those produce the identical node sequence the `Set` would (every insertion succeeds) without the hashing. The `Set` is kept on the axes that genuinely overlap (`descendant`, `ancestor`, `parent`, the siblings, `following`/`preceding`). Predicates are applied per context (unchanged) and the path result still passes through the final order-and-unique pass, so semantics are preserved. Guarded by `XPathNodeSetDedupTests`, which checks that a node shared by many contexts through `ancestor`/`parent`/`descendant` is still counted once, that the disjoint axes return every distinct result, and that an overlapping union still de-duplicates; the full XPath suite and the Apache Xalan XSLT gold-output corpus stay green. diff --git a/Sources/XPath/Evaluator.swift b/Sources/XPath/Evaluator.swift index 056a8f5..88420cb 100644 --- a/Sources/XPath/Evaluator.swift +++ b/Sources/XPath/Evaluator.swift @@ -165,7 +165,12 @@ extension PureXML.XPath { private static func evalPath(absolute: Bool, steps: [Step], _ context: EvaluationContext) throws -> Value { let start: Node = absolute ? root(of: context.node) : context.node - return try .nodeSet(orderUnique(evaluateSteps(steps, from: [start], context))) + // `evaluateSteps` already yields a duplicate-free node-set (each step + // either de-duplicates across contexts or uses an axis that cannot + // produce a duplicate), so only the document-order sort remains; the + // `Set` de-duplication in `orderUnique` would copy every node for + // nothing. + return try .nodeSet(evaluateSteps(steps, from: [start], context).sortedByDocumentOrder()) } private static func evalFilter( @@ -177,7 +182,9 @@ extension PureXML.XPath { var nodes = try orderUnique(nodeSet(eval(primary, context))) nodes = try applyPredicates(predicates, to: nodes, context) if !steps.isEmpty { - nodes = try orderUnique(evaluateSteps(steps, from: nodes, context)) + // As in `evalPath`, `evaluateSteps` returns a duplicate-free set, + // so the trailing path needs only the document-order sort. + nodes = try evaluateSteps(steps, from: nodes, context).sortedByDocumentOrder() } return .nodeSet(nodes) } diff --git a/docs/benchmarks.md b/docs/benchmarks.md index 1a86d45..3aa3850 100644 --- a/docs/benchmarks.md +++ b/docs/benchmarks.md @@ -62,6 +62,7 @@ offsets throughout; libxml2's xmlReadMemory takes an int byte count). | 8 | serialize escaping fast path (text/attribute/comment/PI return the value unchanged when no character needs escaping, instead of rebuilding it character by character; byte-level scan for the escapable bytes) | unchanged | unchanged | 0.032 s (2.7x, from 5.5x) | | 9 | skip cross-context node-set de-duplication on a single context or a disjoint axis (child/attribute/namespace/self); the `Set` hashing and copying was pure overhead where no duplicate can arise, same output (same-state A/B, ~28%) | unchanged | 0.097 s (10x, from 13x) | unchanged | | 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 | ## Profile findings (first sample, parse path)