avoid quadratic tree traversal in pre_order/post_order/leaves#5235
Open
sahvx655-wq wants to merge 2 commits into
Open
avoid quadratic tree traversal in pre_order/post_order/leaves#5235sahvx655-wq wants to merge 2 commits into
sahvx655-wq wants to merge 2 commits into
Conversation
sahvx655-wq
force-pushed
the
iterative-tree-traversal
branch
from
July 13, 2026 16:34
dae9801 to
ca203a5
Compare
for more information, see https://pre-commit.ci
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.
Description
Profiling
blackdon a deeply nested expression such as a longa ** b ** c ** ...power chain showedpre_orderandleavesinblib2to3/pytree.pydominating the trace and growing quadratically.get_features_usedruns onepre_orderwalk over the whole tree on every format pass, so it is an easy place to measure: that walk alone took 8.8ms at 500 terms, 33.9ms at 1000, 132ms at 2000 and 520ms at 4000, quadrupling every time the depth doubled. All three traversal helpers (pre_order,post_order,leaves) descend withyield from child.<method>(), and becauseyield fromresumes every delegating generator in the chain on each step, one full walk costs O(depth) per node and is O(n^2) once the tree is n levels deep. This pass runs before any formatting, so a deeply nested submission is a cheap pre-auth amplification vector through unauthenticatedblackd.Replacing the recursive delegation with an explicit stack visits each node once regardless of depth. The traversal order is unchanged: I checked it leaf-for-leaf and in pre/post-order against the recursive form on assorted trees, and formatting is byte-identical across the whole
tests/datacorpus in stable, preview and unstable modes plus the full test suite (434 passed). Keeping the fix in the traversal helpers themselves linearises every caller at once (get_features_used,normalize_fmt_off, the--line-rangeswalk and the rest) instead of each having to special-case deep input; the same 4000-term walk drops from ~520ms to ~5.9ms and the curve goes from quadratic to linear.Checklist - did you ...
--previewstyle, following the stability policy?CHANGES.mdif necessary?