Skip to content

xpath: keep multi-input step results in document order - #87

Open
dkam wants to merge 1 commit into
jonwiggins:mainfrom
dkam:xpath-nested-input-order
Open

dkam wants to merge 1 commit into
jonwiggins:mainfrom
dkam:xpath-nested-input-order

Conversation

@dkam

@dkam dkam commented Nov 21, 2026

Copy link
Copy Markdown
Contributor

When a step runs from several input nodes, its results can come out of document order. apply_step skips the final sort on the child, descendant, descendant-or-self, self, following and following-sibling axes, because each input node's results on them are in document order. Joined together they aren't always:

let doc = Document::parse_str("<r><a><b/><d/></a><c/></r>").unwrap();
Expression main libxml2 2.14 and 2.15, and this PR
//*/* a, c, b, d a, b, d, c
//*/following-sibling::* c, d d, c
//*/following::* c, d d, c
//*/*[last()] c, d d, c
//*/descendant::*[last()] c, d d, c

On the child and following axes the join breaks when one input node is inside another. On following-sibling it also breaks when one is inside a following sibling of another: with <r><a/><b><c/><d/></b><e/></r>, (/r/a | //c)/following-sibling::* gives b, e, d. Predicates filter each input node's results on their own, so with predicates the descendant axes break too.

The change

As each input node's results are added, check that they start after the ones before them, and sort the step's results if not. The check runs only on the axes where the join can go wrong: child, following and following-sibling, plus the two descendant axes when the step has predicates. It costs one comparison per input node. The doc comment on joins_can_break_order gives the reasoning for each axis.

The comparison is by id, and ids stop following document order once a document is edited. Trusting them would sort results that were already in document order into id order: append y to a in <r><a/><b><x/></b></r> and /r/*/* would change from y, x (right, as on main) to x, y. So a join that looks out of order by id is checked against the tree, by precedes_in_tree, before it triggers a sort. On a parsed document that happens only when the join really is out of order. The walk is out of line, so the common case doesn't pay for it.

Tests

Four new tests in xpath/eval.rs:

  • Two cover 13 expressions over the two documents above, without and with predicates. Both fail on main: 10 of the expressions come out in the wrong order there. The other 3 were already right and guard against regressions: the descendant axes without predicates, and following:: from the union. Every expected order was checked against libxml2 2.14.6 and 2.15.4.
  • One covers edited documents, where joins are in document order but not in id order. It passes on main, and fails if the check trusts ids.
  • One tests precedes_in_tree directly.

Taking any axis out of joins_can_break_order, or breaking any branch of precedes_in_tree, makes a test fail.

I also checked random trees: 2,000 small documents, each with 1,386 expressions (7 input node-sets × 11 axes × 3 node tests × 6 predicates), checking that every node-set is in document order. On main, 148,337 of the 2.77 million node-sets are out of order. With this change, none are.

On randomly edited documents, compared against the tree's actual order, this change corrects 11,400 node-sets that main gets wrong. It gets 164 wrong that main gets right. In all of those, the step's input was already out of order on main too, because the sort uses ids (#85), and main's final answer was right only by chance.

All 1,671 tests pass, as do W3C 1727/1727 and libxml2 119/119. Clippy and docs are clean.

Cost

Instructions per query, counted with callgrind on parser_bench's XPath fixture (50 books in one library):

  • Child, following and following-sibling steps from many input nodes pay about 30 instructions per input node: //book/title +2.3%, //book/*[last()] +1.5%, //book/* +1.1%, //title/following-sibling::* +1.1%, //book/following-sibling::book[1] +0.4%.
  • parser_bench's other five XPath queries: within ±0.1%, except xpath_complex at +0.5%. The relative paths in its predicate are single-input steps, which pay about 2 instructions each.
  • Self and descendant steps from many input nodes: //*/self::title +0.4%, //book//text() +0.04%.
  • Queries whose input nodes nest now pay for the sort they were missing: //*/* +17.1%, //*/*[last()] +5.9%.

Timed as the median of 21 alternating runs: //book/title +2.0%, //book/* +0.9%, //book//text() +0.3%, //*/* +16.4%. //*/self::title timed at +3.6%, against +0.4% in instructions; builds of this crate vary by up to about 4% from code layout alone on this machine.

Not covered

A node-set can enter a path out of document order, from reverse() or a variable. Self and descendant steps on such a node-set still return it unsorted, as on main. Checking their joins too would cost every multi-input self and descendant step the same 30 instructions per input node.

Related

This touches the same part of apply_step as the change proposed in #85. With #85's document-order keys, the check could compare keys instead of walking the tree. I've tested that version on our side.

This came up while comparing against libxml2 for the Rust Nokogiri experiment mentioned in #84.

🤖 Generated with Claude Code

apply_step skips the final sort on the child, descendant,
descendant-or-self, self, following and following-sibling axes, because
each input node's results on them come out in document order. Joined
together they aren't always: on the child and following axes when one
input node is inside another (`//*/*`), and on following-sibling also
when one is inside a following sibling of another. Predicates, which
filter each input node's results on their own, break the descendant
axes the same way (`//*/descendant::*[last()]`).

Where the join can go wrong, check that each input node's results start
after the ones before them, and sort if not: one comparison per input
node. Ids stop following document order once a document is edited, so
a join that looks out of order by id is checked against the tree before
it triggers a sort; otherwise results that were already in document
order would be sorted into id order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant