From 180b3497215dd4e3da5b214d03286823e63f7bdb Mon Sep 17 00:00:00 2001 From: Elliott Pierce Date: Sat, 31 May 2025 17:49:13 -0400 Subject: [PATCH] all destructs increment the generation --- crates/bevy_ecs/src/entity/map_entities.rs | 9 ++++++--- crates/bevy_ecs/src/world/entity_ref.rs | 10 +++++----- crates/bevy_ecs/src/world/mod.rs | 14 -------------- 3 files changed, 11 insertions(+), 22 deletions(-) diff --git a/crates/bevy_ecs/src/entity/map_entities.rs b/crates/bevy_ecs/src/entity/map_entities.rs index 883095933b..75526868d4 100644 --- a/crates/bevy_ecs/src/entity/map_entities.rs +++ b/crates/bevy_ecs/src/entity/map_entities.rs @@ -299,9 +299,12 @@ impl<'m> SceneEntityMapper<'m> { /// safely allocate any more references, this method takes ownership of `self` in order to render it unusable. pub fn finish(self, world: &mut World) { // SAFETY: We never constructed the entity and never released it for something else to construct. - unsafe { - world.release_generations_unchecked(self.dead_start.row(), self.generations); - } + let reuse_row = unsafe { + world + .entities + .mark_free(self.dead_start.row(), self.generations) + }; + world.allocator.free(reuse_row); } /// Creates an [`SceneEntityMapper`] from a provided [`World`] and [`EntityHashMap`], then calls the diff --git a/crates/bevy_ecs/src/world/entity_ref.rs b/crates/bevy_ecs/src/world/entity_ref.rs index e047ead152..6dec064703 100644 --- a/crates/bevy_ecs/src/world/entity_ref.rs +++ b/crates/bevy_ecs/src/world/entity_ref.rs @@ -2373,6 +2373,7 @@ impl<'w> EntityWorldMut<'w> { /// Destructs the entity, without releasing it. /// This may be later [`constructed`](Self::construct). + /// Note that this still increases the generation to differentiate different constructions of the same row. #[track_caller] pub fn destruct(&mut self) -> &mut Self { self.destruct_with_caller(MaybeLocation::caller()) @@ -2517,7 +2518,10 @@ impl<'w> EntityWorldMut<'w> { } // finish + // SAFETY: We just destructed it. + self.entity = unsafe { self.world.entities.mark_free(self.entity.row(), 1) }; self.world.flush(); + self.update_location(); // In case some command re-constructs this entity. self } @@ -2536,11 +2540,7 @@ impl<'w> EntityWorldMut<'w> { pub(crate) fn despawn_with_caller(mut self, caller: MaybeLocation) { self.destruct_with_caller(caller); - // SAFETY: We just destructed. - unsafe { - self.world - .release_generations_unchecked(self.entity.row(), 1); - } + self.world.allocator.free(self.entity); } /// Ensures any commands triggered by the actions of Self are applied, equivalent to [`World::flush`] diff --git a/crates/bevy_ecs/src/world/mod.rs b/crates/bevy_ecs/src/world/mod.rs index f745e1fb43..dd33e2f89b 100644 --- a/crates/bevy_ecs/src/world/mod.rs +++ b/crates/bevy_ecs/src/world/mod.rs @@ -1553,20 +1553,6 @@ impl World { Ok(entity) } - /// Releases `entity` to be reused. - /// - /// # Safety - /// - /// It must have been destructed. - pub(crate) unsafe fn release_generations_unchecked( - &mut self, - entity: EntityRow, - generations: u32, - ) { - self.allocator - .free(self.entities.mark_free(entity, generations)); - } - /// Clears the internal component tracker state. /// /// The world maintains some internal state about changed and removed components. This state