From 37ebf3de6c7168e0e03fb892b6276638322d5279 Mon Sep 17 00:00:00 2001 From: MinerSebas Date: Fri, 19 Mar 2021 20:33:51 +0100 Subject: [PATCH] Provide better size_hint for QueryIter --- crates/bevy_ecs/src/query/iter.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/crates/bevy_ecs/src/query/iter.rs b/crates/bevy_ecs/src/query/iter.rs index 4186ce45c455f..4878d349b4352 100644 --- a/crates/bevy_ecs/src/query/iter.rs +++ b/crates/bevy_ecs/src/query/iter.rs @@ -123,6 +123,20 @@ where } } } + + // NOTE: For unfiltered Queries this should actually return a exact size hint, + // to fulfil the ExactSizeIterator invariant, but this isn't practical without specialization. + // For more information see Issue #1686. + fn size_hint(&self) -> (usize, Option) { + let max_size = self + .query_state + .matched_archetypes + .ones() + .map(|index| self.world.archetypes[ArchetypeId::new(index)].len()) + .sum(); + + (0, Some(max_size)) + } } // NOTE: We can cheaply implement this for unfiltered Queries because we have: