Skip to content

🐛 fix(dom): keep Range calls over a wide child list linear - #2144

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/ksbajdy4
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/ksbajdy4

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Range methods that map a child to its offset, or an offset to its child, walked the sibling list from the first child on every call. 📏 A loop that calls a Range method once per child of a wide element, such as a highlighter that wraps each text node of one paragraph, therefore ran in quadratic time. After this change a loop over one child list takes a constant number of steps per call, in either direction.

Architecture

The tree handle keeps a few sibling cursors, each one a parent, a child and an index. A lookup starts from the cursor nearest its target, and a loop keeps a cursor one step behind it. Range's own edits shift the cursors they touch. An edit made through any other API, or normalize(), empties them, using the handle's mutation version and a count of unlinks.

Storing a rank in every node would also give constant-time offsets, but renumbering on each insertion moves the quadratic cost into tree building, and an order-statistic tree slows every child-list mutation. The cursors cost nothing outside Range calls.

Behavior changes

  • Offsets and results are unchanged. A Range call that follows an edit made through another API walks from the first child once, as before.
  • Range's boundary comparisons and common-ancestor lookup take the tree lock, because they write the cursors.

Fixes #1743

A Range offset is a child index, and the tree keeps children in a linked
list. Every Range call that turned a child into its offset or an offset
into its child walked the siblings from the first child: the boundary
setters, select_node, the point and boundary comparisons, the offset
validation, the child count, the contained-run scan, and the offset
lookups in insert_node and surround_contents. A program that calls one of
these once per child of a wide element, such as a highlighter wrapping
each text node of one paragraph, took time quadratic in the child count.

The tree handle now keeps four (parent, child, index) cursors, modelled
on the etree view cursors. A lookup walks from the node toward the first
child and, in step, both ways toward the cursors of the same parent, so a
loop in either direction over one child list takes a step per call. The
edits Range makes itself move the cursors with each insertion and
removal, so its own content operations keep them. Edits through other
APIs bump mutation_version, and normalize unlinks without one, so either
clears them. Code that never uses a Range pays only for the larger handle.

Fixes tox-dev#1743
@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 77.87%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

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

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

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

See affected benchmarks

⚡ 2 improved benchmarks
✅ 580 untouched benchmarks
⏩ 89 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_mutating_feature[range-partial] 90.8 µs 33.3 µs ×2.7
⚡ test_mutating_feature[path-xpath-one-cold] 124.7 µs 107.5 µs +16.05%

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/ksbajdy4 (473a06a) with main (3e0b864)

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 f631e58 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.

Range.surround_contents() time grows with the square of siblings under one parent

1 participant