Repository navigation
Change QueryManyIter to return Result instead of only matched items - #25200
Conversation
|
I'm a little leery to add more methods to this effect: the compile time and API bloat costs are real, and iter_many_results is pretty niche. Definitely agree on the |
|
I wasn't initially planning to add sorting to @alice-i-cecile do you also want me to remove the Or do you want to go back to designing a different solution altogether? I mean the problem that I am trying to solve for myself is just that it can be pretty hard to notice and debug when an entity is silently skipped via |
|
What if we just changed the call signature of |
|
Sure, I could change I feel like ergonomics would be slightly worse, but that could be easily improved with additional |
|
Yes, that sounds like a lovely plan. I would prefer to make this robust, with sugar for making it easy, rather than duplicating the API or making it impossible to handle this robustly. |
f56df93 to
d7721e7
Compare
Query::iter_many_results and _mut, _inner, _manual variantsQueryManyIter to return Result instead of only matched items
d7721e7 to
f593b61
Compare
|
According to our exchange above, I've reworked the PR as I saw fit. But happy to change it further as needed. @alice-i-cecile would you like to take another look? |
alice-i-cecile
left a comment
There was a problem hiding this comment.
That's a much healthier looking diff now. Remember to add a migration guide :)
|
I added a migration guide now. I also deleted 3 code comments that didn't seem fitting or that I forgot to delete. Is there anything else needed @alice-i-cecile ? |
alice-i-cecile
left a comment
There was a problem hiding this comment.
Yeah okay, I'm happy with this now :) I'll try and wrangle a second reviewer for you.
piedoom
left a comment
There was a problem hiding this comment.
I'm still very new to reading Bevy internals so take with a grain of salt :) This looks good though, I like being able to get errors back.
|
|
||
| // 0.20 | ||
| fn my_system(entity_list: Res<MyEntityList>, my_component_query: Query<&MyComponent>) { | ||
| my_component_query.par_iter_many(entity_list.iter()).for_each(|my_component| { |
There was a problem hiding this comment.
random, and not sure if this is even possible but i wonder if a filter_map could work with the iterator chains better than the let Ok(..) else { }
There was a problem hiding this comment.
Since QueryParManyIter does not actually implement the Iterator trait, it is not possible to call Iterator::filter_map.
| /// } | ||
| /// } | ||
| /// # bevy_ecs::system::assert_is_system(system); | ||
| /// |
There was a problem hiding this comment.
I wonder if this should be the first example listed (since I believe it is the most usual behavior if users don't care about catching errors)
There was a problem hiding this comment.
Makes sense. I changed the order to matching_system, unwrapping_system then error_system, so that the very similar looking matching_system and unwrapping_system are still next to each other.
|
Thank you both for the reviews. @piedoom I think I addressed all your comments. Please check. |
…ms (bevyengine#25200) # Objective Fixes bevyengine#24607 ## Solution The original solution created a modified copy of `QueryManyIter`, but that would induce some API bloat and compile times. Instead, this PR now changes the signature of `QueryManyIter` to iterate over `Result<D::Item<'w, 's>, QueryEntityError>` instead of only matched `D::Item<'w, 's>`. To make the common cases of iterating only matched entities or unwrapping the errors easy, there are new `.matched()` and `.unwrapped()` methods. Similar changes have also been made to related unique and parallel iterators. ## Testing - Existing unit and documentation test have been adjusted. - You can run the tests with `cargo test -p bevy_ecs` and `cargo test -p bevy_ecs --doc` --- ## Showcase This makes it possible to perform custom error handling when an entity does not match, which was not possible with `QueryManyIter`. For example logging, returning errors or a panic (unwrap) when iterating many entities are all now possible. <details> <summary>Click to view showcase (Example: return `BevyError`)</summary> ```rust use bevy_ecs::prelude::*; #[derive(Component)] struct Counter { value: i32 } // A component containing an entity list. #[derive(Component)] struct Friends { list: Vec<Entity>, } fn system( friends_query: Query<&Friends>, counter_query: Query<&Counter>, ) -> Result<(), BevyError> { for friends in &friends_query { for counter_result in counter_query.iter_many(&friends.list) { let counter = counter_result?; println!("Friend's counter: {}", counter.value); } } Ok(()) } ``` </details>
# Objective Fixes bevyengine#25840. ## Solution bevyengine#25200 added wrappers around `fetch_next_aliased_unchecked_internal`, which is marked as `#[inline(always)]`. The new wrappers aren't marked as `#[inline(always)]`, so the function stopped getting inlined. ## Testing Ran a couple benchmarks on my laptop, times in μs. | Benchmark | Before bevyengine#25200 | main | This PR | |---|---|---|---| | propagate_parent_transforms/full_recompute_roots_changed | 300.6 ± 18.5 | 389.8 ± 14.7 | 308.4 ± 21.3 | | propagate_parent_transforms/full_recompute_leaves_changed | 482.0 ± 11.5 | 575.7 ± 10.7 | 488.5 ± 8.5 | | transform_pipeline/localized_updates_static_optimized | 428.5 ± 12.6 | 516.1 ± 16.4 | 407.4 ± 17.0 | | transform_pipeline/localized_updates_no_static_optimization | 303.4 ± 10.1 | 395.9 ± 14.1 | 281.9 ± 14.2 | --- I used a LLM to find the cause of the regression. I then read the bevyengine#25200 diff to verify its claims and ran the benchmarks.
# Objective Fixes #25840. ## Solution #25200 added wrappers around `fetch_next_aliased_unchecked_internal`, which is marked as `#[inline(always)]`. The new wrappers aren't marked as `#[inline(always)]`, so the function stopped getting inlined. ## Testing Ran a couple benchmarks on my laptop, times in μs. | Benchmark | Before #25200 | main | This PR | |---|---|---|---| | propagate_parent_transforms/full_recompute_roots_changed | 300.6 ± 18.5 | 389.8 ± 14.7 | 308.4 ± 21.3 | | propagate_parent_transforms/full_recompute_leaves_changed | 482.0 ± 11.5 | 575.7 ± 10.7 | 488.5 ± 8.5 | | transform_pipeline/localized_updates_static_optimized | 428.5 ± 12.6 | 516.1 ± 16.4 | 407.4 ± 17.0 | | transform_pipeline/localized_updates_no_static_optimization | 303.4 ± 10.1 | 395.9 ± 14.1 | 281.9 ± 14.2 | --- I used a LLM to find the cause of the regression. I then read the #25200 diff to verify its claims and ran the benchmarks.
|
Do we have any benchmark coverage for these functions? |
Objective
Fixes #24607
Solution
The original solution created a modified copy of
QueryManyIter, but that would induce some API bloat and compile times.Instead, this PR now changes the signature of
QueryManyIterto iterate overResult<D::Item<'w, 's>, QueryEntityError>instead of only matchedD::Item<'w, 's>. To make the common cases of iterating only matched entities or unwrapping the errors easy, there are new.matched()and.unwrapped()methods. Similar changes have also been made to related unique and parallel iterators.Testing
cargo test -p bevy_ecsandcargo test -p bevy_ecs --docShowcase
This makes it possible to perform custom error handling when an entity does not match, which was not possible with
QueryManyIter.For example logging, returning errors or a panic (unwrap) when iterating many entities are all now possible.
Click to view showcase (Example: return `BevyError`)