From 7ec3a14ec58c3e75fbfcac48d1564ae30ea463d6 Mon Sep 17 00:00:00 2001 From: Mat Hostetter <4849531+mjhostet@users.noreply.github.com> Date: Thu, 16 Mar 2023 14:03:44 -0700 Subject: [PATCH] Remove redundant bounds check in `Entities::get` Rather than manually check `Vec` bounds before taking a reference to that slot using brackets (which will do the same bounds check again), use `Vec::get` to do both operations in one step. Although the redundant bounds check should be optimized away in release mode, using `Vec::get` is shorter and more idiomatic. --- crates/bevy_ecs/src/entity/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/bevy_ecs/src/entity/mod.rs b/crates/bevy_ecs/src/entity/mod.rs index 1b1afe3151fc6..843e3262a2bf3 100644 --- a/crates/bevy_ecs/src/entity/mod.rs +++ b/crates/bevy_ecs/src/entity/mod.rs @@ -571,8 +571,7 @@ impl Entities { /// Returns the location of an [`Entity`]. /// Note: for pending entities, returns `Some(EntityLocation::INVALID)`. pub fn get(&self, entity: Entity) -> Option { - if (entity.index as usize) < self.meta.len() { - let meta = &self.meta[entity.index as usize]; + if let Some(meta) = self.meta.get(entity.index as usize) { if meta.generation != entity.generation || meta.location.archetype_id == ArchetypeId::INVALID {