Skip to content

Store QueryState in Query as a Cow and remove distinct QueryLens type #15848

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

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benches/benches/bevy_ecs/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ fn setup<T: Component + Default>(entity_count: u32) -> World {

// create a cached query in setup to avoid extra costs in each iter
fn generic_filter_query<F: QueryFilter>(world: &mut World) -> QueryState<Entity, F> {
world.query_filtered::<Entity, F>()
world.query_state_filtered::<Entity, F>()
}

fn generic_bench<P: Copy>(
Expand Down Expand Up @@ -135,7 +135,7 @@ fn all_changed_detection_generic<T: Component + Default + BenchModify>(
|| {
let mut world = setup::<T>(entity_count);
world.clear_trackers();
let mut query = world.query::<&mut T>();
let mut query = world.query_state::<&mut T>();
for mut component in query.iter_mut(&mut world) {
black_box(component.bench_modify());
}
Expand Down Expand Up @@ -185,7 +185,7 @@ fn few_changed_detection_generic<T: Component + Default + BenchModify>(
|| {
let mut world = setup::<T>(entity_count);
world.clear_trackers();
let mut query = world.query::<&mut T>();
let mut query = world.query_state::<&mut T>();
let mut to_modify: Vec<bevy_ecs::prelude::Mut<T>> =
query.iter_mut(&mut world).collect();
to_modify.shuffle(&mut deterministic_rand());
Expand Down Expand Up @@ -316,7 +316,7 @@ fn multiple_archetype_none_changed_detection_generic<T: Component + Default + Be
let mut world = World::new();
add_archetypes_entities::<T>(&mut world, archetype_count, entity_count);
world.clear_trackers();
let mut query = world.query::<(
let mut query = world.query_state::<(
Option<&mut Data<0>>,
Option<&mut Data<1>>,
Option<&mut Data<2>>,
Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'w> Benchmark<'w> {

create_entities!(world; A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

let query = world.query::<&mut Data>();
let query = world.query_state::<&mut Data>();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl<'w> Benchmark<'w> {

create_entities!(world; A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

let query = world.query::<&mut Data>();
let query = world.query_state::<&mut Data>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'w> Benchmark<'w> {
create_entities!(world; C70, C71, C72, C73, C74, C75, C76, C77, C78, C79);
create_entities!(world; C80, C81, C82, C83, C84, C85, C86, C87, C88, C89);
create_entities!(world; C90, C91, C92, C93, C94, C95, C96, C97, C98, C99);
let query = world.query::<&mut Data>();
let query = world.query_state::<&mut Data>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'w> Benchmark<'w> {

create_entities!(world; A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'w> Benchmark<'w> {
create_entities!(world; C70, C71, C72, C73, C74, C75, C76, C77, C78, C79);
create_entities!(world; C80, C81, C82, C83, C84, C85, C86, C87, C88, C89);
create_entities!(world; C90, C91, C92, C93, C94, C95, C96, C97, C98, C99);
let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl<'w> Benchmark<'w> {
create_entities!(world; C70, C71, C72, C73, C74, C75, C76, C77, C78, C79);
create_entities!(world; C80, C81, C82, C83, C84, C85, C86, C87, C88, C89);
create_entities!(world; C90, C91, C92, C93, C94, C95, C96, C97, C98, C99);
let query = world.query::<&mut Data>();
let query = world.query_state::<&mut Data>();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_frag_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'w> Benchmark<'w> {

create_entities!(world; A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'w> Benchmark<'w> {
create_entities!(world; C70, C71, C72, C73, C74, C75, C76, C77, C78, C79);
create_entities!(world; C80, C81, C82, C83, C84, C85, C86, C87, C88, C89);
create_entities!(world; C90, C91, C92, C93, C94, C95, C96, C97, C98, C99);
let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query::<(&Velocity, &mut Position)>();
let query = world.query_state::<(&Velocity, &mut Position)>();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_foreach.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query::<(&Velocity, &mut Position)>();
let query = world.query_state::<(&Velocity, &mut Position)>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ impl<'w> Benchmark<'w> {
world.entity_mut(e).despawn();
}

let query = world.query::<(&mut TableData, &SparseData)>();
let query = world.query_state::<(&mut TableData, &SparseData)>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query::<(&Velocity, &mut Position)>();
let query = world.query_state::<(&Velocity, &mut Position)>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query::<(&Velocity, &mut Position)>();
let query = world.query_state::<(&Velocity, &mut Position)>();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/iter_simple_wide.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl<'w> Benchmark<'w> {
.take(10_000),
);

let query = world.query();
let query = world.query_state();
Self(world, query)
}

Expand Down
2 changes: 1 addition & 1 deletion benches/benches/bevy_ecs/iteration/par_iter_simple.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl<'w> Benchmark<'w> {
insert_if_bit_enabled::<15>(&mut e, i);
}

let query = world.query::<(&Velocity, &mut Position)>();
let query = world.query_state::<(&Velocity, &mut Position)>();
Self(world, query)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl<'w> Benchmark<'w> {
world.entity_mut(e).despawn();
}

let query = world.query::<(&mut TableData, &SparseData)>();
let query = world.query_state::<(&mut TableData, &SparseData)>();
Self(world, query)
}

Expand Down
16 changes: 8 additions & 8 deletions benches/benches/bevy_ecs/world/world_get.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();
let mut query = world.query_state::<&Table>();

bencher.iter(|| {
for i in 0..entity_count {
Expand All @@ -118,7 +118,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
WideTable<4>,
WideTable<5>,
)>(entity_count);
let mut query = world.query::<(
let mut query = world.query_state::<(
&WideTable<0>,
&WideTable<1>,
&WideTable<2>,
Expand All @@ -136,7 +136,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();
let mut query = world.query_state::<&Sparse>();

bencher.iter(|| {
for i in 0..entity_count {
Expand All @@ -156,7 +156,7 @@ pub fn world_query_get(criterion: &mut Criterion) {
WideSparse<4>,
WideSparse<5>,
)>(entity_count);
let mut query = world.query::<(
let mut query = world.query_state::<(
&WideSparse<0>,
&WideSparse<1>,
&WideSparse<2>,
Expand Down Expand Up @@ -186,7 +186,7 @@ pub fn world_query_iter(criterion: &mut Criterion) {
for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();
let mut query = world.query_state::<&Table>();

bencher.iter(|| {
let mut count = 0;
Expand All @@ -200,7 +200,7 @@ pub fn world_query_iter(criterion: &mut Criterion) {
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();
let mut query = world.query_state::<&Sparse>();

bencher.iter(|| {
let mut count = 0;
Expand All @@ -225,7 +225,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {
for entity_count in RANGE.map(|i| i * 10_000) {
group.bench_function(format!("{}_entities_table", entity_count), |bencher| {
let mut world = setup::<Table>(entity_count);
let mut query = world.query::<&Table>();
let mut query = world.query_state::<&Table>();

bencher.iter(|| {
let mut count = 0;
Expand All @@ -239,7 +239,7 @@ pub fn world_query_for_each(criterion: &mut Criterion) {
});
group.bench_function(format!("{}_entities_sparse", entity_count), |bencher| {
let mut world = setup::<Sparse>(entity_count);
let mut query = world.query::<&Sparse>();
let mut query = world.query_state::<&Sparse>();

bencher.iter(|| {
let mut count = 0;
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core/src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ mod tests {
let e1 = world.spawn_empty().id();
let name = Name::new("MyName");
let e2 = world.spawn(name.clone()).id();
let mut query = world.query::<NameOrEntity>();
let mut query = world.query_state::<NameOrEntity>();
let d1 = query.get(&world, e1).unwrap();
let d2 = query.get(&world, e2).unwrap();
// NameOrEntity Display for entities without a Name should be {index}v{generation}
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_core_pipeline/src/taa/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ impl SpecializedRenderPipeline for TaaPipeline {
}

fn extract_taa_settings(mut commands: Commands, mut main_world: ResMut<MainWorld>) {
let mut cameras_3d = main_world.query_filtered::<(
let mut cameras_3d = main_world.query_state_filtered::<(
&RenderEntity,
&Camera,
&Projection,
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_ecs/macros/src/query_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,6 +325,14 @@ pub fn derive_query_data_impl(input: TokenStream) -> TokenStream {
#(#named_field_idents: <#field_types as #path::query::WorldQuery>::State,)*
}

impl #user_impl_generics ::core::clone::Clone for #state_struct_name #user_ty_generics #user_where_clauses {
fn clone(&self) -> Self {
Self {
#(#named_field_idents: ::core::clone::Clone::clone(&self.#named_field_idents),)*
}
}
}

#mutable_world_query_impl

#read_only_impl
Expand Down
8 changes: 8 additions & 0 deletions crates/bevy_ecs/macros/src/query_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@ pub fn derive_query_filter_impl(input: TokenStream) -> TokenStream {
#(#named_field_idents: <#field_types as #path::query::WorldQuery>::State,)*
}

impl #user_impl_generics ::core::clone::Clone for #state_struct_name #user_ty_generics #user_where_clauses {
fn clone(&self) -> Self {
Self {
#(#named_field_idents: ::core::clone::Clone::clone(&self.#named_field_idents),)*
}
}
}

#world_query_impl

#filter_impl
Expand Down
4 changes: 2 additions & 2 deletions crates/bevy_ecs/src/change_detection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,7 +1282,7 @@ mod tests {

// Since the world is always ahead, as long as changes can't get older than `u32::MAX` (which we ensure),
// the wrapping difference will always be positive, so wraparound doesn't matter.
let mut query = world.query::<Ref<C>>();
let mut query = world.query_state::<Ref<C>>();
assert!(query.single(&world).is_changed());
}

Expand All @@ -1297,7 +1297,7 @@ mod tests {
*world.change_tick.get_mut() += MAX_CHANGE_AGE + CHECK_TICK_THRESHOLD;
let change_tick = world.change_tick();

let mut query = world.query::<Ref<C>>();
let mut query = world.query_state::<Ref<C>>();
for tracker in query.iter(&world) {
let ticks_since_insert = change_tick.relative_to(*tracker.ticks.added).get();
let ticks_since_change = change_tick.relative_to(*tracker.ticks.changed).get();
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ecs/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ pub type ComponentHook = for<'w> fn(DeferredWorld<'w>, Entity, ComponentId);
/// world.init_resource::<TrackedEntities>();
///
/// // No entities with `MyTrackedComponent` have been added yet, so we can safely add component hooks
/// let mut tracked_component_query = world.query::<&MyTrackedComponent>();
/// let mut tracked_component_query = world.query_state::<&MyTrackedComponent>();
/// assert!(tracked_component_query.iter(&world).next().is_none());
///
/// world.register_component_hooks::<MyTrackedComponent>().on_add(|mut world, entity, _component_id| {
Expand Down
Loading
Loading