From a02c5ae819001b0d1ceb6cf7dba8b4f0d7975c15 Mon Sep 17 00:00:00 2001 From: James Liu Date: Mon, 30 May 2022 16:59:43 +0000 Subject: [PATCH] Copy TaskPool resoures to subapps (#4792) # Objective Fixes #4791. `ParallelExecutor` inserts a default `CompteTaskPool` if there isn't one stored as a resource, including when it runs on a different world. When spawning the render sub-app, the main world's `ComputeTaskPool` is not cloned and inserted into the render app's, which causes a second `ComputeTaskPool` with the default configuration to be spawned. This results in an excess number of threads being spawned. ## Solution Copy the task pools from the main world to the subapps upon creating them. ## Alternative An alternative to this would be to make the task pools global, as seen in #2250 or bevyengine/rfcs#54. --- crates/bevy_app/Cargo.toml | 1 + crates/bevy_app/src/app.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/crates/bevy_app/Cargo.toml b/crates/bevy_app/Cargo.toml index e386e00c2d..2dd25147e4 100644 --- a/crates/bevy_app/Cargo.toml +++ b/crates/bevy_app/Cargo.toml @@ -20,6 +20,7 @@ bevy_derive = { path = "../bevy_derive", version = "0.8.0-dev" } bevy_ecs = { path = "../bevy_ecs", version = "0.8.0-dev", default-features = false } bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", optional = true } bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } +bevy_tasks = { path = "../bevy_tasks", version = "0.8.0-dev" } # other serde = { version = "1.0", features = ["derive"], optional = true } diff --git a/crates/bevy_app/src/app.rs b/crates/bevy_app/src/app.rs index cdf1984c8b..d587344d65 100644 --- a/crates/bevy_app/src/app.rs +++ b/crates/bevy_app/src/app.rs @@ -10,6 +10,7 @@ use bevy_ecs::{ system::Resource, world::World, }; +use bevy_tasks::{AsyncComputeTaskPool, ComputeTaskPool, IoTaskPool}; use bevy_utils::{tracing::debug, HashMap}; use std::fmt::Debug; @@ -862,9 +863,18 @@ impl App { pub fn add_sub_app( &mut self, label: impl AppLabel, - app: App, + mut app: App, sub_app_runner: impl Fn(&mut World, &mut App) + 'static, ) -> &mut Self { + if let Some(pool) = self.world.get_resource::() { + app.world.insert_resource(pool.clone()); + } + if let Some(pool) = self.world.get_resource::() { + app.world.insert_resource(pool.clone()); + } + if let Some(pool) = self.world.get_resource::() { + app.world.insert_resource(pool.clone()); + } self.sub_apps.insert( Box::new(label), SubApp {