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

Implement ExactSizeIterator for queries that are only filtered by With and Without #3142

Closed
alice-i-cecile opened this issue Nov 17, 2021 · 3 comments
Labels
A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!

Comments

@alice-i-cecile
Copy link
Member

alice-i-cecile commented Nov 17, 2021

What problem does this solve or what need does it fill?

The ExactSizeIterator is useful on queries for learning the exact length of queries.

It's currently implemented for unfiltered queries, but not filtered queries.

What solution would you like?

Separate query filters into "entity-level filters" (Added, Changed) and "archetype-level filters" (With, Without).

Implement ArchetypeFilter for filter tuples that only have archetype-level filters. Do the same for Or.

Implement ExactSizeIterator for queries that only contain ArchetypeFilter filters.

What alternative(s) have you considered?

Clone the query and then count it slowly :(

Use With and Without in the first Query type parameter and discard the bools returned 😱

Additional context

We can't comfortably do this for all filtered queries, as the length of a query that includes Changed etc. is slow to compute.

Idea credit to @Davier and @TheRawMeatball. I just write the tickets!

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Performance A change motivated by improving speed, memory usage or compile times C-Usability A simple quality-of-life change that makes Bevy easier to use labels Nov 17, 2021
@TheRawMeatball
Copy link
Member

I don't think we need something like DynamicFilter

@mockersf
Copy link
Member

Separate query filters into DynamicFilter (Added, Changed) and StaticFilter (With, Without).

Rather than using global terms like dynamic and static, could we think about more specific terms?

Maybe ArchetypeFilter for static, and StatusFilter for dynamic?

@alice-i-cecile
Copy link
Member Author

alice-i-cecile commented Nov 17, 2021

Rather than using global terms like dynamic and static, could we think about more specific terms?
Maybe ArchetypeFilter for static, and StatusFilter for dynamic?

Right, that's a valid point. ArchetypeFilter is very good. I think I would probably prefer EntityFilter over StatusFilter, to indicate that it's performing a O(n) filter over the retrieved entities (in contrast to over the archetypes) but I like StatusFilter better than DynamicFilter and would happily accept it.

Edited the initial message to reflect this improved terminology.

@alice-i-cecile alice-i-cecile added the S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it! label Dec 12, 2021
@bors bors bot closed this as completed in 6e50b24 Jun 29, 2022
james7132 pushed a commit to james7132/bevy that referenced this issue Jul 2, 2022
…Without) (bevyengine#5124)

# Objective

- Fixes bevyengine#3142

## Solution

- Done according to bevyengine#3142
- Created new marker trait `ArchetypeFilter`
- Implement said trait to:
  - `With<T>`
  - `Without<T>`
  - tuples containing only types that implement `ArchetypeFilter`, from 0 to 15 elements
  - `Or<T>` where T is a tuple as described previously
- Changed `ExactSizeIterator` impl to include a new generic that must implement `WorldQuery` and `ArchetypeFilter`
- Added new tests

---

## Changelog

### Added
- `Query`s with archetypal filters can now use `.iter().len()` to get the exact size of the iterator.
inodentry pushed a commit to IyesGames/bevy that referenced this issue Aug 8, 2022
…Without) (bevyengine#5124)

# Objective

- Fixes bevyengine#3142

## Solution

- Done according to bevyengine#3142
- Created new marker trait `ArchetypeFilter`
- Implement said trait to:
  - `With<T>`
  - `Without<T>`
  - tuples containing only types that implement `ArchetypeFilter`, from 0 to 15 elements
  - `Or<T>` where T is a tuple as described previously
- Changed `ExactSizeIterator` impl to include a new generic that must implement `WorldQuery` and `ArchetypeFilter`
- Added new tests

---

## Changelog

### Added
- `Query`s with archetypal filters can now use `.iter().len()` to get the exact size of the iterator.
james7132 pushed a commit to james7132/bevy that referenced this issue Oct 28, 2022
…Without) (bevyengine#5124)

# Objective

- Fixes bevyengine#3142

## Solution

- Done according to bevyengine#3142
- Created new marker trait `ArchetypeFilter`
- Implement said trait to:
  - `With<T>`
  - `Without<T>`
  - tuples containing only types that implement `ArchetypeFilter`, from 0 to 15 elements
  - `Or<T>` where T is a tuple as described previously
- Changed `ExactSizeIterator` impl to include a new generic that must implement `WorldQuery` and `ArchetypeFilter`
- Added new tests

---

## Changelog

### Added
- `Query`s with archetypal filters can now use `.iter().len()` to get the exact size of the iterator.
ItsDoot pushed a commit to ItsDoot/bevy that referenced this issue Feb 1, 2023
…Without) (bevyengine#5124)

# Objective

- Fixes bevyengine#3142

## Solution

- Done according to bevyengine#3142
- Created new marker trait `ArchetypeFilter`
- Implement said trait to:
  - `With<T>`
  - `Without<T>`
  - tuples containing only types that implement `ArchetypeFilter`, from 0 to 15 elements
  - `Or<T>` where T is a tuple as described previously
- Changed `ExactSizeIterator` impl to include a new generic that must implement `WorldQuery` and `ArchetypeFilter`
- Added new tests

---

## Changelog

### Added
- `Query`s with archetypal filters can now use `.iter().len()` to get the exact size of the iterator.
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-Performance A change motivated by improving speed, memory usage or compile times C-Usability A simple quality-of-life change that makes Bevy easier to use S-Ready-For-Implementation This issue is ready for an implementation PR. Go for it!
Projects
None yet
3 participants