From 21f003f13f06f938f9d0980278e1aada373060ef Mon Sep 17 00:00:00 2001 From: Eagster <79881080+ElliottjPierce@users.noreply.github.com> Date: Mon, 10 Mar 2025 17:49:07 -0400 Subject: [PATCH] Improve component registration performance (#18180) # Objective Make component registration faster. This is a tinny, almost petty PR, but it led to roughly 10% faster registration, according to my benchmarks in #17871. Up to y'all if we do this or not. It is completely unnecessary, but its such low hanging fruit that I wanted to put it out there. ## Solution Instead of cloning a `HashSet`, collect it into a `SmallVec`. Since this is empty for many components, this saves a lot of allocation and hashing work. Co-authored-by: Alice Cecile --- crates/bevy_ecs/src/component.rs | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/bevy_ecs/src/component.rs b/crates/bevy_ecs/src/component.rs index c0d2afe540..bb543ec0e1 100644 --- a/crates/bevy_ecs/src/component.rs +++ b/crates/bevy_ecs/src/component.rs @@ -34,6 +34,7 @@ use core::{ ops::{Deref, DerefMut}, }; use disqualified::ShortName; +use smallvec::SmallVec; use thiserror::Error; /// A data type that can be used to store data for an [entity]. @@ -2067,7 +2068,10 @@ impl Components { required_components.0.extend(required_components_tmp.0); // Propagate the new required components up the chain to all components that require the requiree. - if let Some(required_by) = self.get_required_by(requiree).cloned() { + if let Some(required_by) = self + .get_required_by(requiree) + .map(|set| set.iter().copied().collect::>()) + { // `required` is now required by anything that `requiree` was required by. self.get_required_by_mut(required) .unwrap()