Skip to content

Commit cd2faf2

Browse files
committed
Fix: avoid unnecessary list allocation in broadcast close() (#372)
1 parent 89dcb4e commit cd2faf2

File tree

1 file changed

+3
-5
lines changed

1 file changed

+3
-5
lines changed

pkgs/async/lib/src/stream_group.dart

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -300,22 +300,20 @@ class StreamGroup<T> implements Sink<Stream<T>> {
300300
// For a broadcast group that's closed, we must listen to streams with
301301
// null subscriptions to detect when they complete. This ensures the
302302
// group itself can close once all its streams have closed.
303-
final streamsToRemove = <Stream<T>>[];
303+
List<Stream<T>>? streamsToRemove;
304304

305305
_subscriptions.updateAll((stream, subscription) {
306306
if (subscription != null) return subscription;
307307

308308
try {
309309
return _listenToStream(stream);
310310
} on Object {
311-
streamsToRemove.add(stream);
311+
(streamsToRemove ??= []).add(stream);
312312
return null;
313313
}
314314
});
315315

316-
for (final stream in streamsToRemove) {
317-
_subscriptions.remove(stream);
318-
}
316+
streamsToRemove?.forEach(_subscriptions.remove);
319317
}
320318

321319
return _controller.done;

0 commit comments

Comments
 (0)