Repository navigation
🐛 fix(xpath): index positional selections on axes - #2398
Merged
Merged
Conversation
A predicate that selects by position on a walked axis, or that looks down a descendant axis, scanned the whole axis again for every context node. On nested or flat documents the cost grew with the square of the node count. Each evaluation now numbers its tree once and answers the selection by rank and select over the good nodes of the step. Ancestor and sibling steps climb skew-binary jump chains. The walk memo keeps the nearest and existence forms, and the index takes the positional forms and the descendant axes. Results match the plain walk; growth is linear. Fixes tox-dev#2381
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.
XPath queries that select by position among siblings or ancestors, or that look inside a subtree, slowed down with the square of the document size (#2381). A query that asks for the second matching ancestor of every element re-scanned the whole chain for each element, so a document of a few thousand nodes already took seconds. Once this merges, those queries take time proportional to the document, and the answers stay the same.
The first time an evaluation needs a positional selection or a subtree query, it numbers the document once, in document order, and keeps the matching nodes of each step in that order. Each selection then becomes a lookup by rank instead of a walk. The existing walk memo keeps answering the nearest-match and existence forms, which were already linear, and the evaluator picks one of the two answers per path. The trade-off is one index per evaluation that uses these forms, with memory proportional to the document; evaluations that never use them build nothing.
Callers see the same results for every expression. The only visible change is the time these queries take on large documents. No configuration or migration is needed.
Performance
Callgrind instructions per call on release LTO builds, measured at 20 and 220 repetitions. The base is upstream commit 7fc9028. Commits upstream landed after it change one file in the shared DOM code and no xpath source, so the table applies to the merged branch. The
xpathrow, which checks every link element of the specification document for an attribute, drops by 0.677%: the existence test now checks the first step's axis before choosing a route. The rows above +0.05% (xpath-compare-unequal,xpath-set-disjoint,xpath-set-difference) run comparison and set code this change does not modify. Their deltas moved by up to 0.6 percent between two builds of the same rows, which is code placement.xpath-compare-scalarmoves by 1 instruction per call, under the noise floor.Fixes #2381