Skip to content

Commit

Permalink
remove UnsafeCell from WorldCell cache
Browse files Browse the repository at this point in the history
  • Loading branch information
BoxyUwU committed Feb 2, 2023
1 parent 23e6ddf commit 8e8ecc4
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
3 changes: 1 addition & 2 deletions crates/bevy_ecs/src/world/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ use bevy_ptr::{OwningPtr, Ptr};
use bevy_utils::tracing::warn;
use std::{
any::TypeId,
cell::UnsafeCell,
fmt,
sync::atomic::{AtomicU32, Ordering},
};
Expand Down Expand Up @@ -61,7 +60,7 @@ pub struct World {
pub(crate) bundles: Bundles,
pub(crate) removed_components: SparseSet<ComponentId, Vec<Entity>>,
/// Access cache used by [WorldCell]. Is only accessed in the `Drop` impl of `WorldCell`.
pub(crate) archetype_component_access: UnsafeCell<ArchetypeComponentAccess>,
pub(crate) archetype_component_access: ArchetypeComponentAccess,
pub(crate) change_tick: AtomicU32,
pub(crate) last_change_tick: u32,
pub(crate) last_check_tick: u32,
Expand Down
13 changes: 6 additions & 7 deletions crates/bevy_ecs/src/world/world_cell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ impl<'w> Drop for WorldCell<'w> {
let mut access = self.access.borrow_mut();

{
// SAFETY: we only swap `archetype_component_access`
let world = unsafe { self.world.world() };
// SAFETY: the WorldCell has exclusive world access
let world_cached_access = unsafe { &mut *world.archetype_component_access.get() };
// SAFETY: `WorldCell` does not hand out `UnsafeWorldCell` to anywhere else so this is the only
// `UnsafeWorldCell` and we have exclusive access to it.
let world = unsafe { self.world.world_mut() };
let world_cached_access = &mut world.archetype_component_access;

// give world ArchetypeComponentAccess back to reuse allocations
std::mem::swap(world_cached_access, &mut *access);
Expand Down Expand Up @@ -185,7 +185,7 @@ impl<'w> WorldCell<'w> {
pub(crate) fn new(world: &'w mut World) -> Self {
// this is cheap because ArchetypeComponentAccess::new() is const / allocation free
let access = std::mem::replace(
world.archetype_component_access.get_mut(),
&mut world.archetype_component_access,
ArchetypeComponentAccess::new(),
);
// world's ArchetypeComponentAccess is recycled to cut down on allocations
Expand Down Expand Up @@ -435,11 +435,10 @@ mod tests {
let u32_archetype_component_id = world
.get_resource_archetype_component_id(u32_component_id)
.unwrap();
assert_eq!(world.archetype_component_access.get_mut().access.len(), 1);
assert_eq!(world.archetype_component_access.access.len(), 1);
assert_eq!(
world
.archetype_component_access
.get_mut()
.access
.get(u32_archetype_component_id),
Some(&BASE_ACCESS),
Expand Down

0 comments on commit 8e8ecc4

Please sign in to comment.