Skip to content

add a QueryManyUniqueIter #13477

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

Closed
wants to merge 3 commits into from

Conversation

Victoronz
Copy link
Contributor

@Victoronz Victoronz commented May 22, 2024

Objective

Requires #13476.

Currently we cannot iterate mutably over QueryManyIter even if all entities we pass are unique.

Solution

Add a QueryManyUniqueIter wrapper around QueryManyIter for full Iterator trait iteration.
It can be constructed via either the dedup_entities or entities_all_unique method on QueryManyIter.
dedup_entities deduplicates the internal entity_iter, retaining order.
entities_all_unique takes self, checks for uniqueness, returns QueryManyUniqueIter on success, and QueryManyIter (Self) on failure.
Since QueryManyUniqueIter can iterate over mutable QueryData , it does not expose a fetch_next method.

Testing

The example/doc test shows the method in use, also making sure expect can be called on the return Result.

Changelog

Added dedup_entities and entities_all_unique methods on QueryManyIter.
Added QueryManyUniqueIter struct.

@Victoronz Victoronz force-pushed the entities_all_unique branch from f4f433e to cabaf73 Compare May 22, 2024 17:54
@Victoronz Victoronz added A-ECS Entities, components, systems, and events S-Blocked This cannot move forward until something else changes C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes labels May 22, 2024
github-merge-queue bot pushed a commit that referenced this pull request May 22, 2024
# Objective

The current query iterators cannot be used in positions with a `Debug`
bound.
F.e. when they are packaged in `Result` in the error position, `expect`
cannot be called on them.
Required for `QueryManyIter::entities_all_unique` in #13477.

## Solution

Add simple `Debug` impls that print the query iterator names.

## Changelog

`QueryIter`, `QueryManyIter`, `QueryCombinationIter`, and
`QuerySortedIter` now implement `Debug`.
@Victoronz Victoronz force-pushed the entities_all_unique branch from cabaf73 to 3c502c4 Compare May 22, 2024 19:33
@Victoronz Victoronz added S-Needs-Review Needs reviewer attention (from anyone!) to move forward and removed S-Blocked This cannot move forward until something else changes labels May 22, 2024
{
// Entities are guaranteed to be unique, so no lifetime shrinking needed for mutable iteration.
#[inline(always)]
fn fetch_next(&mut self) -> Option<D::Item<'w>> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This duplicates the code in QueryManyIter::fetch_next_aliased_unchecked(). Could you share the implementation by having QueryManyUniqueIter wrap a whole QueryManyIter, and then implement Iterator::next() by calling fetch_next_aliased_unchecked() on the inner QueryManyIter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, I was not sure whether the uniqueness property could simplify the next() call, so I kept them separate, but looking at it again, you're right, this can just be wrapper type! Good idea.

/// # schedule.run(&mut world);
/// ```
#[inline(always)]
pub fn entities_all_unique(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It might be useful to have methods to construct a QueryManyUniqueIter directly from a few iterator types that can already guarantee uniqueness, such as HashSet. That could let users skip the overhead of the uniqueness check.

It might also be useful to have a method that removes duplicates instead of returning Err, by doing something like

entity_iter: self
    .entity_iter
    .map(|e| *e.borrow())
    .collect::<EntityHashSet>()
    .into_iter(),

(Those could always be done in future PRs, of course.)

Copy link
Contributor Author

@Victoronz Victoronz Jul 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the methods to construct from unique iterators. What other source iterator types would you suggest?

Deduplication can already be done on the iterator itself with itertools, so isn't as important to have as the others. Still a nice to have though.
I wonder whether a sort + dedup would be faster than your HashSet version.
Edit: Actually just collecting into a BTreeSet might be simpler and faster. None of these retain input order however, that would be more expensive.

I think I'll make these separate follow-up PRs, since I have to keep these changes in line with #13443.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have added dedup_entities on QueryManyIter to this PR anyway.

Victoronz added 2 commits July 2, 2024 19:28
also adds:
a Debug impl
associated type bounds in the struct and impl definitions
a missing unsafe block in QueryManyIter
.get(location.archetype_id)
.debug_checked_unwrap();
let table = self.tables.get(location.table_id).debug_checked_unwrap();
let (archetype, table);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good cleanup, but could probably be a separate PR.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair, I'll put it in a different PR.

@Victoronz
Copy link
Contributor Author

I wonder whether it might be easier to have a direct Query::iter_many_unique method instead of these roundabout conversions on QueryManyIter.

@Victoronz Victoronz changed the title add entities_all_unique to QueryManyIter add a QueryManyUniqueIter Jul 2, 2024
@Victoronz
Copy link
Contributor Author

Victoronz commented Jul 13, 2024

I have come up with a more powerful, general solution, which I think I will put in a different PR. I'll keep this open as the simpler alternative.

This PR would also need to be updated with a TrustedEq: Eq trait, because even though we only use it for Entity, unsafe code must not rely on an Eq trait impl for correctness, because Eq itself does not contain a logical correctness guarantee.
That being said, this would be a simple unsafe marker trait impl on Entity stating the safety contract.

@Victoronz Victoronz added S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Jul 14, 2024
@Victoronz
Copy link
Contributor Author

This PR is now superseded by the concept of #16547, though the actual conversion API would be a follow-up to that PR.

@Victoronz
Copy link
Contributor Author

Victoronz commented Dec 20, 2024

#16547 has merged.

@Victoronz Victoronz closed this Dec 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use D-Modest A "normal" level of difficulty; suitable for simple features or challenging fixes S-Waiting-on-Author The author needs to make changes or address concerns before this can be merged
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants