Skip to content

Commit 8e8ecc4

Browse files
committed
remove UnsafeCell from WorldCell cache
1 parent 23e6ddf commit 8e8ecc4

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

crates/bevy_ecs/src/world/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use bevy_ptr::{OwningPtr, Ptr};
2323
use bevy_utils::tracing::warn;
2424
use std::{
2525
any::TypeId,
26-
cell::UnsafeCell,
2726
fmt,
2827
sync::atomic::{AtomicU32, Ordering},
2928
};
@@ -61,7 +60,7 @@ pub struct World {
6160
pub(crate) bundles: Bundles,
6261
pub(crate) removed_components: SparseSet<ComponentId, Vec<Entity>>,
6362
/// Access cache used by [WorldCell]. Is only accessed in the `Drop` impl of `WorldCell`.
64-
pub(crate) archetype_component_access: UnsafeCell<ArchetypeComponentAccess>,
63+
pub(crate) archetype_component_access: ArchetypeComponentAccess,
6564
pub(crate) change_tick: AtomicU32,
6665
pub(crate) last_change_tick: u32,
6766
pub(crate) last_check_tick: u32,

crates/bevy_ecs/src/world/world_cell.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ impl<'w> Drop for WorldCell<'w> {
8080
let mut access = self.access.borrow_mut();
8181

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

8888
// give world ArchetypeComponentAccess back to reuse allocations
8989
std::mem::swap(world_cached_access, &mut *access);
@@ -185,7 +185,7 @@ impl<'w> WorldCell<'w> {
185185
pub(crate) fn new(world: &'w mut World) -> Self {
186186
// this is cheap because ArchetypeComponentAccess::new() is const / allocation free
187187
let access = std::mem::replace(
188-
world.archetype_component_access.get_mut(),
188+
&mut world.archetype_component_access,
189189
ArchetypeComponentAccess::new(),
190190
);
191191
// world's ArchetypeComponentAccess is recycled to cut down on allocations
@@ -435,11 +435,10 @@ mod tests {
435435
let u32_archetype_component_id = world
436436
.get_resource_archetype_component_id(u32_component_id)
437437
.unwrap();
438-
assert_eq!(world.archetype_component_access.get_mut().access.len(), 1);
438+
assert_eq!(world.archetype_component_access.access.len(), 1);
439439
assert_eq!(
440440
world
441441
.archetype_component_access
442-
.get_mut()
443442
.access
444443
.get(u32_archetype_component_id),
445444
Some(&BASE_ACCESS),

0 commit comments

Comments
 (0)