Skip to content

Add query reborrowing#14690

Merged
alice-i-cecile merged 8 commits intobevyengine:mainfrom
SkiFire13:query-reborrow
Aug 15, 2024
Merged

Add query reborrowing#14690
alice-i-cecile merged 8 commits intobevyengine:mainfrom
SkiFire13:query-reborrow

Conversation

@SkiFire13
Copy link
Contributor

@SkiFire13 SkiFire13 commented Aug 10, 2024

Objective

Solution

  • Add a way for reborrowing queries, that is going from a &'a mut Query<'w, 's, D, F> to a Query<'a, 's, D, F>:
    • this is safe because the original query will be borrowed while the new query exists and thus no aliased access can happen;
    • it's basically the equivalent of going from &'short mut &'long mut T to &'short mut T the the compiler automatically implements.
  • Add a way for getting the remainder of a query iterator:
    • this is interesting also because the original iterator keeps its position, which was not possible before;
    • this in turn requires a way to reborrow query fetches, which I had to add to WorldQuery.

Showcase

  • You can now reborrow a Query, getting an equivalent Query with a shorter lifetime. Previously this was possible for read-only queries by using Query::to_readonly, now it's possible for mutable queries too;
  • You can now separately iterate over the remainder of QueryIter.

Migration Guide

  • WorldQuery now has an additional shrink_fetch method you have to implement if you were implementing WorldQuery manually.

@IQuick143 IQuick143 added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use S-Needs-Review Needs reviewer attention (from anyone!) to move forward labels Aug 10, 2024
Copy link
Contributor

@re0312 re0312 left a comment

Choose a reason for hiding this comment

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

I really like this . and from a performance perspective, except for some specific fetch , it's almost zero cost.
I did a simple bench and it was nearly 2X faster than our QueryCombinationIter<2>

@rparrett rparrett 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 Aug 13, 2024
@alice-i-cecile alice-i-cecile added this pull request to the merge queue Aug 15, 2024
Merged via the queue into bevyengine:main with commit e9e9e5e Aug 15, 2024
@SkiFire13 SkiFire13 deleted the query-reborrow branch August 15, 2024 18:01
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 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.

7 participants