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] - Add ExactSizeIterator implementation for QueryCombinatonIter #5148

Closed

Conversation

nicopap
Copy link
Contributor

@nicopap nicopap commented Jun 30, 2022

Following #5124 I decided to add the ExactSizeIterator impl for QueryCombinationIter.

Also:

  • Clean up the tests for size_hint and len for both the normal QueryIter and QueryCombinationIter.
  • Add tests to QueryCombinationIter when it shouldn't be ExactSizeIterator

Changelog

  • Added ExactSizeIterator implementation for QueryCombinatonIter

@nicopap nicopap force-pushed the exact-size-query-combination branch from 4674c1c to 9e73a7e Compare June 30, 2022 11:19
// binomial coefficient: (n ; k) = n! / k!(n-k)! = (n*n-1*...*n-k+1) / k!
// See https://en.wikipedia.org/wiki/Binomial_coefficient
// See https://blog.plover.com/math/choose.html for implementation
// It was chosen to reduce overflow potential.
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Note: It reduces overflow potential at least compared to the previous solution which computed n! and k!(n-k)!. This still can overflow even when the result is not an overflow. An example is provided in https://blog.plover.com/math/choose-2.html

It's also probably slower than the previous solution, because it does k div and k checked_mul, while the previous one did 1 div, k mul and k checked_mul.

assert_eq!(a_query.iter_combinations::<5>(w).count(), 0);
assert_eq!(a_query.iter_combinations::<5>(w).size_hint().1, Some(0));
assert_eq!(a_query.iter_combinations::<128>(w).count(), 0);
assert_eq!(a_query.iter_combinations::<128>(w).size_hint().1, Some(0));
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 moved those tests to query_filtered_exactsizeiterator_len, so that they can be deduplicated and tested on more stuff.

@alice-i-cecile alice-i-cecile added A-ECS Entities, components, systems, and events C-Usability A targeted quality-of-life change that makes Bevy easier to use labels Jun 30, 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.

Nice work; this LGTM.

@alice-i-cecile alice-i-cecile added this to the Bevy 0.8 milestone Jun 30, 2022
@alice-i-cecile
Copy link
Member

Check the timings for your tests; they may be making miri take excessively long.

bors retry

@nicopap nicopap force-pushed the exact-size-query-combination branch from 9e73a7e to 8460fc4 Compare June 30, 2022 16:00
@alice-i-cecile
Copy link
Member

Despite this behavior being incorrect per #5149, I'd prefer to leave this behavior intact for consistency. That issue should be fixed all at once.

nicopap added a commit to nicopap/bevy that referenced this pull request Jul 5, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149 (although, if bevyengine#5148 is merged, bevyengine#5149 will re-open)

- bevyengine#5149
- bevyengine#5148
@alice-i-cecile
Copy link
Member

@bevyengine/ecs-team can I get a review please? I'd like to merge this, then fix it all together in #5214.

nicopap added a commit to nicopap/bevy that referenced this pull request Jul 5, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149 (although, if bevyengine#5148 is merged, bevyengine#5149 will re-open)

- bevyengine#5149
- bevyengine#5148
nicopap added a commit to nicopap/bevy that referenced this pull request Jul 5, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
nicopap added a commit to nicopap/bevy that referenced this pull request Jul 5, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
Copy link
Contributor

@hymm hymm 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. My only question would be is #5214 guaranteed to land or what is the plan if we don't fix #5149?

@alice-i-cecile alice-i-cecile added the S-Ready-For-Final-Review This PR has been approved by the community. It's ready for a maintainer to consider merging it label Jul 13, 2022
@alice-i-cecile
Copy link
Member

IMO #5214 is basically guaranteed to land; the performance overhead doesn't look nearly as bad as I expected. Merging now for consistency.

bors r+

bors bot pushed a commit that referenced this pull request Jul 13, 2022
Following #5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
@bors
Copy link
Contributor

bors bot commented Jul 13, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Jul 13, 2022
Following #5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
@bors
Copy link
Contributor

bors bot commented Jul 13, 2022

Build failed (retrying...):

bors bot pushed a commit that referenced this pull request Jul 13, 2022
Following #5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
@bors
Copy link
Contributor

bors bot commented Jul 13, 2022

Build failed:

@nicopap nicopap force-pushed the exact-size-query-combination branch from 8460fc4 to 2927014 Compare July 13, 2022 15:49
@alice-i-cecile
Copy link
Member

bors retry

bors bot pushed a commit that referenced this pull request Jul 13, 2022
Following #5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
@nicopap nicopap force-pushed the exact-size-query-combination branch from 2927014 to 865c34e Compare July 13, 2022 16:00
@bors
Copy link
Contributor

bors bot commented Jul 13, 2022

Canceled.

@alice-i-cecile
Copy link
Member

bors retry

bors bot pushed a commit that referenced this pull request Jul 13, 2022
Following #5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
@bors bors bot changed the title Add ExactSizeIterator implementation for QueryCombinatonIter [Merged by Bors] - Add ExactSizeIterator implementation for QueryCombinatonIter Jul 13, 2022
@bors bors bot closed this Jul 13, 2022
nicopap added a commit to nicopap/bevy that referenced this pull request Jul 13, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
nicopap added a commit to nicopap/bevy that referenced this pull request Jul 19, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
nicopap added a commit to nicopap/bevy that referenced this pull request Jul 22, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
inodentry pushed a commit to IyesGames/bevy that referenced this pull request Aug 8, 2022
…ine#5148)

Following bevyengine#5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
nicopap added a commit to nicopap/bevy that referenced this pull request Sep 1, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
james7132 pushed a commit to james7132/bevy that referenced this pull request Oct 28, 2022
…ine#5148)

Following bevyengine#5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
nicopap added a commit to nicopap/bevy that referenced this pull request Nov 1, 2022
Instead of returning the total count of elements in the `QueryIter` in
`size_hint`, we return the count of remaining elements in it. This
Fixes bevyengine#5149. This is also true of `QueryCombinationIter`.

- bevyengine#5149
- bevyengine#5148
bors bot pushed a commit that referenced this pull request Nov 21, 2022
…er (#5214)

# Objective

Fix #5149

## Solution

Instead of returning the **total count** of elements in the `QueryIter` in
`size_hint`, we return the **count of remaining elements**. This
Fixes #5149 even when #5148 gets merged.

- #5149
- #5148

---

## Changelog

- Fix partially consumed `QueryIter` and `QueryCombinationIter` having invalid `size_hint`


Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
cart pushed a commit that referenced this pull request Nov 30, 2022
…er (#5214)

# Objective

Fix #5149

## Solution

Instead of returning the **total count** of elements in the `QueryIter` in
`size_hint`, we return the **count of remaining elements**. This
Fixes #5149 even when #5148 gets merged.

- #5149
- #5148

---

## Changelog

- Fix partially consumed `QueryIter` and `QueryCombinationIter` having invalid `size_hint`


Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…ine#5148)

Following bevyengine#5124 I decided to add the `ExactSizeIterator` impl for `QueryCombinationIter`.

Also:
- Clean up the tests for `size_hint` and `len` for both the normal `QueryIter` and `QueryCombinationIter`.
- Add tests to `QueryCombinationIter` when it shouldn't be `ExactSizeIterator`

---

## Changelog

- Added `ExactSizeIterator` implementation for `QueryCombinatonIter`
ItsDoot pushed a commit to ItsDoot/bevy that referenced this pull request Feb 1, 2023
…er (bevyengine#5214)

# Objective

Fix bevyengine#5149

## Solution

Instead of returning the **total count** of elements in the `QueryIter` in
`size_hint`, we return the **count of remaining elements**. This
Fixes bevyengine#5149 even when bevyengine#5148 gets merged.

- bevyengine#5149
- bevyengine#5148

---

## Changelog

- Fix partially consumed `QueryIter` and `QueryCombinationIter` having invalid `size_hint`


Co-authored-by: Nicola Papale <nicopap@users.noreply.github.com>
@nicopap nicopap deleted the exact-size-query-combination branch September 5, 2023 15:45
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.

3 participants