perf: Replace generator-based tree teardown and propagation with an explicit collection pass - #3981
Merged
Merged
Conversation
This was referenced Aug 5, 2026
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 5, 2026 20:18
4276a39 to
237d786
Compare
luanpotter
reviewed
Aug 7, 2026
| }, | ||
| includeSelf: true, | ||
| ); | ||
| final buffer = _teardownBuffer.isEmpty ? _teardownBuffer : <Component>[]; |
Member
There was a problem hiding this comment.
is allocating one empty list per component removal that significant? feels like this static cache is introducing potential future concurrency problems or hard to find headaches. seems that major gain here is the non-generator teardown and not the brittle [] cache?
luanpotter
reviewed
Aug 7, 2026
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
2 times, most recently
from
August 16, 2026 13:42
707869e to
f9caf7d
Compare
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 16, 2026 13:44
f9caf7d to
ed22345
Compare
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 16, 2026 14:42
ed22345 to
ad280e1
Compare
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 16, 2026 14:52
ad280e1 to
8d97b08
Compare
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 16, 2026 15:05
8d97b08 to
1b48f55
Compare
luanpotter
reviewed
Aug 16, 2026
| /// subtrees. This matches the order that | ||
| /// `descendants(reversed: true, includeSelf: true)` would produce, without | ||
| /// allocating generator frames on every removal. | ||
| void _collectTeardown(List<Component> out) { |
Member
There was a problem hiding this comment.
wondering if descendants should just use this implementation, then they 1 stay in sync and 2 get the perf benefit?
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
2 times, most recently
from
August 16, 2026 15:17
c817249 to
933bb82
Compare
…xplicit collection pass
spydon
force-pushed
the
perf/removal-teardown-buffer
branch
from
August 16, 2026 15:27
933bb82 to
13caa12
Compare
luanpotter
approved these changes
Aug 16, 2026
Member
There was a problem hiding this comment.
LGTM, but it is unfortunate that now we have 3 shapes of traversal (before we had 1)
descendants- still kept, lazy using generators, can stop at any time, pays (apparently) huge cost because generators suck?propagateToChildren- fastest and can stop, but does not collect list_collectDescendants- fast and creates list, but eager
And we are saying that _remove cannot use propagateToChildren because of concurrent modifications, but it is not clear to me when that happens.
5 tasks
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
The removal teardown and
propagateToChildrenwalked the subtree through the recursivedescendantssync* generator, allocating generator frames per tree level on every traversal. The teardown now collects the subtree into a local buffer (same leaves-first order) via_collectDescendantsand iterates that snapshot, sinceonRemovecallbacks may mutate the tree mid-walk.propagateToChildreninstead walks the tree with direct recursion and unwinds as soon as a handler stops propagation, so it neither allocates a buffer nor visits more components than the lazy generator did. Event delivery (deliverToComponents) is routed throughpropagateToChildren, so tap, drag, and keyboard propagation benefit as well.The public
descendants()method keeps its documented lazy semantics, since user code relies on early stopping and live iteration there.Extracted from #3960 so the data-structure change there stands alone (as requested in this comment).
Checklist
docsand added dartdoc comments with///.examplesordocs.Breaking Change?
Related Issues
Relates to #3957