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

Conversation

DJMcNab
Copy link
Member

@DJMcNab DJMcNab commented Jan 29, 2021

The name init does not suggest it would overwrite an already created version.

We discussed this on discord and @cart has at one point been on board.

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Jan 29, 2021

I like this change, although it would be nice to change add_resource to set_resource at the same time to make the distinction clearer. Similarly, the 'Commands::insert_resource` should have its name unified based on its behavior.

See #1186 for more discussion on this topic.

Edit: I've made my own PR to solve this: #1352.

Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

I've looked over the codes changes themselves, and they're simple and sane 👍🏽

@@ -235,8 +235,10 @@ impl AppBuilder {
where
R: FromResources + Send + Sync + 'static,
{
let resource = R::from_resources(&self.app.resources);
self.app.resources.insert(resource);
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.

@DJMcNab
Copy link
Member Author

DJMcNab commented Jan 30, 2021

Well in that case, I think we'll all ready to merge.

Message to future readers: Don't try and solve that perf thing, you're in for a world of unsafe pain.

@cart cart merged commit b922a3e into bevyengine:master Jan 30, 2021
@DJMcNab DJMcNab deleted the init_overwritent branch February 19, 2021 20:56
alice-i-cecile added a commit to alice-i-cecile/bevy that referenced this pull request Nov 6, 2021
alice-i-cecile added a commit to alice-i-cecile/bevy that referenced this pull request Feb 8, 2022
bors bot pushed a commit that referenced this pull request Mar 31, 2022
# Objective

- The perf comments, added (by me) in #1349, became outdated once the initialisation call started to take an exclusive reference, (presumably in #1525).
- They have been naïvely transferred along ever since

## Solution

- Remove them
aevyrie pushed a commit to aevyrie/bevy that referenced this pull request Jun 7, 2022
# Objective

- The perf comments, added (by me) in bevyengine#1349, became outdated once the initialisation call started to take an exclusive reference, (presumably in bevyengine#1525).
- They have been naïvely transferred along ever since

## Solution

- Remove them
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
# Objective

- The perf comments, added (by me) in bevyengine#1349, became outdated once the initialisation call started to take an exclusive reference, (presumably in bevyengine#1525).
- They have been naïvely transferred along ever since

## Solution

- Remove them
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants