Skip to content

🐛 fix(etree): stop drop_tree copying the tail - #1935

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

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

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

ElementView.drop_tree() copied the dropped element's tail onto that element after detaching it. The tail is the whole run of text after the element, and it already held the text that earlier drops had left in the parent. Each drop therefore reread and copied a run that grew with every drop, so repeated drops within one parent took quadratic time and memory: dropping 16,000 matches from a 144 KB page reached 530 MB of peak memory and 5.6 s (#1727). 🌳 Now drop_tree() only detaches the element, and each call does constant work.

The text after the element stays in the parent, which the etree reference already described. The dropped element has no tail afterwards, and the reference now says so.

After drop_tree(), the dropped element's .tail is None. Before this change it returned a copy of the text that stays in the parent. The markup of the tree after the drop is unchanged.

Fixes #1727

drop_tree() gave the dropped element a copy of its tail. The tail is the
whole text run after the element, and that run already holds the tails
left in the parent by earlier drops. Each drop rereads the run and writes
a new copy into the tree's arena, so a sequence of drops within one parent
took time and memory quadratic in the number of drops.

The tail now stays in the parent and the dropped element has none, so each
drop does constant work. A copy kept per drop would stay quadratic, so the
copy is removed rather than moved. Code that read .tail of a dropped
element now gets None.
@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 not alter performance

✅ 582 untouched benchmarks
⏩ 89 skipped benchmarks1


Comparing gaborbernat:sec-fix/eg8j0kgf (9dc3dd1) with main (5ab55d1)

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 6a176c0 into tox-dev:main Nov 15, 2026
31 checks passed
gaborbernat added a commit to gaborbernat/trafilatura that referenced this pull request Nov 22, 2026
The Python element adapter wrapped every node and rebuilt text and tail
runs in Python, which made fast and fallback extraction slower than the
lxml baseline. turbohtml.etree implements the same lxml.html operations
in C, so dom imports it directly and the adapter goes away.

drop_tree() leaves the text after the element in the parent, but the
dropped element keeps no tail (tox-dev/turbohtml#1935). Trafilatura never
reads it, so the test checks only the parent.
gaborbernat added a commit to gaborbernat/trafilatura that referenced this pull request Nov 24, 2026
The Python element adapter wrapped every node and rebuilt text and tail
runs in Python, which made fast and fallback extraction slower than the
lxml baseline. turbohtml.etree implements the same lxml.html operations
in C, so dom imports it directly and the adapter goes away.

drop_tree() leaves the text after the element in the parent, but the
dropped element keeps no tail (tox-dev/turbohtml#1935). Trafilatura never
reads it, so the test checks only the parent.
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.

Dropping matched elements in reverse order takes quadratic time and memory

1 participant