From 4bc2f39d6e9edda20a329c1ab84b21938105a42f Mon Sep 17 00:00:00 2001 From: James Liu Date: Mon, 27 Feb 2023 23:59:04 +0000 Subject: [PATCH] Use a bounded channel in the multithreaded executor (#7829) # Objective This is a follow-up to #7745. An unbounded `async_channel` occasionally allocates whenever it exceeds the capacity of the current buffer in it's internal linked list. This is avoidable. This also used to be a bounded channel before stageless, which was introduced in #4919. ## Solution Use a bounded channel to avoid allocations on system completion. This shouldn't conflict with #7745, as it's impossible for the scheduler to exceed the channel capacity, even if somehow every system completed at the same time. --- crates/bevy_ecs/src/schedule/executor/multi_threaded.rs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs b/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs index 252cf6675d7e01..881b311e334570 100644 --- a/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs +++ b/crates/bevy_ecs/src/schedule/executor/multi_threaded.rs @@ -121,6 +121,10 @@ impl SystemExecutor for MultiThreadedExecutor { let sys_count = schedule.system_ids.len(); let set_count = schedule.set_ids.len(); + let (tx, rx) = async_channel::bounded(sys_count.max(1)); + + self.sender = tx; + self.receiver = rx; self.evaluated_sets = FixedBitSet::with_capacity(set_count); self.ready_systems = FixedBitSet::with_capacity(sys_count); self.ready_systems_copy = FixedBitSet::with_capacity(sys_count);