Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update init_resource to not overwrite #1349

Merged
merged 5 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
// 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>() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does "double hash" the type id. Resources don't currently have an "entry" api. And I don't think adding one is in scope for this pr. But can we add a // PERF: remove this double hash comment so it is discoverable?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would be difficult in this case, because we need to both be modifying self.resources() for the Entry, and reading it for the from_resources call - so without some gnarly unsafe code, I'm not sure this is feasible.
It's also worth noting that this is using the bevy_utils::HashMap, so the hashing is rather cheap.
Also this only happens at app startup time, so the cost doesn't matter too much. I've added a comment explaining this though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense. i added a "PERF" label here. I use those like TODO labels, but for places where we are knowingly leaving perf on the table.

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