Repository navigation
🐛 fix(etree): stop drop_tree copying the tail - #1935
Merged
Merged
Conversation
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.
Merging this PR will not alter performance
Comparing Footnotes
|
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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). 🌳 Nowdrop_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.tailisNone. 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