Skip to content

🐛 fix(dom): keep text writes from storing every assigned value - #2133

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

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

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Writing Text.data or Comment.data many times kept every intermediate value in the document's memory. The arena frees nothing until the tree is dropped, so code that builds a text one piece at a time used memory that grew with the square of the final text. Moving each removed element's tail into the text before it was one such pattern, and calling normalize() after each append was another. 🧠 After this change a node reuses one buffer that grows by doubling, so memory tracks the text the node holds now.

Architecture

Each text node keeps its text in one buffer and records the capacity of that buffer. A write that fits is copied into the node's own buffer, and a write that does not fit takes a buffer twice the old capacity, so a run of writes allocates a constant multiple of its largest value. normalize() merges into the first Text node through the same path.

The etree text and tail setters update the first Text node of a run instead of replacing it. Any place that shares a buffer or moves a node's view into it drops the capacity, so only a buffer the node alone writes is reused.

Behavior changes

  • The etree text and tail setters update the existing first Text node instead of inserting a new one. A Text object held from before the assignment shows the new value, and a MutationObserver records a characterData change for that node instead of a childList change.
  • A node whose text has been extended may hold a buffer up to twice the length of its text.

Fixes #1760.

Assigning Text.data or Comment.data, or the etree text and tail
properties, copied the new value into a fresh arena buffer on every call.
The arena frees nothing until the tree is released, so code that grows a
text one piece at a time, such as the lxml idiom of handing each removed
element's tail to its neighbour, kept every intermediate value and used
memory that grows with the square of the final text. The same held for
normalize() after each append.

Each character-data node now keeps the capacity of its buffer in the
byte insert_text already uses for that. A write that fits goes into the
node's own buffer; one that does not takes a buffer of double the size,
so a run of writes allocates a small multiple of its largest value. The
first new buffer stays exact, so a node written once uses what it used
before. The etree setters write into the first Text node of the run
instead of replacing it, so they reach the same path. A buffer shared
with a parser clone loses its capacity, and so do one replaced by
whitespace collapsing and one a Range edit narrows or splits, so no write
lands in memory another node reads or past the end of its buffer.

Fixes tox-dev#1760
@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 33.33%

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_mutating_feature[path-xpath-one-cold] 143.2 µs 107.4 µs +33.33%

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/rhiqaxuq (255f489) with main (fe21f92)

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 f3c5d96 into tox-dev:main Nov 15, 2026
30 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.

Deleting sibling elements through etree text and tail setters grows memory quadratically

1 participant