Repository navigation
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When a step runs from several input nodes, its results can come out of document order.
apply_stepskips 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://*/*//*/following-sibling::*//*/following::*//*/*[last()]//*/descendant::*[last()]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_ordergives 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
ytoain<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, byprecedes_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:following::from the union. Every expected order was checked against libxml2 2.14.6 and 2.15.4.precedes_in_treedirectly.Taking any axis out of
joins_can_break_order, or breaking any branch ofprecedes_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 (50books in onelibrary)://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%, exceptxpath_complexat +0.5%. The relative paths in its predicate are single-input steps, which pay about 2 instructions each.//*/self::title+0.4%,//book//text()+0.04%.//*/*+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::titletimed 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_stepas 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