perf: answer the <pre> ancestor check from parent_tags - #273
Open
dugynoo wants to merge 1 commit into
Open
Conversation
matthewwithanm#191 propagated parent tag context downward to replace ancestor tree walks. This call site was missed, so process_tag still calls find_parent('pre') once per node, making conversion O(nodes x depth). On a 366 KB document nested 5000 deep this takes conversion from 23.39 s to 0.34 s. The same content with no nesting takes 0.33 s before and 0.27 s after, so the nesting accounted for all of it. Output is unchanged: parent_tags excludes the node itself, exactly as find_parent does, and the separate node.name == 'pre' test still covers that case. Verified byte-identical on 20,000 real-world HTML documents.
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.
#191 propagated the parent tag context downward so ancestor questions could be answered from
parent_tagsinstead of walking the tree. One call site inprocess_tagstill walks:parent_tagsis in scope and already holds every ancestor tag name, so the same question is aset lookup:
find_parentruns once per node and walks to the root, which makes conversion O(nodes × depth).Flat documents never notice, nested ones do.
Measurement
("<div>" * 5000) + ("<b>x</b>" * 40000) + ("</div>" * 5000), 366 KB:The flat case is the floor, so the nesting accounted for all of it.
Equivalence
parent_tagsexcludes the node itself and so doesfind_parent, so the separatenode.name == 'pre'test still covers that case.Output is byte-identical on that input and on 20,000 real-world HTML emails: 0 differences.
The existing suite passes (83 tests), along with flake8 and restructuredtext-lint.