Repository navigation
Convert many sibling divs in linear time - #45
Merged
mscrivo merged 1 commit intoNov 24, 2026
Merged
Conversation
A nested div gets no leading line break if it has all of its parent's text, which was checked by comparing the parent's text with the div's. Computing the parent's text again for every child div made converting documents with many sibling divs quadratic: 625 KB of sibling divs took 13 seconds. The stripped text lengths are now compared first, with the parent's length computed once. If a sibling has text, the parent's text is longer than the div's, so the texts are only compared for at most one child per parent.
mscrivo
reviewed
Nov 24, 2026
| end | ||
|
|
||
| def stripped_text_length(node) | ||
| @stripped_text_lengths ||= {}.compare_by_identity |
Collaborator
There was a problem hiding this comment.
probably should reset this at the start of convert calls?
Collaborator
There was a problem hiding this comment.
actually on second thought, I don't think we need to
Collaborator
|
thanks for your contribution @fschwahn! |
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.
A nested div gets no leading line break if it has all of its parent's text, which was checked by comparing the parent's text with the div's. Computing the parent's text again for every child div made converting documents with many sibling divs quadratic: 625 KB of sibling divs took 13 seconds.
The stripped text lengths are now compared first, with the parent's length computed once. If a sibling has text, the parent's text is longer than the div's, so the texts are only compared for at most one child per parent.
As this library commonly runs on untrusted user input (ie. incoming emails), I think it's worth fixing this edge case. According to my measurements this allocates less objects than before despite the cache, because we exit early if lengths are different.