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

[Merged by Bors] - small ecs cleanup and remove_bundle drop bugfix #2172

Closed
wants to merge 4 commits into from

Conversation

Frizi
Copy link
Contributor

@Frizi Frizi commented May 15, 2021

  • simplified code around archetype generations a little bit, as the special case value is not actually needed
  • removed unnecessary UnsafeCell around pointer value that is never updated through shared references
  • fixed and added a test for correct drop behaviour when removing sparse components through remove_bundle command

crates/bevy_ecs/src/archetype.rs Show resolved Hide resolved
crates/bevy_ecs/src/system/commands.rs Outdated Show resolved Hide resolved
@mockersf mockersf added C-Code-Quality A section of code that is hard to understand or change A-ECS Entities, components, systems, and events labels May 16, 2021
@cart
Copy link
Member

cart commented May 17, 2021

This looks good to me. Love the archetype generation simplification!

I added UnsafeCell largely because hecs does it in a similar context. We never create shared mutable references to a particular "range" in data, but we do access different offsets of data mutably at the same time (ex: we allow accessing entity1's component data at the same time we access entity2's component data). We also allow shared references of Tables (which use BlobVec) to exist at the same time that mutable references to data exist (which I believe is what the hecs comment calls out).

As long as these use cases are ok, I'm down to remove it. But I want to be doubly sure before removing it (and im not even singly sure at this point 😄).

@Frizi
Copy link
Contributor Author

Frizi commented May 17, 2021

NonNull<T> already represents *mut T with extra guarantee of not being a null pointer, no need to put it inside UnsafeCell. It is only required to have UnsafeCell in order to get &mut T out of &T in any way, but in our case there are no shared references involved, as the memory comes directly from alloc call.

@cart
Copy link
Member

cart commented May 17, 2021

I buy that. I think that means that hecs could remove it too? @Ralith you might want to look in to this?

@Ralith
Copy link

Ralith commented May 17, 2021

Thanks for the ping! My knee-jerk reaction is that the interpretation implied by this change makes more sense; the argument for UnsafeCell to be used here always seemed suspiciously magical to me, though I ultimately played it safe. I'll run the question by experts before removing it, though.

Co-authored-by: Nathan Ward <43621845+NathanSWard@users.noreply.github.com>
@Ralith
Copy link

Ralith commented May 18, 2021

Every source I've checked confirms that UnsafeCell is unnecessary here.

data: UnsafeCell<NonNull<u8>>,
swap_scratch: UnsafeCell<NonNull<u8>>,
data: NonNull<u8>,
swap_scratch: NonNull<u8>,
Copy link
Contributor

@bjorn3 bjorn3 May 18, 2021

Choose a reason for hiding this comment

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

I can confirm this UnsafeCell is unnecessary. Neither pointer is itself changed behind &self. Only the pointed to value can be changed using &self, which is fine as they are raw pointers that are not derived from a reference.

@cart
Copy link
Member

cart commented May 18, 2021

Sounds like we have consensus. A big thanks to everyone for providing insight!

@cart
Copy link
Member

cart commented May 18, 2021

bors r+

bors bot pushed a commit that referenced this pull request May 18, 2021
- simplified code around archetype generations a little bit, as the special case value is not actually needed
- removed unnecessary UnsafeCell around pointer value that is never updated through shared references
- fixed and added a test for correct drop behaviour when removing sparse components through remove_bundle command
@bors bors bot changed the title small ecs cleanup and remove_bundle drop bugfix [Merged by Bors] - small ecs cleanup and remove_bundle drop bugfix May 18, 2021
@bors bors bot closed this May 18, 2021
Ralith added a commit to Ralith/hecs that referenced this pull request May 23, 2021
The shared memory is behind a raw pointer, which reference visibility
semantics don't see through. See
bevyengine/bevy#2172 for discussion.
Ralith added a commit to Ralith/hecs that referenced this pull request May 24, 2021
The shared memory is behind a raw pointer, which reference visibility
semantics don't see through. See
bevyengine/bevy#2172 for discussion.
Ralith added a commit to Ralith/hecs that referenced this pull request May 24, 2021
The shared memory is behind a raw pointer, which reference visibility
semantics don't see through. See
bevyengine/bevy#2172 for discussion.
Ralith added a commit to Ralith/hecs that referenced this pull request May 24, 2021
The shared memory is behind a raw pointer, which reference visibility
semantics don't see through. See
bevyengine/bevy#2172 for discussion.
ostwilkens pushed a commit to ostwilkens/bevy that referenced this pull request Jul 27, 2021
- simplified code around archetype generations a little bit, as the special case value is not actually needed
- removed unnecessary UnsafeCell around pointer value that is never updated through shared references
- fixed and added a test for correct drop behaviour when removing sparse components through remove_bundle command
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants