Skip to content

🐛 fix(xpath): index positional selections on axes - #2398

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/xpath-axes-linear
Nov 16, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:fix/xpath-axes-linear

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

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 xpath row, 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-scalar moves by 1 instruction per call, under the noise floor.

Row main Ir per call head Ir per call change
path-xpath 4,799,265 4,799,265 +0.000%
path-xpath-cold 1,007,907 1,007,907 +0.000%
path-xpath-one-cold 441,039 441,039 +0.000%
path-xpath-wide 906,361 906,361 +0.000%
xpath 485,987 482,699 -0.677%
xpath-compare 562,767 561,939 -0.147%
xpath-compare-scalar 3,491 3,492 +0.029%
xpath-compare-unequal 702,637 704,650 +0.286%
xpath-concat 1,503,246 1,503,246 +0.000%
xpath-concat-long 4,203 4,203 +0.000%
xpath-concat-mixed 178,196 178,195 -0.001%
xpath-concat-nested 6,043 6,043 +0.000%
xpath-concat-small 333,122 333,122 +0.000%
xpath-deep-descendants 1,414,757 1,414,757 +0.000%
xpath-distinct 232,872 232,872 +0.000%
xpath-distinct-empty 678,000 678,000 +0.000%
xpath-distinct-singleton 658,020 658,020 +0.000%
xpath-distinct-unique 269,422 269,422 +0.000%
xpath-id 724,765 724,762 -0.000%
xpath-id-nodes 8,823,524 8,823,521 -0.000%
xpath-id-nodes-long 5,856,179 5,856,176 -0.000%
xpath-order 705,231 705,238 +0.001%
xpath-order-ge 880,833 880,840 +0.001%
xpath-order-gt 880,829 880,836 +0.001%
xpath-order-le 880,823 880,830 +0.001%
xpath-regex-alternatives 925,822 925,647 -0.019%
xpath-regex-exslt 1,042,030 1,042,030 +0.000%
xpath-regex-matches 1,042,030 1,042,030 +0.000%
xpath-replace 1,024,600 1,024,599 -0.000%
xpath-replace-short 389,000 388,900 -0.026%
xpath-set 1,054,162 1,052,893 -0.120%
xpath-set-difference 380,567 380,888 +0.084%
xpath-set-disjoint 289,497 289,808 +0.107%
xpath-set-overlap 1,066,403 1,066,404 +0.000%
xpath-translate 298,637 298,632 -0.002%
xpath-translate-long-map 3,640 3,635 -0.137%
xpath-translate-repeated 298,007 298,002 -0.002%
xpath-translate-short 747,830 746,980 -0.114%
xpath-translate-varied 531,651 531,646 -0.001%
xpath-wide 183,318 183,319 +0.001%
xpath-wide-nested-descendants 4,898,647 4,898,647 +0.000%
xpath-wide-union 310,804 310,804 +0.000%

Fixes #2381

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
@gaborbernat gaborbernat added the bug Something isn't working label Nov 16, 2026
@gaborbernat
gaborbernat merged commit c2a57bf into tox-dev:main Nov 16, 2026
6 of 56 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

XPath positional predicates on walked axes take quadratic time

1 participant