Skip to content

Commit b922a3e

Browse files
authored
Update init_resource to not overwrite (#1349)
Update init_resource to not overwrite
1 parent 8e0e422 commit b922a3e

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

crates/bevy_app/src/app_builder.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,8 +235,13 @@ impl AppBuilder {
235235
where
236236
R: FromResources + Send + Sync + 'static,
237237
{
238-
let resource = R::from_resources(&self.app.resources);
239-
self.app.resources.insert(resource);
238+
// PERF: We could avoid double hashing here, since the `from_resources` call is guaranteed not to
239+
// modify the map. However, we would need to be borrowing resources both mutably and immutably,
240+
// so we would need to be extremely certain this is correct
241+
if !self.resources().contains::<R>() {
242+
let resource = R::from_resources(&self.resources());
243+
self.add_resource(resource);
244+
}
240245

241246
self
242247
}
@@ -245,8 +250,11 @@ impl AppBuilder {
245250
where
246251
R: FromResources + 'static,
247252
{
248-
let resource = R::from_resources(&self.app.resources);
249-
self.app.resources.insert_thread_local(resource);
253+
// See perf comment in init_resource
254+
if self.app.resources.get_thread_local::<R>().is_none() {
255+
let resource = R::from_resources(&self.app.resources);
256+
self.app.resources.insert_thread_local(resource);
257+
}
250258

251259
self
252260
}

crates/bevy_render/src/lib.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,7 @@ impl Plugin for RenderPlugin {
177177
shader::clear_shader_defs_system.system(),
178178
);
179179

180-
if app.resources().get::<Msaa>().is_none() {
181-
app.init_resource::<Msaa>();
182-
}
180+
app.init_resource::<Msaa>();
183181

184182
if let Some(ref config) = self.base_render_graph_config {
185183
let resources = app.resources();

0 commit comments

Comments
 (0)