Update init_resource to not overwrite (#1349)

Update init_resource to not overwrite
This commit is contained in:
Daniel McNab 2021-01-30 20:48:11 +00:00 committed by GitHub
parent 8e0e4223e5
commit b922a3ec60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 7 deletions

View File

@ -235,8 +235,13 @@ impl AppBuilder {
where where
R: FromResources + Send + Sync + 'static, R: FromResources + Send + Sync + 'static,
{ {
let resource = R::from_resources(&self.app.resources); // PERF: We could avoid double hashing here, since the `from_resources` call is guaranteed not to
self.app.resources.insert(resource); // 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::<R>() {
let resource = R::from_resources(&self.resources());
self.add_resource(resource);
}
self self
} }
@ -245,8 +250,11 @@ impl AppBuilder {
where where
R: FromResources + 'static, R: FromResources + 'static,
{ {
let resource = R::from_resources(&self.app.resources); // See perf comment in init_resource
self.app.resources.insert_thread_local(resource); if self.app.resources.get_thread_local::<R>().is_none() {
let resource = R::from_resources(&self.app.resources);
self.app.resources.insert_thread_local(resource);
}
self self
} }

View File

@ -177,9 +177,7 @@ impl Plugin for RenderPlugin {
shader::clear_shader_defs_system.system(), shader::clear_shader_defs_system.system(),
); );
if app.resources().get::<Msaa>().is_none() { app.init_resource::<Msaa>();
app.init_resource::<Msaa>();
}
if let Some(ref config) = self.base_render_graph_config { if let Some(ref config) = self.base_render_graph_config {
let resources = app.resources(); let resources = app.resources();