From 06738bff639ee416a2b51a731ba8efe1f90a27f6 Mon Sep 17 00:00:00 2001 From: re0312 <45868716+re0312@users.noreply.github.com> Date: Tue, 2 Apr 2024 14:02:56 +0800 Subject: [PATCH] Remove unnecessary branch in query iteration (#12844) # Objective - Since #10811,Bevy uses `assert `in the hot path of iteration. The `for_each `method has an assert in the outer loop to help the compiler remove unnecessary branching in the internal loop. - However , ` for` style iterations do not receive the same treatment. it still have a branch check in the internal loop, which could potentially hurt performance. ## Solution - use `TableRow::from_u32 ` instead of ` TableRow::from_usize` to avoid unnecessary branch. Before ![image](https://github.com/bevyengine/bevy/assets/45868716/f6d2a1ac-2129-48ff-97bf-d86713ddeaaf) After ---------------------------------------------------------------------------- ![image](https://github.com/bevyengine/bevy/assets/45868716/bfe5a9ee-ba6c-4a80-85b0-1c6d43adfe8c) --- crates/bevy_ecs/src/storage/table.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/storage/table.rs b/crates/bevy_ecs/src/storage/table.rs index 8d6b03d1da189..3c06b41b91a48 100644 --- a/crates/bevy_ecs/src/storage/table.rs +++ b/crates/bevy_ecs/src/storage/table.rs @@ -56,7 +56,7 @@ impl TableId { /// Will panic if the provided value does not fit within a [`u32`]. #[inline] pub const fn from_usize(index: usize) -> Self { - assert!(index as u32 as usize == index); + debug_assert!(index as u32 as usize == index); Self(index as u32) } @@ -116,7 +116,7 @@ impl TableRow { /// Will panic if the provided value does not fit within a [`u32`]. #[inline] pub const fn from_usize(index: usize) -> Self { - assert!(index as u32 as usize == index); + debug_assert!(index as u32 as usize == index); Self(index as u32) }