Skip to content

Shortcut for With and Without #9255

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
106 changes: 106 additions & 0 deletions crates/bevy_ecs/src/query/filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,3 +661,109 @@ macro_rules! impl_archetype_filter_tuple {
}

all_tuples!(impl_archetype_filter_tuple, 0, 15, F);

/// This macro implements `WorldQuery` and `ReadOnlyWorldQuery` for a filter
/// so that `Filter<(A, B, C)>` acts as a shortcut for `(Filter<A>, Filter<B>, Filter<C>)`.
macro_rules! impl_filter_tuple_to_tuple_filter {
Copy link
Member

Choose a reason for hiding this comment

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

This macro and the two invocations adds yet another set of tuple pyramids, which can add a lot of additional code to compile. Can you check the difference in compile time for bevy_ecs before and after this change?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With the changes, bevy_ecs took about half a second longer to compile.

rustc 1.71.0 (8ede3aae2 2023-07-12)
Host: x86_64-pc-windows-msvc
Target: x86_64-pc-windows-msvc

($filter:ident; $($T:ident),*) => {

// SAFETY: no component access or archetype component access
unsafe impl<$($T: Component),*> ReadOnlyWorldQuery for $filter<($($T),*)> {}

// SAFETY: `Self::ReadOnly` is the same as `Self`
unsafe impl<$($T: Component),*> WorldQuery for $filter<($($T),*)> {
type Fetch<'w> = <($($filter<$T>,)*) as WorldQuery>::Fetch<'w>;
type Item<'w> = <($($filter<$T>,)*) as WorldQuery>::Item<'w>;
type ReadOnly = Self;
type State = <($($filter<$T>,)*) as WorldQuery>::State;

fn shrink<'wlong: 'wshort, 'wshort>(item: Self::Item<'wlong>) -> Self::Item<'wshort> {
<($($filter<$T>,)*) as WorldQuery>::shrink(item)
}

#[inline]
unsafe fn init_fetch<'w>(
world: UnsafeWorldCell<'w>,
state: &Self::State,
last_run: Tick,
this_run: Tick,
) -> Self::Fetch<'w> {
<($($filter<$T>,)*) as WorldQuery>::init_fetch(world, state, last_run, this_run)
}

unsafe fn clone_fetch<'w>(fetch: &Self::Fetch<'w>) -> Self::Fetch<'w> {
<($($filter<$T>,)*) as WorldQuery>::clone_fetch(fetch)
}

const IS_DENSE: bool = <($($filter<$T>,)*) as WorldQuery>::IS_DENSE;

const IS_ARCHETYPAL: bool = <($($filter<$T>,)*) as WorldQuery>::IS_ARCHETYPAL;

#[inline]
unsafe fn set_table<'w>(fetch: &mut Self::Fetch<'w>, state: &Self::State, table: &'w Table) {
<($($filter<$T>,)*) as WorldQuery>::set_table(fetch, state, table)
}

#[inline]
unsafe fn set_archetype<'w>(
fetch: &mut Self::Fetch<'w>,
state: &Self::State,
archetype: &'w Archetype,
table: &'w Table,
) {
<($($filter<$T>,)*) as WorldQuery>::set_archetype(fetch, state, archetype, table)
}

#[inline(always)]
unsafe fn fetch<'w>(
fetch: &mut Self::Fetch<'w>,
entity: Entity,
table_row: TableRow,
) -> Self::Item<'w> {
<($($filter<$T>,)*) as WorldQuery>::fetch(fetch, entity, table_row)
}

#[inline]
fn update_component_access(state: &Self::State, access: &mut FilteredAccess<ComponentId>) {
<($($filter<$T>,)*) as WorldQuery>::update_component_access(state, access)
}

#[inline]
fn update_archetype_component_access(
state: &Self::State,
archetype: &Archetype,
access: &mut Access<ArchetypeComponentId>,
) {
<($($filter<$T>,)*) as WorldQuery>::update_archetype_component_access(
state, archetype, access,
)
}

fn init_state(world: &mut World) -> Self::State {
<($($filter<$T>,)*) as WorldQuery>::init_state(world)
}

fn matches_component_set(
state: &Self::State,
set_contains_id: &impl Fn(ComponentId) -> bool,
) -> bool {
<($($filter<$T>,)*) as WorldQuery>::matches_component_set(state, set_contains_id)
}
}
};
}

macro_rules! impl_tuple_with {
($($T:ident),*) => {
impl_filter_tuple_to_tuple_filter!(With; $($T),*);
};
}

macro_rules! impl_tuple_without {
($($T:ident),*) => {
impl_filter_tuple_to_tuple_filter!(Without; $($T),*);
};
}

all_tuples!(impl_tuple_with, 2, 15, T);
all_tuples!(impl_tuple_without, 2, 15, T);
39 changes: 39 additions & 0 deletions crates/bevy_ecs/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,4 +799,43 @@ mod tests {
let values = world.query::<&B>().iter(&world).collect::<Vec<&B>>();
assert_eq!(values, vec![&B(2)]);
}

#[test]
fn query_filter_tuple() {
let mut world = World::new();

world.spawn(A(0));
world.spawn((A(0), B(0)));
world.spawn((B(0), C(0)));
world.spawn((A(0), B(0), C(0)));
world.spawn((C(0), D(0)));

{
let tuple_of_with = world
.query_filtered::<Entity, (With<A>, With<B>)>()
.iter(&world)
.collect::<HashSet<Entity>>();

let with_of_tuple = world
.query_filtered::<Entity, With<(A, B)>>()
.iter(&world)
.collect::<HashSet<Entity>>();

assert_eq!(tuple_of_with, with_of_tuple);
}

{
let tuple_of_without = world
.query_filtered::<Entity, (Without<A>, Without<B>)>()
.iter(&world)
.collect::<HashSet<Entity>>();

let without_of_tuple = world
.query_filtered::<Entity, Without<(A, B)>>()
.iter(&world)
.collect::<HashSet<Entity>>();

assert_eq!(tuple_of_without, without_of_tuple);
}
}
}