From 075aa75d7d18ac9a638961123775d641ec90807b Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:40:17 -0700 Subject: [PATCH 1/6] Fix endless loop in empty view query. --- src/query/result/reshape.rs | 4 ++-- src/query/result/sealed/mod.rs | 2 +- src/query/view/sealed.rs | 2 +- src/registry/sealed/view.rs | 4 ++-- src/world/mod.rs | 18 ++++++++++++++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/query/result/reshape.rs b/src/query/result/reshape.rs index 3dda6384..17576556 100644 --- a/src/query/result/reshape.rs +++ b/src/query/result/reshape.rs @@ -13,8 +13,8 @@ pub trait Reshape { fn reshape(self) -> R; } -impl Reshape, Null> for iter::Repeat { - fn reshape(self) -> iter::Repeat { +impl Reshape>, Null> for iter::Take> { + fn reshape(self) -> iter::Take> { self } } diff --git a/src/query/result/sealed/mod.rs b/src/query/result/sealed/mod.rs index 16c87989..24c47453 100644 --- a/src/query/result/sealed/mod.rs +++ b/src/query/result/sealed/mod.rs @@ -14,7 +14,7 @@ pub trait Results { fn into_iterator(self) -> Self::Iterator; } -impl Results for iter::Repeat { +impl Results for iter::Take> { type View = view::Null; type Iterator = Self; diff --git a/src/query/view/sealed.rs b/src/query/view/sealed.rs index 2c759a86..cc3f8209 100644 --- a/src/query/view/sealed.rs +++ b/src/query/view/sealed.rs @@ -59,7 +59,7 @@ pub trait ViewsSealed<'a> { } impl<'a> ViewsSealed<'a> for Null { - type Results = iter::Repeat; + type Results = iter::Take>; } impl<'a, V, W> ViewsSealed<'a> for (V, W) diff --git a/src/registry/sealed/view.rs b/src/registry/sealed/view.rs index 5a824201..496c77ed 100644 --- a/src/registry/sealed/view.rs +++ b/src/registry/sealed/view.rs @@ -69,13 +69,13 @@ where impl<'a> CanonicalViews<'a, view::Null, Null> for registry::Null { unsafe fn view( _columns: &[(*mut u8, usize)], - _length: usize, + length: usize, _archetype_identifier: archetype::identifier::Iter, ) -> >::Results where R: Registry, { - iter::repeat(view::Null) + iter::repeat(view::Null).take(length) } unsafe fn view_one( diff --git a/src/world/mod.rs b/src/world/mod.rs index 6fd83f34..1577bb16 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1436,6 +1436,24 @@ mod tests { assert_eq!(b, &mut B('a')); } + #[test] + fn query_empty() { + let mut world = World::::new(); + + world.insert(entity!(B('a'), A(1))); + world.insert(entity!(A(2))); + world.insert(entity!(B('b'))); + world.insert(entity!()); + + let count = world + .query(Query::::new()) + .iter + .collect::>() + .len(); + + assert_eq!(count, 4); + } + #[cfg(feature = "rayon")] #[test] fn par_query_refs() { From f299d976dcd551692035a155072b37b9e31636c4 Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:40:55 -0700 Subject: [PATCH 2/6] Use count instead of container. --- src/world/mod.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/world/mod.rs b/src/world/mod.rs index 1577bb16..58ef9844 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1448,8 +1448,7 @@ mod tests { let count = world .query(Query::::new()) .iter - .collect::>() - .len(); + .count(); assert_eq!(count, 4); } From 991ed57cdca8c34f00c5b418678fbb67082cad73 Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:42:51 -0700 Subject: [PATCH 3/6] Fix endless loop in empty view parallel queries. --- src/registry/sealed/par_view.rs | 4 ++-- src/world/mod.rs | 20 ++++++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/src/registry/sealed/par_view.rs b/src/registry/sealed/par_view.rs index 51594c2b..8dd788ef 100644 --- a/src/registry/sealed/par_view.rs +++ b/src/registry/sealed/par_view.rs @@ -49,13 +49,13 @@ where impl<'a> CanonicalParViews<'a, view::Null, Null> for registry::Null { unsafe fn par_view( _columns: &[(*mut u8, usize)], - _length: usize, + length: usize, _archetype_identifier: archetype::identifier::Iter, ) -> >::ParResults where R: Registry, { - iter::repeatn(view::Null, usize::MAX) + iter::repeatn(view::Null, length) } } diff --git a/src/world/mod.rs b/src/world/mod.rs index 58ef9844..fe254a51 100644 --- a/src/world/mod.rs +++ b/src/world/mod.rs @@ -1445,10 +1445,7 @@ mod tests { world.insert(entity!(B('b'))); world.insert(entity!()); - let count = world - .query(Query::::new()) - .iter - .count(); + let count = world.query(Query::::new()).iter.count(); assert_eq!(count, 4); } @@ -1657,6 +1654,21 @@ mod tests { assert_eq!(b, &mut B('a')); } + #[cfg(feature = "rayon")] + #[test] + fn par_query_empty() { + let mut world = World::::new(); + + world.insert(entity!(B('a'), A(1))); + world.insert(entity!(A(2))); + world.insert(entity!(B('b'))); + world.insert(entity!()); + + let count = world.par_query(Query::::new()).iter.count(); + + assert_eq!(count, 4); + } + #[test] fn system_refs() { struct TestSystem; From 2189ce802c92840d2e11af2f90b73aa1d0cbc59f Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:53:35 -0700 Subject: [PATCH 4/6] Document fix in changelog. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c009655..26eaea28 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased +### Fixed +- Querying with empty component views no longer iterates endlessly. It now iterates once for each entity filtered, despite no components being viewed. ## 0.6.0 - 2023-03-20 ### Added From 341f23569720da252b269f99b4f389a9c95a3939 Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:54:53 -0700 Subject: [PATCH 5/6] Cut release in changelog. --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 26eaea28..86c197a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,8 @@ # Changelog ## Unreleased + +## 0.6.1 - 2023-03-21 ### Fixed - Querying with empty component views no longer iterates endlessly. It now iterates once for each entity filtered, despite no components being viewed. From 6f762d7c87cc4cfe089406873d0980e0c46fe242 Mon Sep 17 00:00:00 2001 From: Anders429 Date: Tue, 21 Mar 2023 22:55:16 -0700 Subject: [PATCH 6/6] Increment version number. --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8bf6935a..010fa929 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "brood" -version = "0.6.0" +version = "0.6.1" authors = ["Anders Evensen"] edition = "2021" rust-version = "1.65.0"