Skip to content

🐛 fix(xpath): keep nested predicates linear in pinned nodes - #2179

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/nlogpxwu
Nov 15, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/nlogpxwu

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

XPath expressions that call into Python, such as matches(), replace(), re:test() or an extension function, got slower with every level of nesting. Nested predicates over a large document could make one evaluation take several times as long as its flat form, and the cost grew with the nesting rather than the document. 🚀 Once this merges, nested expressions stay fast, and the cost no longer climbs with the nesting. Fixes #1748.

Before each call into Python, the engine hands out a wrapper for every node the evaluation holds, and it used to look each new node up in every live frame. Once an evaluation holds enough frames, the engine builds one index of those nodes and keeps it until the evaluation ends, so a new node costs one lookup. Shallow expressions keep the per-frame path.

Behavior changes

None. Results, errors and the wrappers handed to Python are the same. Only the cost of nested expressions that call into Python changes.

Before each call into Python, the pinning hook paired every live frame
with the frames it held by scanning them all, and looked every newly
pinned node up in each held frame's map. Nested predicates and
operators paid the nesting depth again per frame and per pinned node.

Once the live frames first reach 16, the evaluation indexes every
frame's pins by node and pairs the frames in one pass, until it ends.
Shallower expressions keep the per-frame lookup unchanged.

Fixes tox-dev#1748
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 6.99%

2 warnings

⚠️ 1 benchmark with a significant change ran on different hardware

Its base and head results were measured on machines with a different CPU, core count or memory, so the change might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 1 benchmark with a significant change ran in a different software environment

Its base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the change might come from this difference rather than from the code. Open the benchmark to see what changed.

See affected benchmarks

⚡ 1 improved benchmark
✅ 581 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_feature[query-closest] 273.9 µs 256 µs +6.99%

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/nlogpxwu (d4c3f24) with main (707bab0)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit 0d94f7b into tox-dev:main Nov 15, 2026
31 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 with matches() and deep predicate nesting spends seconds on node pinning

1 participant