From b8c790ba407eb2f4b22389e6f6d377ed827295d0 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Fri, 29 Jan 2021 15:31:24 +0000 Subject: [PATCH 1/5] Update init_resource to not overwrite --- crates/bevy_app/src/app_builder.rs | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index fb69222b9b5ef..d5284c276f492 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -235,8 +235,10 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { - let resource = R::from_resources(&self.app.resources); - self.app.resources.insert(resource); + if self.resources().contains::() { + let resource = R::from_resources(&self.app.resources); + self.add_resource(resource); + } self } @@ -245,8 +247,10 @@ impl AppBuilder { where R: FromResources + 'static, { - let resource = R::from_resources(&self.app.resources); - self.app.resources.insert_thread_local(resource); + if self.app.resources.get_thread_local::().is_some() { + let resource = R::from_resources(&self.app.resources); + self.app.resources.insert_thread_local(resource); + } self } From dc1a26e5dcfe98ab393f5440eec9d8478bdafa89 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Fri, 29 Jan 2021 15:33:58 +0000 Subject: [PATCH 2/5] Don't double check for init_resources --- crates/bevy_render/src/lib.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/crates/bevy_render/src/lib.rs b/crates/bevy_render/src/lib.rs index 39ca67dc2b627..56c55010b557b 100644 --- a/crates/bevy_render/src/lib.rs +++ b/crates/bevy_render/src/lib.rs @@ -177,9 +177,7 @@ impl Plugin for RenderPlugin { shader::clear_shader_defs_system.system(), ); - if app.resources().get::().is_none() { - app.init_resource::(); - } + app.init_resource::(); if let Some(ref config) = self.base_render_graph_config { let resources = app.resources(); From d749b514d49660b7fafe383284f2b81aaf958ad2 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Fri, 29 Jan 2021 15:34:40 +0000 Subject: [PATCH 3/5] Fix incorrect conditions --- crates/bevy_app/src/app_builder.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index d5284c276f492..dc84b9a17b84c 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -235,7 +235,7 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { - if self.resources().contains::() { + if !self.resources().contains::() { let resource = R::from_resources(&self.app.resources); self.add_resource(resource); } @@ -247,7 +247,7 @@ impl AppBuilder { where R: FromResources + 'static, { - if self.app.resources.get_thread_local::().is_some() { + if self.app.resources.get_thread_local::().is_none() { let resource = R::from_resources(&self.app.resources); self.app.resources.insert_thread_local(resource); } From e231ada73a7e6c7b734812e91dfb7429a498e9b6 Mon Sep 17 00:00:00 2001 From: Daniel McNab <36049421+DJMcNab@users.noreply.github.com> Date: Sat, 30 Jan 2021 11:46:38 +0000 Subject: [PATCH 4/5] Add comment about double hashing --- crates/bevy_app/src/app_builder.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index dc84b9a17b84c..d1afda8b69cee 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -235,8 +235,11 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { + // We could avoid double hashing here, since the `from_resources` call is guaranteed not to + // modify the map. However, we would need to be borrowing resources both mutably and immutably, + // so we would need to be extremely certain this is correct if !self.resources().contains::() { - let resource = R::from_resources(&self.app.resources); + let resource = R::from_resources(&self.resources()); self.add_resource(resource); } @@ -247,6 +250,7 @@ impl AppBuilder { where R: FromResources + 'static, { + // See perf comment in init_resource if self.app.resources.get_thread_local::().is_none() { let resource = R::from_resources(&self.app.resources); self.app.resources.insert_thread_local(resource); From bdc4f3c4acd24c19bc9eab24d84e22930d7b1a42 Mon Sep 17 00:00:00 2001 From: Carter Anderson Date: Sat, 30 Jan 2021 12:12:44 -0800 Subject: [PATCH 5/5] add PERF label --- crates/bevy_app/src/app_builder.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index d1afda8b69cee..e0fc4bce29a68 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -235,7 +235,7 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { - // We could avoid double hashing here, since the `from_resources` call is guaranteed not to + // PERF: We could avoid double hashing here, since the `from_resources` call is guaranteed not to // modify the map. However, we would need to be borrowing resources both mutably and immutably, // so we would need to be extremely certain this is correct if !self.resources().contains::() {