-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
implement EntitySet and iter_many_unique methods #16547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
add EntityBorrow, TrustedEntityBorrow, EntitySet and EntitySetIterator traits add iter_many_unique, iter_many_unique_mut, iter_many_unique_unsafe methods on Query add iter_many_unique, iter_many_unique_mut, iter_many_unique_manual and iter_many_unique_unchecked_manual methods on QueryState add the corresponding QueryManyUniqueIter add UniqueEntityIter
Haven't had a chance to do a review yet, but just a thought:
We could implement impl EntitySet for HashSet<Entity, AGoodHasher> { /* ... */ }
impl EntitySet for HashSet<Entity, AnotherGoodHasher> { /* ... */ }
// ... Or perhaps add another trait, |
However, it is also a question whether/when we want The inconvenient newtyping and order instability leads me to suggest that we add an The true solution is to ask rust-lang to add a defaulted |
Ah ok, apologies for the misunderstanding, I fixated on that point! I'll give the full PR a review at some point this weekend |
/// | ||
/// - [`iter_many_unique_mut`](Self::iter_many_unique_mut) to get mutable query items. | ||
#[inline] | ||
pub fn iter_many_unique<'w, 's, EntityList: EntitySet>( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do agree that these should exist for API symmetry, but ... are the non-mut
versions actually useful? If you're getting the read-only version then iter_many()
already gives you an Iterator
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the mutable versions are definitely the main feature, these are nice too! Just the guarantee that each entity will only be handled once can be useful, especially in libraries.
There are non-unique query iterators like ancestor and descendant iterators that can be rejected this way.
It also serves as an intermediate step in tandem with iter_many_unique_mut
, if you want to query for some TrustedEntityBorrow
type, you can pass the query directly into iter_many_unique_mut
, or store it in a Vec
so you can pass it later. Mutable access is not needed for either.
/// A trait for entity borrows. | ||
/// | ||
/// This trait can be thought of as `Borrow<Entity>`, but yielding `Entity` directly. | ||
pub trait EntityBorrow { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the advantage of a separate trait here versus unsafe trait TrustedEntityBorrow: Borrow<Entity>
? I assumed it was to avoid the orphan rule somehow, but I don't see what type impls EntityBorrow
that wouldn't impl Borrow<Entity>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are a few reasons for this trait:
Borrow<T>
is blanket implemented for T
, &T
, &mut T
, Box<T>
, Rc<T>
, Arc<T>
.
These blankets overlap with the "for every &U where U: EntityBorrow
" impls we would like.
Because Borrow<Entity>
can not be guaranteed to be implemented for a reference, you'd need to bound EntitySetIterator
impls to only valid references:
unsafe impl<'a, T: TrustedEntityBorrow> EntitySetIterator for result::Iter<'a, T>
where &'a T: TrustedEntityBorrow {}
vs
unsafe impl<T: TrustedEntityBorrow> EntitySetIterator for result::Iter<'_, T>
In addition, we would now have to manually implement Borrow<Entity>
for all levels of nested impls that we want: &&T
, &&mut T
, &Arc<T>
, &mut SomeBorrowEntityType
These may seem unnecessary, but iterators/iterator adapters can easily introduce more reference levels to a type.
Furthermore, Entity
is Copy
, we do not need the reference returned by Borrow::borrow()
!
I only discovered this after making the PR, but the idea of such a trait has been floated before in #9367
EntityBorrow
also looks nicer than Borrow<Entity>
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was imagining that a few calls to Iterator::copied()
could prevent the deeply-nested types and save a whole pile of impl
blocks. Not a big deal either way!
Hmm, given that you aren't returning a reference anymore, maybe AsEntity
from the linked issue is a better name than EntityBorrow
.
Oh, and that reminds me! Do you want to implement TrustedEntityBorrow
for MainEntity
and RenderEntity
, since those are simple newtypes over Entity
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and that reminds me! Do you want to implement TrustedEntityBorrow for MainEntity and RenderEntity, since those are simple newtypes over Entity?
Yes! Anything that represents an Entity
and can delegate its comparison trait to Entity
.
There's a handful more of Entity
newtypes in bevy itself as far as I'm aware.
EntityBorrow
could even be made derivable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, given that you aren't returning a reference anymore, maybe AsEntity from the linked issue is a better name than EntityBorrow.
The name EntityBorrow
isn't fully ideal, but there is an important distinction between as
and borrow
in Rust:
borrow
is supposed to preserve equality, as
does not have this restriction. (See Borrow
and AsRef
docs, as
is also often used for lossy integer conversions)
It is important that this is not implemented for something that compares differently to Entity
, if that is really desired, then it can't be bundled with EntityBorrow
, it would have to be separate.
IMO, It's okay to start with the stricter version and introduce a relaxed one later if we want to!
I think we want Would it work to offer a safe method to create a |
I agree that we would like this to work with |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very thorough. and will be useful for perf optimization. Thanks for bringing this to my attention.
# Objective In current Bevy, it is very inconvenient to mutably retrieve a user-provided list of entities more than one element at a time. If the list contains any duplicate entities, we risk mutable aliasing. Users of `Query::iter_many_mut` do not have access to `Iterator` trait, and thus miss out on common functionality, for instance collecting their `QueryManyIter`. We can circumvent this issue with validation, however that entails checking every entity against all others for inequality, or utilizing an `EntityHashSet`. Even if an entity list remains unchanged, this validation is/would have to be redone every time we wish to fetch with the list. This presents a lot of wasted work, as we often trivially know an entity list to be unique f.e.: `QueryIter` will fetch every `Entity` once and only once. As more things become entities – assets, components, queries – this issue will become more pronounced. `get_many`/`many`/`iter_many`/`par_iter_many`-like functionality is all affected. ## Solution The solution this PR proposes is to introduce functionality built around a new trait: `EntitySet`. The goal is to preserve the property of "uniqueness" in a list wherever possible, and then rely on it as a bound within new `*_many_unique` methods to avoid the need for validation. This is achieved using `Iterator`: `EntitySet` is blanket implemented for any `T` that implements `IntoIterator<IntoIter: EntitySetIterator>`. `EntitySetIterator` is the unsafe trait that actually guarantees an iterator to be "unique" via its safety contract. We define an "Iterator over unique entities" as: "No two entities returned by the iterator may compare equal." For iterators that cannot return more than 1 element, this is trivially true. Whether an iterator can satisfy this is up to the `EntitySetIterator` implementor to ensure, hence the unsafe. However, this is not yet a complete solution. Looking at the signature of `iter_many`, we find that `IntoIterator::Item` is not `Entity`, but is instead bounded by the `Borrow<Entity>` trait. That is because iteration without consuming the collection will often yield us references, not owned items. `Borrow<Entity>` presents an issue: The `Borrow` docs state that `x = y` should equal `x.borrow() = y.borrow()`, but unsafe cannot rely on this for soundness. We run into similar problems with other trait implementations of any `Borrow<Entity>` type: `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Clone`, `Borrow`, and `BorrowMut`. This PR solves this with the unsafe `TrustedEntityBorrow` trait: Any implementor promises that the behavior of the aforementioned traits matches that of the underlying entity. While `Borrow<Entity>` was the inspiration, we use our own counterpart trait `EntityBorrow` as the supertrait to `TrustedEntityBorrow`, so we can circumvent the limitations of the existing `Borrow<T>` blanket impls. All together, these traits allow us to implement `*_many_unique` functionality with a lone `EntitySet` bound. `EntitySetIterator` is implemented for all the std iterators and iterator adapters that guarantee or preserve uniqueness, so we can filter, skip, take, step, reverse, ... our unique entity iterators without worry! Sadly, current `HashSet` iterators do not carry the necessary type information with them to determine whether the source `HashSet` produces logic errors; A malicious `Hasher` could compromise a `HashSet`. `HashSet` iteration is generally discouraged in the first place, so we also exclude the set operation iterators, even though they do carry the `Hasher` type parameter. `BTreeSet` implements `EntitySet` without any problems. If an iterator type cannot guarantee uniqueness at compile time, then a user can still attach `EntitySetIterator` to an individual instance of that type via `UniqueEntityIter::from_iterator_unchecked`. With this, custom types can use `UniqueEntityIter<I>` as their `IntoIterator::IntoIter` type, if necessary. This PR is focused on the base concept, and expansions on it are left for follow-up PRs. See "Potential Future Work" below. ## Testing Doctests on `iter_many_unique`/`iter_many_unique_mut` + 2 tests in entity_set.rs. ## Showcase ```rust // Before: fn system(player_list: Res<SomeUniquePlayerList>, players: Query<&mut Player>) { let value = 0; while let Some(player) = players.iter_many_mut(player_list).fetch_next() { value += mem::take(player.value_mut()) } } // After: fn system(player_list: Res<SomeUniquePlayerList>, players: Query<&mut Player>) { let value = players .iter_many_unique_mut(player_list) .map(|player| mem::take(player.value_mut())) .sum(); } ``` ## Changelog - added `EntityBorrow`, `TrustedEntityBorrow`, `EntitySet` and `EntitySetIterator` traits - added `iter_many_unique`, `iter_many_unique_mut`, `iter_many_unique_unsafe` methods on `Query` - added `iter_many_unique`, `iter_many_unique_mut`, `iter_many_unique_manual` and `iter_many_unique_unchecked_manual` methods on `QueryState` - added corresponding `QueryManyUniqueIter` - added `UniqueEntityIter` ## Migration Guide Any custom type used as a `Borrow<Entity>` entity list item for an `iter_many` method now has to implement `EntityBorrow` instead. Any type that implements `Borrow<Entity>` can trivially implement `EntityBorrow`. ## Potential Future Work - `ToEntitySet` trait for converting any entity iterator into an `EntitySetIterator` - `EntityIndexSet/Map` to tie in hashing with `EntitySet` - add `EntityIndexSetSlice/MapSlice` - requires: `EntityIndexSet/Map` - Implementing `par_iter_many_unique_mut` for parallel mutable iteration - requires: `par_iter_many` - allow collecting into `UniqueEntityVec` to store entity sets - add `UniqueEntitySlice`s - Doesn't require, but should be done after: `UniqueEntityVec` - add `UniqueEntityArray`s - Doesn't require, but should be done after: `UniqueEntitySlice` - `get_many_unique`/`many_unique` methods - requires: `UniqueEntityArray` - `World::entity_unique` to match `World::entity` methods - Doesn't require, but makes sense after: `get_many_unique`/`many_unique` - implement `TrustedEntityBorrow` for the `EntityRef` family - Doesn't require, but makes sense after: `UniqueEntityVec`
# Objective In #16547, we added `EntitySet`s/`EntitySetIterator`s. We can know whenever an iterator only contains unique entities, however we do not yet have the ability to collect and reuse these without either the unsafe `UniqueEntityIter::from_iterator_unchecked`, or the expensive `HashSet::from_iter`. An important piece for being able to do this is a `Vec` that maintains the uniqueness property, can be collected into, and is itself `EntitySet`. A lot of entity collections are already intended to be "unique", but have no way of expressing that when stored, other than using an aforementioned `HashSet`. Such a type helps by limiting or even removing the need for unsafe on the user side when not using a validated `Set` type, and makes it easier to interface with other infrastructure like f.e. `RelationshipSourceCollection`s. ## Solution We implement `UniqueEntityVec`. This is a wrapper around `Vec`, that only ever contains unique elements. It mirrors the API of `Vec`, however restricts any mutation as to not violate the uniqueness guarantee. Meaning: - Any inherent method which can introduce new elements or mutate existing ones is now unsafe, f.e.: `insert`, `retain_mut` - Methods that are impossible to use safely are omitted, f.e.: `fill`, `extend_from_within` A handful of the unsafe methods can do element-wise mutation (`retain_mut`, `dedup_by`), which can be an unwind safety hazard were the element-wise operation to panic. For those methods, we require that each individual execution of the operation upholds uniqueness, not just the entire method as a whole. To be safe for mutable usage, slicing and the associated slice methods require a matching `UniqueEntitySlice` type , which we leave for a follow-up PR. Because this type will deref into the `UniqueEntitySlice` type, we also offer the immutable `Vec` methods on this type (which only amount to a handful). "as inner" functionality is covered by additional `as_vec`/`as_mut_vec` methods + `AsRef`/`Borrow` trait impls. Like `UniqueEntityIter::from_iterator_unchecked`, this type has a `from_vec_unchecked` method as well. The canonical way to safely obtain this type however is via `EntitySetIterator::collect_set` or `UniqueEntityVec::from_entity_set_iter`. Like mentioned in #17513, these are named suboptimally until supertrait item shadowing arrives, since a normal `collect` will still run equality checks.
# Objective In current Bevy, it is very inconvenient to mutably retrieve a user-provided list of entities more than one element at a time. If the list contains any duplicate entities, we risk mutable aliasing. Users of `Query::iter_many_mut` do not have access to `Iterator` trait, and thus miss out on common functionality, for instance collecting their `QueryManyIter`. We can circumvent this issue with validation, however that entails checking every entity against all others for inequality, or utilizing an `EntityHashSet`. Even if an entity list remains unchanged, this validation is/would have to be redone every time we wish to fetch with the list. This presents a lot of wasted work, as we often trivially know an entity list to be unique f.e.: `QueryIter` will fetch every `Entity` once and only once. As more things become entities – assets, components, queries – this issue will become more pronounced. `get_many`/`many`/`iter_many`/`par_iter_many`-like functionality is all affected. ## Solution The solution this PR proposes is to introduce functionality built around a new trait: `EntitySet`. The goal is to preserve the property of "uniqueness" in a list wherever possible, and then rely on it as a bound within new `*_many_unique` methods to avoid the need for validation. This is achieved using `Iterator`: `EntitySet` is blanket implemented for any `T` that implements `IntoIterator<IntoIter: EntitySetIterator>`. `EntitySetIterator` is the unsafe trait that actually guarantees an iterator to be "unique" via its safety contract. We define an "Iterator over unique entities" as: "No two entities returned by the iterator may compare equal." For iterators that cannot return more than 1 element, this is trivially true. Whether an iterator can satisfy this is up to the `EntitySetIterator` implementor to ensure, hence the unsafe. However, this is not yet a complete solution. Looking at the signature of `iter_many`, we find that `IntoIterator::Item` is not `Entity`, but is instead bounded by the `Borrow<Entity>` trait. That is because iteration without consuming the collection will often yield us references, not owned items. `Borrow<Entity>` presents an issue: The `Borrow` docs state that `x = y` should equal `x.borrow() = y.borrow()`, but unsafe cannot rely on this for soundness. We run into similar problems with other trait implementations of any `Borrow<Entity>` type: `PartialEq`, `Eq`, `PartialOrd`, `Ord`, `Hash`, `Clone`, `Borrow`, and `BorrowMut`. This PR solves this with the unsafe `TrustedEntityBorrow` trait: Any implementor promises that the behavior of the aforementioned traits matches that of the underlying entity. While `Borrow<Entity>` was the inspiration, we use our own counterpart trait `EntityBorrow` as the supertrait to `TrustedEntityBorrow`, so we can circumvent the limitations of the existing `Borrow<T>` blanket impls. All together, these traits allow us to implement `*_many_unique` functionality with a lone `EntitySet` bound. `EntitySetIterator` is implemented for all the std iterators and iterator adapters that guarantee or preserve uniqueness, so we can filter, skip, take, step, reverse, ... our unique entity iterators without worry! Sadly, current `HashSet` iterators do not carry the necessary type information with them to determine whether the source `HashSet` produces logic errors; A malicious `Hasher` could compromise a `HashSet`. `HashSet` iteration is generally discouraged in the first place, so we also exclude the set operation iterators, even though they do carry the `Hasher` type parameter. `BTreeSet` implements `EntitySet` without any problems. If an iterator type cannot guarantee uniqueness at compile time, then a user can still attach `EntitySetIterator` to an individual instance of that type via `UniqueEntityIter::from_iterator_unchecked`. With this, custom types can use `UniqueEntityIter<I>` as their `IntoIterator::IntoIter` type, if necessary. This PR is focused on the base concept, and expansions on it are left for follow-up PRs. See "Potential Future Work" below. ## Testing Doctests on `iter_many_unique`/`iter_many_unique_mut` + 2 tests in entity_set.rs. ## Showcase ```rust // Before: fn system(player_list: Res<SomeUniquePlayerList>, players: Query<&mut Player>) { let value = 0; while let Some(player) = players.iter_many_mut(player_list).fetch_next() { value += mem::take(player.value_mut()) } } // After: fn system(player_list: Res<SomeUniquePlayerList>, players: Query<&mut Player>) { let value = players .iter_many_unique_mut(player_list) .map(|player| mem::take(player.value_mut())) .sum(); } ``` ## Changelog - added `EntityBorrow`, `TrustedEntityBorrow`, `EntitySet` and `EntitySetIterator` traits - added `iter_many_unique`, `iter_many_unique_mut`, `iter_many_unique_unsafe` methods on `Query` - added `iter_many_unique`, `iter_many_unique_mut`, `iter_many_unique_manual` and `iter_many_unique_unchecked_manual` methods on `QueryState` - added corresponding `QueryManyUniqueIter` - added `UniqueEntityIter` ## Migration Guide Any custom type used as a `Borrow<Entity>` entity list item for an `iter_many` method now has to implement `EntityBorrow` instead. Any type that implements `Borrow<Entity>` can trivially implement `EntityBorrow`. ## Potential Future Work - `ToEntitySet` trait for converting any entity iterator into an `EntitySetIterator` - `EntityIndexSet/Map` to tie in hashing with `EntitySet` - add `EntityIndexSetSlice/MapSlice` - requires: `EntityIndexSet/Map` - Implementing `par_iter_many_unique_mut` for parallel mutable iteration - requires: `par_iter_many` - allow collecting into `UniqueEntityVec` to store entity sets - add `UniqueEntitySlice`s - Doesn't require, but should be done after: `UniqueEntityVec` - add `UniqueEntityArray`s - Doesn't require, but should be done after: `UniqueEntitySlice` - `get_many_unique`/`many_unique` methods - requires: `UniqueEntityArray` - `World::entity_unique` to match `World::entity` methods - Doesn't require, but makes sense after: `get_many_unique`/`many_unique` - implement `TrustedEntityBorrow` for the `EntityRef` family - Doesn't require, but makes sense after: `UniqueEntityVec`
# Objective Some collections are more efficient to construct when we know that every element is unique in advance. We have `EntitySetIterator`s from bevyengine#16547, but currently no API to safely make use of them this way. ## Solution Add `FromEntitySetIterator` as a subtrait to `FromIterator`, and implement it for the `EntityHashSet`/`hashbrown::HashSet` types. To match the normal `FromIterator`, we also add a `EntitySetIterator::collect_set` method. It'd be better if these methods could shadow `from_iter` and `collect` completely, but rust-lang/rust#89151 is needed for that. While currently only `HashSet`s implement this trait, future `UniqueEntityVec`/`UniqueEntitySlice` functionality comes with more implementors. Because `HashMap`s are collected from tuples instead of singular types, implementing this same optimization for them is more complex, and has to be done separately. ## Showcase This is basically a free speedup for collecting `EntityHashSet`s! ```rust pub fn collect_milk_dippers(dippers: Query<Entity, (With<Milk>, With<Cookies>)>) { dippers.iter().collect_set::<EntityHashSet>(); // or EntityHashSet::from_entity_set_iter(dippers); } --------- Co-authored-by: SpecificProtagonist <[email protected]>
# Objective In bevyengine#16547, we added `EntitySet`s/`EntitySetIterator`s. We can know whenever an iterator only contains unique entities, however we do not yet have the ability to collect and reuse these without either the unsafe `UniqueEntityIter::from_iterator_unchecked`, or the expensive `HashSet::from_iter`. An important piece for being able to do this is a `Vec` that maintains the uniqueness property, can be collected into, and is itself `EntitySet`. A lot of entity collections are already intended to be "unique", but have no way of expressing that when stored, other than using an aforementioned `HashSet`. Such a type helps by limiting or even removing the need for unsafe on the user side when not using a validated `Set` type, and makes it easier to interface with other infrastructure like f.e. `RelationshipSourceCollection`s. ## Solution We implement `UniqueEntityVec`. This is a wrapper around `Vec`, that only ever contains unique elements. It mirrors the API of `Vec`, however restricts any mutation as to not violate the uniqueness guarantee. Meaning: - Any inherent method which can introduce new elements or mutate existing ones is now unsafe, f.e.: `insert`, `retain_mut` - Methods that are impossible to use safely are omitted, f.e.: `fill`, `extend_from_within` A handful of the unsafe methods can do element-wise mutation (`retain_mut`, `dedup_by`), which can be an unwind safety hazard were the element-wise operation to panic. For those methods, we require that each individual execution of the operation upholds uniqueness, not just the entire method as a whole. To be safe for mutable usage, slicing and the associated slice methods require a matching `UniqueEntitySlice` type , which we leave for a follow-up PR. Because this type will deref into the `UniqueEntitySlice` type, we also offer the immutable `Vec` methods on this type (which only amount to a handful). "as inner" functionality is covered by additional `as_vec`/`as_mut_vec` methods + `AsRef`/`Borrow` trait impls. Like `UniqueEntityIter::from_iterator_unchecked`, this type has a `from_vec_unchecked` method as well. The canonical way to safely obtain this type however is via `EntitySetIterator::collect_set` or `UniqueEntityVec::from_entity_set_iter`. Like mentioned in bevyengine#17513, these are named suboptimally until supertrait item shadowing arrives, since a normal `collect` will still run equality checks.
# Objective Follow-up to #17549 and #16547. A large part of `Vec`s usefulness is behind its ability to be sliced, like sorting f.e., so we want the same to be possible for `UniqueEntityVec`. ## Solution Add a `UniqueEntitySlice` type. It is a wrapper around `[T]`, and itself a DST. Because `mem::swap` has a `Sized` bound, DSTs cannot be swapped, and we can freely hand out mutable subslices without worrying about the uniqueness invariant of the backing collection! `UniqueEntityVec` and the relevant `UniqueEntityIter`s now have methods and trait impls that return `UniqueEntitySlice`s. `UniqueEntitySlice` itself can deref into normal slices, which means we can avoid implementing the vast majority of immutable slice methods. Most of the remaining methods: - split a slice/collection in further unique subsections/slices - reorder the slice: `sort`, `rotate_*`, `swap` - construct/deconstruct/convert pointer-like types: `Box`, `Arc`, `Rc`, `Cow` - are comparison trait impls As this PR is already larger than I'd like, we leave several things to follow-ups: - `UniqueEntityArray` and the related slice methods that would return it - denoted by "chunk", "array_*" for iterators - Methods that return iterators with `UniqueEntitySlice` as their item - `windows`, `chunks` and `split` families - All methods that are capable of actively mutating individual elements. While they could be offered unsafely, subslicing makes their safety contract weird enough to warrant its own discussion. - `fill_with`, `swap_with_slice`, `iter_mut`, `split_first/last_mut`, `select_nth_unstable_*` Note that `Arc`, `Rc` and `Cow` are not fundamental types, so even if they contain `UniqueEntitySlice`, we cannot write direct trait impls for them. On top of that, `Cow` is not a receiver (like `self: Arc<Self>` is) so we cannot write inherent methods for it either.
# Objective Continuation of #17589 and #16547. Slices have several methods that return iterators which themselves yield slices, which we have not yet implemented. An example use is `par_iter_many` style logic. ## Solution Their implementation is rather straightforward, we simply delegate all impls to `[T]`. The resulting iterator types need their own wrappers in the form of `UniqueEntitySliceIter` and `UniqueEntitySliceIterMut`. We also add three free functions that cast slices of entity slices to slices of `UniqueEntitySlice`. These three should be sufficient, though infinite nesting is achievable with a trait (like `TrustedEntityBorrow` works over infinite reference nesting), should the need ever arise.
# Objective Continuation of #16547. We do not yet have parallel versions of `par_iter_many` and `par_iter_many_unique`. It is currently very painful to try and use parallel iteration over entity lists. Even if a list is not long, each operation might still be very expensive, and worth parallelizing. Plus, it has been requested several times! ## Solution Once again, we implement what we lack! These parallel iterators collect their input entity list into a `Vec`/`UniqueEntityVec`, then chunk that over the available threads, inspired by the original `par_iter`. Since no order guarantee is given to the caller, we could sort the input list according to `EntityLocation`, but that would likely only be worth it for very large entity lists. There is some duplication which could likely be improved, but I'd like to leave that for a follow-up. ## Testing The doc tests on `for_each_init` of `QueryParManyIter` and `QueryParManyUniqueIter`.
# Objective Follow-up to bevyengine#17549 and bevyengine#16547. A large part of `Vec`s usefulness is behind its ability to be sliced, like sorting f.e., so we want the same to be possible for `UniqueEntityVec`. ## Solution Add a `UniqueEntitySlice` type. It is a wrapper around `[T]`, and itself a DST. Because `mem::swap` has a `Sized` bound, DSTs cannot be swapped, and we can freely hand out mutable subslices without worrying about the uniqueness invariant of the backing collection! `UniqueEntityVec` and the relevant `UniqueEntityIter`s now have methods and trait impls that return `UniqueEntitySlice`s. `UniqueEntitySlice` itself can deref into normal slices, which means we can avoid implementing the vast majority of immutable slice methods. Most of the remaining methods: - split a slice/collection in further unique subsections/slices - reorder the slice: `sort`, `rotate_*`, `swap` - construct/deconstruct/convert pointer-like types: `Box`, `Arc`, `Rc`, `Cow` - are comparison trait impls As this PR is already larger than I'd like, we leave several things to follow-ups: - `UniqueEntityArray` and the related slice methods that would return it - denoted by "chunk", "array_*" for iterators - Methods that return iterators with `UniqueEntitySlice` as their item - `windows`, `chunks` and `split` families - All methods that are capable of actively mutating individual elements. While they could be offered unsafely, subslicing makes their safety contract weird enough to warrant its own discussion. - `fill_with`, `swap_with_slice`, `iter_mut`, `split_first/last_mut`, `select_nth_unstable_*` Note that `Arc`, `Rc` and `Cow` are not fundamental types, so even if they contain `UniqueEntitySlice`, we cannot write direct trait impls for them. On top of that, `Cow` is not a receiver (like `self: Arc<Self>` is) so we cannot write inherent methods for it either.
# Objective Continuation of #17589 and #16547. `get_many` is last of the `many` methods with a missing `unique` counterpart. It both takes and returns arrays, thus necessitates a matching `UniqueEntityArray` type! Plus, some slice methods involve returning arrays, which are currently missing from `UniqueEntitySlice`. ## Solution Add the type, the related methods and trait impls. Note that for this PR, we abstain from some methods/trait impls that create `&mut UniqueEntityArray`, because it can be successfully mem-swapped. This can potentially invalidate a larger slice, which is the same reason we punted on some mutable slice methods in #17589. We can follow-up on all of these together in a following PR. The new `unique_array` module is not glob-exported, because the trait alias `unique_array::IntoIter` would conflict with `unique_vec::IntoIter`. The solution for this is to make the various `unique_*` modules public, which I intend to do in yet another PR.
# Objective Part of the #16547 series. The entity wrapper types often have some associated types an aliases with them that cannot be re-exported into an outer module together. Some helper types are best used with part of their path: `bevy::ecs::entity::index_set::Slice` as `index_set::Slice`. This has already been done for `entity::hash_set` and `entity::hash_map`. ## Solution Publicize the `index_set`, `index_map`, `unique_vec`, `unique_slice`, and `unique_array` modules. ## Migration Guide Any mention or import of types in the affected modules have to add the respective module name to the import path. F.e.: `bevy::ecs::entity::EntityIndexSet` -> `bevy::ecs::entity::index_set::EntityIndexSet`
# Objective Installment of the #16547 series. The vast majority of uses for these types will be the `Entity` case, so it makes sense for it to be the default. ## Solution `UniqueEntityVec`, `UniqueEntitySlice`, `UniqueEntityArray` and their helper iterator aliases now have `Entity` as a default. Unfortunately, this means the the `T` parameter for `UniqueEntityArray` now has to be ordered after the `N` constant, which breaks the consistency to `[T; N]`. Same with about a dozen iterator aliases that take some `P`/`F` predicate/function parameter. This should however be an ergonomic improvement in most cases, so we'll just have to live with this inconsistency. ## Migration Guide Switch type parameter order for the relevant wrapper types/aliases.
# Objective Continuation to #16547 and #17954. The `get_many` family are the last methods on `Query`/`QueryState` for which we're still missing a `unique` version. ## Solution Offer `get_many_unique`/`get_many_unique_mut` and `get_many_unique_inner`! Their implementation is the same as `get_many`, the difference lies in their guaranteed-to-be unique inputs, meaning we never do any aliasing checks. To reduce confusion, we also rename `get_many_readonly` into `get_many_inner` and the current `get_many_inner` into `get_many_mut_inner` to clarify their purposes. ## Testing Doc examples. ## Migration Guide `get_many_inner` is now called `get_many_mut_inner`. `get_many_readonly` is now called `get_many_inner`.
# Objective Installment of the bevyengine#16547 series. The vast majority of uses for these types will be the `Entity` case, so it makes sense for it to be the default. ## Solution `UniqueEntityVec`, `UniqueEntitySlice`, `UniqueEntityArray` and their helper iterator aliases now have `Entity` as a default. Unfortunately, this means the the `T` parameter for `UniqueEntityArray` now has to be ordered after the `N` constant, which breaks the consistency to `[T; N]`. Same with about a dozen iterator aliases that take some `P`/`F` predicate/function parameter. This should however be an ergonomic improvement in most cases, so we'll just have to live with this inconsistency. ## Migration Guide Switch type parameter order for the relevant wrapper types/aliases.
…alent (#18470) # Objective Fixes #9367. Yet another follow-up to #16547. These traits were initially based on `Borrow<Entity>` because that trait was what they were replacing, and it felt close enough in meaning. However, they ultimately don't quite match: `borrow` always returns references, whereas `EntityBorrow` always returns a plain `Entity`. Additionally, `EntityBorrow` can imply that we are borrowing an `Entity` from the ECS, which is not what it does. Due to its safety contract, `TrustedEntityBorrow` is important an important and widely used trait for `EntitySet` functionality. In contrast, the safe `EntityBorrow` does not see much use, because even outside of `EntitySet`-related functionality, it is a better idea to accept `TrustedEntityBorrow` over `EntityBorrow`. Furthermore, as #9367 points out, abstracting over returning `Entity` from pointers/structs that contain it can skip some ergonomic friction. On top of that, there are aspects of #18319 and #18408 that are relevant to naming: We've run into the issue that relying on a type default can switch generic order. This is livable in some contexts, but unacceptable in others. To remedy that, we'd need to switch to a type alias approach: The "defaulted" `Entity` case becomes a `UniqueEntity*`/`Entity*Map`/`Entity*Set` alias, and the base type receives a more general name. `TrustedEntityBorrow` does not mesh clearly with sensible base type names. ## Solution Replace any `EntityBorrow` bounds with `TrustedEntityBorrow`. + Rename them as such: `EntityBorrow` -> `ContainsEntity` `TrustedEntityBorrow` -> `EntityEquivalent` For `EntityBorrow` we produce a change in meaning; We designate it for types that aren't necessarily strict wrappers around `Entity` or some pointer to `Entity`, but rather any of the myriad of types that contain a single associated `Entity`. This pattern can already be seen in the common `entity`/`id` methods across the engine. We do not mean for `ContainsEntity` to be a trait that abstracts input API (like how `AsRef<T>` is often used, f.e.), because eliding `entity()` would be too implicit in the general case. We prefix "Contains" to match the intuition of a struct with an `Entity` field, like some contain a `length` or `capacity`. It gives the impression of structure, which avoids the implication of a relationship to the `ECS`. `HasEntity` f.e. could be interpreted as "a currently live entity", As an input trait for APIs like #9367 envisioned, `TrustedEntityBorrow` is a better fit, because it *does* restrict itself to strict wrappers and pointers. Which is why we replace any `EntityBorrow`/`ContainsEntity` bounds with `TrustedEntityBorrow`/`EntityEquivalent`. Here, the name `EntityEquivalent` is a lot closer to its actual meaning, which is "A type that is both equivalent to an `Entity`, and forms the same total order when compared". Prior art for this is the [`Equivalent`](https://docs.rs/hashbrown/latest/hashbrown/trait.Equivalent.html) trait in `hashbrown`, which utilizes both `Borrow` and `Eq` for its one blanket impl! Given that we lose the `Borrow` moniker, and `Equivalent` can carry various meanings, we expand on the safety comment of `EntityEquivalent` somewhat. That should help prevent the confusion we saw in [#18408](#18408 (comment)). The new name meshes a lot better with the type aliasing approach in #18408, by aligning with the base name `EntityEquivalentHashMap`. For a consistent scheme among all set types, we can use this scheme for the `UniqueEntity*` wrapper types as well! This allows us to undo the switched generic order that was introduced to `UniqueEntityArray` by its `Entity` default. Even without the type aliases, I think these renames are worth doing! ## Migration Guide Any use of `EntityBorrow` becomes `ContainsEntity`. Any use of `TrustedEntityBorrow` becomes `EntityEquivalent`.
…alent (#18470) # Objective Fixes #9367. Yet another follow-up to #16547. These traits were initially based on `Borrow<Entity>` because that trait was what they were replacing, and it felt close enough in meaning. However, they ultimately don't quite match: `borrow` always returns references, whereas `EntityBorrow` always returns a plain `Entity`. Additionally, `EntityBorrow` can imply that we are borrowing an `Entity` from the ECS, which is not what it does. Due to its safety contract, `TrustedEntityBorrow` is important an important and widely used trait for `EntitySet` functionality. In contrast, the safe `EntityBorrow` does not see much use, because even outside of `EntitySet`-related functionality, it is a better idea to accept `TrustedEntityBorrow` over `EntityBorrow`. Furthermore, as #9367 points out, abstracting over returning `Entity` from pointers/structs that contain it can skip some ergonomic friction. On top of that, there are aspects of #18319 and #18408 that are relevant to naming: We've run into the issue that relying on a type default can switch generic order. This is livable in some contexts, but unacceptable in others. To remedy that, we'd need to switch to a type alias approach: The "defaulted" `Entity` case becomes a `UniqueEntity*`/`Entity*Map`/`Entity*Set` alias, and the base type receives a more general name. `TrustedEntityBorrow` does not mesh clearly with sensible base type names. ## Solution Replace any `EntityBorrow` bounds with `TrustedEntityBorrow`. + Rename them as such: `EntityBorrow` -> `ContainsEntity` `TrustedEntityBorrow` -> `EntityEquivalent` For `EntityBorrow` we produce a change in meaning; We designate it for types that aren't necessarily strict wrappers around `Entity` or some pointer to `Entity`, but rather any of the myriad of types that contain a single associated `Entity`. This pattern can already be seen in the common `entity`/`id` methods across the engine. We do not mean for `ContainsEntity` to be a trait that abstracts input API (like how `AsRef<T>` is often used, f.e.), because eliding `entity()` would be too implicit in the general case. We prefix "Contains" to match the intuition of a struct with an `Entity` field, like some contain a `length` or `capacity`. It gives the impression of structure, which avoids the implication of a relationship to the `ECS`. `HasEntity` f.e. could be interpreted as "a currently live entity", As an input trait for APIs like #9367 envisioned, `TrustedEntityBorrow` is a better fit, because it *does* restrict itself to strict wrappers and pointers. Which is why we replace any `EntityBorrow`/`ContainsEntity` bounds with `TrustedEntityBorrow`/`EntityEquivalent`. Here, the name `EntityEquivalent` is a lot closer to its actual meaning, which is "A type that is both equivalent to an `Entity`, and forms the same total order when compared". Prior art for this is the [`Equivalent`](https://docs.rs/hashbrown/latest/hashbrown/trait.Equivalent.html) trait in `hashbrown`, which utilizes both `Borrow` and `Eq` for its one blanket impl! Given that we lose the `Borrow` moniker, and `Equivalent` can carry various meanings, we expand on the safety comment of `EntityEquivalent` somewhat. That should help prevent the confusion we saw in [#18408](#18408 (comment)). The new name meshes a lot better with the type aliasing approach in #18408, by aligning with the base name `EntityEquivalentHashMap`. For a consistent scheme among all set types, we can use this scheme for the `UniqueEntity*` wrapper types as well! This allows us to undo the switched generic order that was introduced to `UniqueEntityArray` by its `Entity` default. Even without the type aliases, I think these renames are worth doing! ## Migration Guide Any use of `EntityBorrow` becomes `ContainsEntity`. Any use of `TrustedEntityBorrow` becomes `EntityEquivalent`.
# Objective Newest installment of the #16547 series. In #18319 we introduced `Entity` defaults to accomodate the most common use case for these types, however that resulted in the switch of the `T` and `N` generics of `UniqueEntityArray`. Swapping generics might be somewhat acceptable for `UniqueEntityArray`, it is not at all acceptable for map and set types, which we would make generic over `T: EntityEquivalent` in #18408. Leaving these defaults in place would result in a glaring inconsistency between these set collections and the others. Additionally, the current standard in the engine is for "entity" to mean `Entity`. APIs could be changed to accept `EntityEquivalent`, however that is a separate and contentious discussion. ## Solution Name these set collections `UniqueEntityEquivalent*`, and retain the `UniqueEntity*` name for an alias of the `Entity` case. While more verbose, this allows for all generics to be in proper order, full consistency between all set types*, and the "entity" name to be restricted to `Entity`. On top of that, `UniqueEntity*` now always have 1 generic less, when previously this was not enforced for the default case. *`UniqueEntityIter<I: Iterator<T: EntityEquivalent>>` is the sole exception to this. Aliases are unable to enforce bounds (`lazy_type_alias` is needed for this), so for this type, doing this split would be a mere suggestion, and in no way enforced. Iterator types are rarely ever named, and this specific one is intended to be aliased when it sees more use, like we do for the corresponding set collection iterators. Furthermore, the `EntityEquivalent` precursor `Borrow<Entity>` was used exactly because of such iterator bounds! Because of that, we leave it as is. While no migration guide for 0.15 users, for those that upgrade from main: `UniqueEntityVec<T>` -> `UniqueEntityEquivalentVec<T>` `UniqueEntitySlice<T>` -> `UniqueEntityEquivalentSlice<T>` `UniqueEntityArray<N, T>` -> `UniqueEntityEquivalentArray<T, N>`
# Objective Newest installment of the #16547 series. In #18319 we introduced `Entity` defaults to accomodate the most common use case for these types, however that resulted in the switch of the `T` and `N` generics of `UniqueEntityArray`. Swapping generics might be somewhat acceptable for `UniqueEntityArray`, it is not at all acceptable for map and set types, which we would make generic over `T: EntityEquivalent` in #18408. Leaving these defaults in place would result in a glaring inconsistency between these set collections and the others. Additionally, the current standard in the engine is for "entity" to mean `Entity`. APIs could be changed to accept `EntityEquivalent`, however that is a separate and contentious discussion. ## Solution Name these set collections `UniqueEntityEquivalent*`, and retain the `UniqueEntity*` name for an alias of the `Entity` case. While more verbose, this allows for all generics to be in proper order, full consistency between all set types*, and the "entity" name to be restricted to `Entity`. On top of that, `UniqueEntity*` now always have 1 generic less, when previously this was not enforced for the default case. *`UniqueEntityIter<I: Iterator<T: EntityEquivalent>>` is the sole exception to this. Aliases are unable to enforce bounds (`lazy_type_alias` is needed for this), so for this type, doing this split would be a mere suggestion, and in no way enforced. Iterator types are rarely ever named, and this specific one is intended to be aliased when it sees more use, like we do for the corresponding set collection iterators. Furthermore, the `EntityEquivalent` precursor `Borrow<Entity>` was used exactly because of such iterator bounds! Because of that, we leave it as is. While no migration guide for 0.15 users, for those that upgrade from main: `UniqueEntityVec<T>` -> `UniqueEntityEquivalentVec<T>` `UniqueEntitySlice<T>` -> `UniqueEntityEquivalentSlice<T>` `UniqueEntityArray<N, T>` -> `UniqueEntityEquivalentArray<T, N>`
Objective
In current Bevy, it is very inconvenient to mutably retrieve a user-provided list of entities more than one element at a time.
If the list contains any duplicate entities, we risk mutable aliasing.
Users of
Query::iter_many_mut
do not have access toIterator
trait, and thus miss out on common functionality, for instance collecting theirQueryManyIter
.We can circumvent this issue with validation, however that entails checking every entity against all others for inequality, or utilizing an
EntityHashSet
. Even if an entity list remains unchanged, this validation is/would have to be redone every time we wish to fetch with the list.This presents a lot of wasted work, as we often trivially know an entity list to be unique f.e.:
QueryIter
will fetch everyEntity
once and only once.As more things become entities – assets, components, queries – this issue will become more pronounced.
get_many
/many
/iter_many
/par_iter_many
-like functionality is all affected.Solution
The solution this PR proposes is to introduce functionality built around a new trait:
EntitySet
.The goal is to preserve the property of "uniqueness" in a list wherever possible, and then rely on it as a bound within new
*_many_unique
methods to avoid the need for validation.This is achieved using
Iterator
:EntitySet
is blanket implemented for anyT
that implementsIntoIterator<IntoIter: EntitySetIterator>
.EntitySetIterator
is the unsafe trait that actually guarantees an iterator to be "unique" via its safety contract.We define an "Iterator over unique entities" as: "No two entities returned by the iterator may compare equal."
For iterators that cannot return more than 1 element, this is trivially true.
Whether an iterator can satisfy this is up to the
EntitySetIterator
implementor to ensure, hence the unsafe.However, this is not yet a complete solution. Looking at the signature of
iter_many
, we find thatIntoIterator::Item
is notEntity
, but is instead bounded by theBorrow<Entity>
trait. That is because iteration without consuming the collection will often yield us references, not owned items.Borrow<Entity>
presents an issue: TheBorrow
docs state thatx = y
should equalx.borrow() = y.borrow()
, but unsafe cannot rely on this for soundness. We run into similar problems with other trait implementations of anyBorrow<Entity>
type:PartialEq
,Eq
,PartialOrd
,Ord
,Hash
,Clone
,Borrow
, andBorrowMut
.This PR solves this with the unsafe
TrustedEntityBorrow
trait:Any implementor promises that the behavior of the aforementioned traits matches that of the underlying entity.
While
Borrow<Entity>
was the inspiration, we use our own counterpart traitEntityBorrow
as the supertrait toTrustedEntityBorrow
, so we can circumvent the limitations of the existingBorrow<T>
blanket impls.All together, these traits allow us to implement
*_many_unique
functionality with a loneEntitySet
bound.EntitySetIterator
is implemented for all the std iterators and iterator adapters that guarantee or preserve uniqueness, so we can filter, skip, take, step, reverse, ... our unique entity iterators without worry!Sadly, current
HashSet
iterators do not carry the necessary type information with them to determine whether the sourceHashSet
produces logic errors; A maliciousHasher
could compromise aHashSet
.HashSet
iteration is generally discouraged in the first place, so we also exclude the set operation iterators, even though they do carry theHasher
type parameter.BTreeSet
implementsEntitySet
without any problems.If an iterator type cannot guarantee uniqueness at compile time, then a user can still attach
EntitySetIterator
to an individual instance of that type viaUniqueEntityIter::from_iterator_unchecked
.With this, custom types can use
UniqueEntityIter<I>
as theirIntoIterator::IntoIter
type, if necessary.This PR is focused on the base concept, and expansions on it are left for follow-up PRs. See "Potential Future Work" below.
Testing
Doctests on
iter_many_unique
/iter_many_unique_mut
+ 2 tests in entity_set.rs.Showcase
Changelog
EntityBorrow
,TrustedEntityBorrow
,EntitySet
andEntitySetIterator
traitsiter_many_unique
,iter_many_unique_mut
,iter_many_unique_unsafe
methods onQuery
iter_many_unique
,iter_many_unique_mut
,iter_many_unique_manual
anditer_many_unique_unchecked_manual
methods onQueryState
QueryManyUniqueIter
UniqueEntityIter
Migration Guide
Any custom type used as a
Borrow<Entity>
entity list item for aniter_many
method now has to implementEntityBorrow
instead. Any type that implementsBorrow<Entity>
can trivially implementEntityBorrow
.Potential Future Work
ToEntitySet
trait for converting any entity iterator into anEntitySetIterator
EntityIndexSet/Map
to tie in hashing withEntitySet
EntityIndexSetSlice/MapSlice
EntityIndexSet/Map
par_iter_many_unique_mut
for parallel mutable iterationpar_iter_many
UniqueEntityVec
to store entity setsUniqueEntitySlice
sUniqueEntityVec
UniqueEntityArray
sUniqueEntitySlice
get_many_unique
/many_unique
methodsUniqueEntityArray
World::entity_unique
to matchWorld::entity
methodsget_many_unique
/many_unique
TrustedEntityBorrow
for theEntityRef
familyUniqueEntityVec