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

[Merged by Bors] - Improve QueryIter size_hint hints #4244

Closed
wants to merge 3 commits into from

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented Mar 17, 2022

Objective

This fixes #1686.

size_hint can be useful even if a little niche. For example,
collect::<Vec<_>>() uses the size_hint of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

Solution

To this effect I made the following changes:

  • Add a IS_ARCHETYPAL associated constant to the Fetch trait,
    this constant tells us when it is safe to assume that the Fetch
    relies exclusively on archetypes to filter queried entities
  • Add IS_ARCHETYPAL to all the implementations of Fetch
  • Use that constant in QueryIter::size_hint to provide a more useful

Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom Fetch, it means they have to add this
associated constant to their implementation. Either true if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
false for when it does.

@github-actions github-actions bot added the S-Needs-Triage This issue needs to be labelled label Mar 17, 2022
@MinerSebas
Copy link
Contributor

The size_hint of the QueryCombinationIter should also be updated (https://github.com/nicopap/bevy/blob/filter-size-hint/crates/bevy_ecs/src/query/iter.rs#L331)

The ExactSizeIterator Implementation (https://github.com/nicopap/bevy/blob/filter-size-hint/crates/bevy_ecs/src/query/iter.rs#L335-L351) also needs to be updated.

@nicopap
Copy link
Contributor Author

nicopap commented Mar 17, 2022

The size_hint of the QueryCombinationIter should also be updated (https://github.com/nicopap/bevy/blob/filter-size-hint/crates/bevy_ecs/src/query/iter.rs#L331)

Technically it doesn't need to (I don't see an ExactSizeIterator for QueryCombinationIter), but I can add that functionality. I might look into that tomorrow if I have time.

The ExactSizeIterator Implementation (https://github.com/nicopap/bevy/blob/filter-size-hint/crates/bevy_ecs/src/query/iter.rs#L335-L351) also needs to be updated.

It doesn't make sense to check in ExactSizeIterator. Since the trait iterator has an exact length. You can't do a min_size = if {... since ExactSizeIterator::len is a unique value and not a tuple like Iterator::size_hint. The code doesn't change, it's still the same.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Code-Quality A section of code that is hard to understand or change and removed S-Needs-Triage This issue needs to be labelled labels Mar 17, 2022
Copy link
Member

@alice-i-cecile alice-i-cecile left a comment

Choose a reason for hiding this comment

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

Code LGTM. I'd be happy to get the combinations work done at the same time, but I won't block on it. The IS_ARCHETYPAL associated const is solid: I suspect we'll run into more uses for this later.

@nicopap
Copy link
Contributor Author

nicopap commented Mar 18, 2022

Perfect. I think I can add fairly trivially the combination code, and I'll also remove the outdated references to #1686 in the comments.

@nicopap
Copy link
Contributor Author

nicopap commented Mar 18, 2022

uhh, the tests assumes minimal size hint is 0, I'll update them.

@mockersf
Copy link
Member

looks good once the tests are fixed 👍

@nicopap
Copy link
Contributor Author

nicopap commented Mar 19, 2022

Should be good to go now. I updated the tests so that they don't inspect the min size of the QueryCombinatorIter. Everything passes.

@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Apr 26, 2022
## Objective

This fixes #1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@bors
Copy link
Contributor

bors bot commented Apr 26, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Apr 26, 2022
## Objective

This fixes #1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@bors
Copy link
Contributor

bors bot commented Apr 26, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Apr 26, 2022
## Objective

This fixes #1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@bors
Copy link
Contributor

bors bot commented Apr 26, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Apr 26, 2022
## Objective

This fixes #1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@bors
Copy link
Contributor

bors bot commented Apr 26, 2022

Build failed:

@alice-i-cecile
Copy link
Member

bors try

bors bot added a commit that referenced this pull request Apr 26, 2022
@bors
Copy link
Contributor

bors bot commented Apr 26, 2022

try

Build failed:

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Apr 26, 2022

@nicopap looks like you've got some failing tests now. Could you rebase and fix that up?

@nicopap
Copy link
Contributor Author

nicopap commented Apr 27, 2022

Working on it. Aaah, having some computer related woes right now. I'll leave a thank to BORS for doing its job yet again!

nicopap added 3 commits April 27, 2022 09:44
This fixes bevyengine#1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful
  minima when it's possible to predict the exact size of the query.

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation.
@nicopap
Copy link
Contributor Author

nicopap commented Apr 27, 2022

I took the opportunity to rename some variables in the test to make the code clearer (the names are not clearer, but with rustfmt, the structure of the code is much easier to grasp at a quick glance).

@alice-i-cecile should be good to go 👍

@alice-i-cecile
Copy link
Member

bors r+

bors bot pushed a commit that referenced this pull request Apr 27, 2022
## Objective

This fixes #1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@bors bors bot changed the title Improve QueryIter size_hint hints [Merged by Bors] - Improve QueryIter size_hint hints Apr 27, 2022
@bors bors bot closed this Apr 27, 2022
exjam pushed a commit to exjam/bevy that referenced this pull request May 22, 2022
## Objective

This fixes bevyengine#1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
## Objective

This fixes bevyengine#1686.

`size_hint` can be useful even if a little niche. For example,
`collect::<Vec<_>>()` uses the `size_hint` of Iterator it collects from
to pre-allocate a memory slice large enough to not require re-allocating
when pushing all the elements of the iterator.

## Solution

To this effect I made the following changes:
* Add a `IS_ARCHETYPAL` associated constant to the `Fetch` trait,
  this constant tells us when it is safe to assume that the `Fetch`
  relies exclusively on archetypes to filter queried entities
* Add `IS_ARCHETYPAL` to all the implementations of `Fetch`
* Use that constant in `QueryIter::size_hint` to provide a more useful

## Migration guide

The new associated constant is an API breaking change. For the user,
if they implemented a custom `Fetch`, it means they have to add this
associated constant to their implementation. Either `true` if it doesn't limit
the number of entities returned in a query beyond that of archetypes, or
`false` for when it does.
@nicopap nicopap deleted the filter-size-hint branch September 5, 2023 15:43
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
Archived in project
Development

Successfully merging this pull request may close these issues.

QueryIter implements ExactSizeIterator, but doesnt provide an exact size_hint
4 participants