Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions packages/flame/lib/src/components/core/component_tree_root.dart
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,18 @@ class ComponentTreeRoot extends Component {
}

void processLifecycleEvents() {
if (!hasLifecycleEvents) {
assert(
_lifecycleEventsCompleter == null,
'The completer is only ever created while events are queued, so it '
'should never exist while the queue is empty',
);
return;
}
// reorder events to process later grouped by parent
final reorderParents = <Component>{};
Set<Component>? reorderParents;
LifecycleEventStatus handleReorderEvent(Component parent) {
reorderParents.add(parent);
(reorderParents ??= {}).add(parent);
return LifecycleEventStatus.done;
}

Expand All @@ -151,7 +159,8 @@ class ComponentTreeRoot extends Component {
for (final event in queue) {
final child = event.child!;
final parent = event.parent!;
if (_blocked.contains(child) || _blocked.contains(parent)) {
if (_blocked.isNotEmpty &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is the isNotEmpty saving any time here, the contains should be O(0) in that case?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It saves a little bit since it has to calculate the identityHashCode in contains, meanwhile isNotEmpty is virtually free.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this because _blocked is almost always empty? that doesn't appear to necessarily be the case here.

I honestly don't see how a hash check could be any sort of bottleneck, in fact if it were wouldn't the set contains implementation do this? according to clanker the dart identityHashCode implementation is cached per object and saved on the 64-bit pointer header.
I am ok with keeping if we have evidence but I am in favour of having commented out justifications for non-obvious micro-optimizations such as this. as is this looks like a silly mistake. a simple one line comment justifying the cost and why this is such a hot path would make it more clear.

thinking more broadly I am now wondering if this repeat with blocked thing is not ideal to begin with, there might be a way to sort the events in a deterministically correct order, but that is out of scope, just a thought.

(_blocked.contains(child) || _blocked.contains(parent))) {
continue;
}

Expand All @@ -176,7 +185,7 @@ class ComponentTreeRoot extends Component {
_blocked.clear();
}

for (final parent in reorderParents) {
for (final parent in reorderParents ?? const <Component>{}) {
parent.rebalanceChildren();
}

Expand Down
Loading