diff --git a/crates/bevy_app/src/app_builder.rs b/crates/bevy_app/src/app_builder.rs index fb69222b9b5ef..e0fc4bce29a68 100644 --- a/crates/bevy_app/src/app_builder.rs +++ b/crates/bevy_app/src/app_builder.rs @@ -235,8 +235,13 @@ impl AppBuilder { where R: FromResources + Send + Sync + 'static, { - let resource = R::from_resources(&self.app.resources); - self.app.resources.insert(resource); + // 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::() { + let resource = R::from_resources(&self.resources()); + self.add_resource(resource); + } self } @@ -245,8 +250,11 @@ impl AppBuilder { where R: FromResources + 'static, { - let resource = R::from_resources(&self.app.resources); - self.app.resources.insert_thread_local(resource); + // 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); + } self } 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();