Skip to content
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

Query::get_many should not check for duplicates #17724

Merged
merged 1 commit into from
Feb 10, 2025

Conversation

chescock
Copy link
Contributor

@chescock chescock commented Feb 7, 2025

Objective

Restore the behavior of Query::get_many prior to #15858.

When passed duplicate Entitys, get_many is supposed to return results for all of them, since read-only queries don't alias. However, #15858 merged the implementation with get_many_mut and caused it to return QueryEntityError::AliasedMutability.

Solution

Introduce a new Query::get_many_readonly method that consumes the Query like get_many_inner, but that is constrained to D: ReadOnlyQueryData so that it can skip the aliasing check. Implement Query::get_many in terms of that new method. Add a test, and a comment explaining why it doesn't match the pattern of the other &self methods.

Copy link
Contributor

@Carter0 Carter0 left a comment

Choose a reason for hiding this comment

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

Code looks good and I like the test! Strange functionality though. Why would anyone want this?

@chescock
Copy link
Contributor Author

chescock commented Feb 7, 2025

Why would anyone want this?

I think the advantage of get_many over iter_many is that you can destructure an array with pattern matching, which you can't do with iterators. So you can write things like

if let Ok([player_data, enemy_data]) = query.get_many([player_id, enemy_id])

@Carter0
Copy link
Contributor

Carter0 commented Feb 7, 2025

Why would anyone want this?

I think the advantage of get_many over iter_many is that you can destructure an array with pattern matching, which you can't do with iterators. So you can write things like

if let Ok([player_data, enemy_data]) = query.get_many([player_id, enemy_id])

Yeah, but wouldn't you always want to check for duplicates? Like having ([player_id, player_id]) would be weird even if you can't alter the player data cause it's read-only.

@chescock
Copy link
Contributor Author

chescock commented Feb 7, 2025

Yeah, but wouldn't you always want to check for duplicates? Like having ([player_id, player_id]) would be weird even if you can't alter the player data cause it's read-only.

I think the goal there is just performance. Doing the check isn't free. We need it for mutable data for soundness, but for read-only data we can skip it.

But I haven't actually used this method myself, so I may not be the best person to justify it :).

@Carter0
Copy link
Contributor

Carter0 commented Feb 7, 2025

Yeah, but wouldn't you always want to check for duplicates? Like having ([player_id, player_id]) would be weird even if you can't alter the player data cause it's read-only.

I think the goal there is just performance. Doing the check isn't free. We need it for mutable data for soundness, but for read-only data we can skip it.

But I haven't actually used this method myself, so I may not be the best person to justify it :).

yoki doki makes sense

@ickshonpe ickshonpe added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 8, 2025
@chescock chescock added S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it and removed S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Feb 10, 2025
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Feb 10, 2025
Merged via the queue into bevyengine:main with commit c34a2c2 Feb 10, 2025
33 checks passed
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-Code-Quality A section of code that is hard to understand or change S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants