PureXML/Tests/SiblingAxisTests.swift
Mihaela Mihaljevic b104723fed
perf(xpath): cache and fuse the following-sibling/preceding-sibling axes (#306)
siblingPosition(of:) located the context node with
parent.children.firstIndex { $0 === tree } on every call: a linear scan of the
parent's child list, independent of the result. Over a wide parent evaluated
from many context siblings (//x/following-sibling::y) that scan was quadratic,
and the axes then materialized every sibling before the node test filtered it.

Look the index up through the shared per-evaluation SiblingIndexCache (O(1)
after the per-parent table is built), and fuse the step's node test into the
walk so a rejected sibling is never wrapped (the same fusion the descendant and
following/preceding axes use). The selected siblings and their order are
unchanged.

The gratuitous per-context scan and the non-match wrapping are gone (the
remaining per-context iteration over the siblings is inherent to the axis); a
wide-parent following-sibling query is ~1.2-1.6x faster. The full suite, the
W3C XPath/XSLT and Apache Xalan corpora, and the WASM build stay green;
SiblingAxisTests pins the fused node test and document order.
2026-06-25 22:41:32 +02:00

45 lines
2.0 KiB
Swift

import Testing
@testable import PureXML
/// The following-sibling and preceding-sibling axes look up a node's index among
/// its parent's children through a shared cache (not a scan per context node)
/// and fuse the step's node test into the walk (a rejected sibling is never
/// wrapped). These pin that the selected siblings and their order are unchanged:
/// a name test selects only matching siblings, and the path result is in
/// document order for both axes.
@Suite("following-sibling and preceding-sibling axes")
struct SiblingAxisTests {
private static let doc = "<r><a/><b/><c/><a/><d/></r>"
private func names(_ path: String) throws -> [String] {
try PureXML.XPath.Query(path).evaluate(over: PureXML.parse(Self.doc)).compactMap {
if case let .node(node) = $0 { return node.element?.name.localName }
return nil
}
}
@Test("following-sibling selects later siblings in document order")
func test_following() throws {
#expect(try names("//b/following-sibling::*") == ["c", "a", "d"])
// A name test keeps only the matching siblings (the fused node test).
#expect(try names("//b/following-sibling::a") == ["a"])
#expect(try names("//d/following-sibling::*") == [])
}
@Test("preceding-sibling selects earlier siblings (path result in document order)")
func test_preceding() throws {
#expect(try names("//c/preceding-sibling::*") == ["a", "b"])
#expect(try names("//d/preceding-sibling::a") == ["a", "a"])
#expect(try names("//a/preceding-sibling::*") == ["a", "b", "c"])
}
@Test("the axes are correct across many context nodes (shared cache)")
func test_manyContexts() throws {
let xml = "<r>" + String(repeating: "<x/>", count: 60) + "</r>"
// Each x[i] has 60 - i following x siblings; the deduplicated union is the
// 59 x's after the first.
let total = try Int(PureXML.XPath.Query("count(//x/following-sibling::x)").value(at: PureXML.parseTree(xml)).number)
#expect(total == 59)
}
}