Skip to content

Commit 06738bf

Browse files
authored
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)
1 parent 8092e2c commit 06738bf

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

crates/bevy_ecs/src/storage/table.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl TableId {
5656
/// Will panic if the provided value does not fit within a [`u32`].
5757
#[inline]
5858
pub const fn from_usize(index: usize) -> Self {
59-
assert!(index as u32 as usize == index);
59+
debug_assert!(index as u32 as usize == index);
6060
Self(index as u32)
6161
}
6262

@@ -116,7 +116,7 @@ impl TableRow {
116116
/// Will panic if the provided value does not fit within a [`u32`].
117117
#[inline]
118118
pub const fn from_usize(index: usize) -> Self {
119-
assert!(index as u32 as usize == index);
119+
debug_assert!(index as u32 as usize == index);
120120
Self(index as u32)
121121
}
122122

0 commit comments

Comments
 (0)