Skip to content

Commit

Permalink
Update init_resource to not overwrite (#1349)
Browse files Browse the repository at this point in the history
Update init_resource to not overwrite
  • Loading branch information
DJMcNab authored Jan 30, 2021
1 parent 8e0e422 commit b922a3e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 7 deletions.
16 changes: 12 additions & 4 deletions crates/bevy_app/src/app_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<R>() {
let resource = R::from_resources(&self.resources());
self.add_resource(resource);
}

self
}
Expand All @@ -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::<R>().is_none() {
let resource = R::from_resources(&self.app.resources);
self.app.resources.insert_thread_local(resource);
}

self
}
Expand Down
4 changes: 1 addition & 3 deletions crates/bevy_render/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,7 @@ impl Plugin for RenderPlugin {
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 {
let resources = app.resources();
Expand Down

0 comments on commit b922a3e

Please sign in to comment.