Repository navigation
Conversation
Both the BeautifulSoup and the Parsel implementation appended to a growing string and, on every text node and block element, ran a regex or endswith() over that whole buffer to decide whether a leading space or a newline was needed. Each check re-read everything emitted so far, so the traversal was quadratic in the number of nodes: a 0.6 MB document of 64000 inline elements took 53 s on CPython 3.13 and 160000 elements took minutes. The output is now collected in a list of parts, the checks look only at the last emitted character, and the parts are joined once at the end. The same inputs take 0.3 s and 0.8 s, and the output is unchanged on the existing fixtures and on randomly generated markup. The two regex constants that only served the old checks are removed.
The CI unit tests run under coverage, where 160000 nodes take 13-15 s even with the linear implementation, so the fixed 2 s bound failed on every runner. The test now times 20000 and 80000 nodes and asserts the ratio stays under 10: the linear code measures 3.5-4.8 and the previous quadratic code 14-15, with or without coverage tracing.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #2372 +/- ##
==========================================
+ Coverage 94.06% 94.13% +0.06%
==========================================
Files 184 184
Lines 13654 13662 +8
==========================================
+ Hits 12844 12861 +17
+ Misses 810 801 -9
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
This branch has not been deployed
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.
Both
html_to_text()implementations (BeautifulSoup and Parsel) built the result by appending to one growing string. On every text node and block element they then ran a regex orendswith()over that whole buffer to decide whether a leading space or a newline was needed, so each check re-read everything emitted so far and the traversal was quadratic in the number of nodes. On CPython 3.13 a 0.6 MB document of 64000 inline elements took 53 s, 160000 elements took minutes, and the first 1 MB of a real Wikipedia article took 10 s.The output is now collected in a list of parts, the checks look only at the last emitted character, and the parts are joined once at the end. The 64000-element document takes 0.3 s and the 160000-element one about a second. The result is byte-identical to the old code on the existing fixtures and on 1200 randomly generated documents. The two regex constants that only served the old checks are gone. A new test converts 20000 and then 80000 nodes with each parser and asserts the second run takes less than ten times as long, which holds for the linear code (ratio 3.5 to 4.8) and fails for the old one (ratio 14 to 15), with or without coverage tracing. Same change as apify/crawlee#4322 on the JS side.