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

Dynamic queries and builder API #9774

Merged
merged 57 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
b56e1c7
Fully dynamic term based query builder
james-j-obrien Sep 11, 2023
5917065
System installation of dynamically built queries
james-j-obrien Sep 11, 2023
b7f04df
Fix change detection, add Changed and Added
james-j-obrien Sep 11, 2023
07dc116
Add additional tests, fix bugs and clean up
james-j-obrien Sep 11, 2023
a12b5dd
Add trivial examples
james-j-obrien Sep 11, 2023
f6382c1
Performance tweaks
james-j-obrien Sep 12, 2023
25623dc
Add dynamic reference methods
james-j-obrien Sep 12, 2023
c16e0ec
Fix rebase issue
james-j-obrien Sep 12, 2023
799b318
Remove examples so github is less angry, rename builder methods to be…
james-j-obrien Sep 12, 2023
711653b
Better builder method names
james-j-obrien Sep 12, 2023
ff1e33a
Major perf improvements, add additional dynamic builder methods
james-j-obrien Sep 12, 2023
40bd8c5
Remove closures from hot loop, remove more runtime checks
james-j-obrien Sep 12, 2023
d0736f5
Remove allocations from hot loop
james-j-obrien Sep 12, 2023
c3d37da
Minor cleanup
james-j-obrien Sep 12, 2023
353c248
Remove #[inline(always)] where it doesn't improve or degrades perform…
james-j-obrien Sep 13, 2023
40c1c62
Flatten terms
james-j-obrien Sep 13, 2023
7667c0a
Fix AnyOf issue
james-j-obrien Sep 13, 2023
031779c
Reorder impls to be more intuitive
james-j-obrien Sep 13, 2023
5494b28
Add dynamic_query example
james-j-obrien Sep 14, 2023
688459d
First docs pass, bug fixes and minor improvements
james-j-obrien Sep 15, 2023
666edb3
Improve documentation, add additional utility functions, add benches
james-j-obrien Sep 18, 2023
5ac0e50
Remove temporary test
james-j-obrien Sep 18, 2023
9d6382f
Minor cleanup
james-j-obrien Sep 18, 2023
f7abf12
First CI pass
james-j-obrien Sep 18, 2023
5c880e9
Second CI pass
james-j-obrien Sep 18, 2023
7680abd
Third times the charm
james-j-obrien Sep 18, 2023
7bd46ab
Add comments
james-j-obrien Sep 18, 2023
db48ac8
Flatten terms, remove branching where possible
james-j-obrien Sep 29, 2023
02091d9
Fix example
james-j-obrien Sep 29, 2023
9a3f5b3
Static iteration, remove FetchedTerm and FetchBuffer
james-j-obrien Sep 30, 2023
88ba5e3
Cleanup component ptr access
james-j-obrien Sep 30, 2023
2cf8f1e
Tidy docs slightly
james-j-obrien Sep 30, 2023
9c65312
Swap to constants to speed up dense iteration, minor API cleanup
james-j-obrien Sep 30, 2023
e9c7861
Consistency pass
james-j-obrien Sep 30, 2023
c674c4a
Move to pointers from iterators, split fetch and filter terms
james-j-obrien Oct 4, 2023
5952756
Add From<Mut<T>> For MutUntyped
james-j-obrien Oct 7, 2023
a76370c
FilteredAccess based implementation
james-j-obrien Nov 14, 2023
323cc74
Merge branch 'dynamic-term-builder' into query-builder
james-j-obrien Nov 14, 2023
ce0ea41
Merge branch 'bevyengine:main' into dynamic-term-builder
james-j-obrien Nov 14, 2023
89f1eac
Doc fixes
james-j-obrien Nov 14, 2023
bf9cee7
Merge branch 'dynamic-term-builder' of https://github.com/james-j-obr…
james-j-obrien Nov 14, 2023
32a6796
Add compile fail tests, add safety comments to example
james-j-obrien Nov 20, 2023
289ca79
Merge main
james-j-obrien Nov 22, 2023
52e0391
Learn how to spell required correctly
james-j-obrien Nov 22, 2023
fecd6bc
Merge main, incorporate feedback
james-j-obrien Nov 30, 2023
04d88cc
Fix doc test
james-j-obrien Nov 30, 2023
3bfcc3f
Fix docs and compile fail tests
james-j-obrien Nov 30, 2023
60c006f
Fix trailing newline
james-j-obrien Nov 30, 2023
b59bb38
Fix trailing whitespace...
james-j-obrien Nov 30, 2023
cf951bb
Merge branch 'main' into dynamic-term-builder
james-j-obrien Dec 2, 2023
2e8a197
Remove unused import
james-j-obrien Dec 2, 2023
5c98ef4
Improve safety comments
james-j-obrien Dec 15, 2023
6f6a0cc
Merge main
james-j-obrien Dec 15, 2023
63f97a3
Address remaining feedback
james-j-obrien Jan 16, 2024
056ff2a
Merge branch 'main' into dynamic-term-builder
james-j-obrien Jan 16, 2024
a3ab076
Fix panic strings
james-j-obrien Jan 16, 2024
8c1bbb0
Fix doc test
james-j-obrien Jan 16, 2024
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
Prev Previous commit
Next Next commit
Remove #[inline(always)] where it doesn't improve or degrades perform…
…ance
  • Loading branch information
james-j-obrien committed Sep 13, 2023
commit 353c2489fc19594167ce196151214d8f552c4d1c
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/term_query/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl<'w, 's> TermQueryCursor<'w, 's> {
}
}

#[inline(always)]
#[inline]
unsafe fn next<Q: QueryTermGroup>(
&mut self,
tables: &'w Tables,
Expand Down Expand Up @@ -145,7 +145,7 @@ impl<'w, 's> TermQueryIterUntyped<'w, 's> {

// This would be a streaming iterator if rust supported those
impl<'w, 's> TermQueryIterUntyped<'w, 's> {
#[inline(always)]
#[inline]
fn next_fetch<'f>(&'f mut self) -> Option<&'f [FetchedTerm<'w>]> {
unsafe {
self.cursor
Expand All @@ -158,7 +158,7 @@ impl<'w, 's> TermQueryIterUntyped<'w, 's> {
impl<'w, 's> Iterator for TermQueryIterUntyped<'w, 's> {
type Item = Vec<FetchedTerm<'w>>;

#[inline(always)]
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.next_fetch().map(|fetches| fetches.to_vec())
}
Expand All @@ -170,7 +170,7 @@ pub struct TermQueryIter<'w, 's, Q: QueryTermGroup> {
}

impl<'w, 's, Q: QueryTermGroup> TermQueryIter<'w, 's, Q> {
#[inline(always)]
#[inline]
pub unsafe fn new(
world: UnsafeWorldCell<'w>,
query_state: &'s TermQueryState<Q>,
Expand All @@ -187,7 +187,7 @@ impl<'w, 's, Q: QueryTermGroup> TermQueryIter<'w, 's, Q> {
impl<'w, 's, Q: QueryTermGroup> Iterator for TermQueryIter<'w, 's, Q> {
type Item = Q::Item<'w>;

#[inline(always)]
#[inline]
fn next(&mut self) -> Option<Self::Item> {
unsafe {
if let Some(fetches) = &mut self.inner.next_fetch() {
Expand Down
6 changes: 3 additions & 3 deletions crates/bevy_ecs/src/term_query/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@ impl RawFetches {
}
}

#[inline(always)]
#[inline]
pub(crate) fn as_uninit<'w>(&self) -> &mut [MaybeUninit<FetchedTerm<'w>>] {
unsafe { std::slice::from_raw_parts_mut(self.mem.cast(), self.len) }
}

#[inline(always)]
#[inline]
pub(crate) fn as_slice<'w>(&self) -> &mut [FetchedTerm<'w>] {
unsafe { std::slice::from_raw_parts_mut(self.mem.cast(), self.len) }
}
Expand Down Expand Up @@ -558,7 +558,7 @@ impl<Q: QueryTermGroup, F: QueryTermGroup> TermQueryState<Q, F> {
}
}

#[inline(always)]
#[inline]
pub unsafe fn set_table<'w>(&self, state: &mut TermVec<TermState<'w>>, table: &'w Table) {
let len = self.terms.len();
let terms = &self.terms[..len];
Expand Down
22 changes: 11 additions & 11 deletions crates/bevy_ecs/src/term_query/terms/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ impl Fetchable for ComponentTerm {
state
}

#[inline(always)]
#[inline]
unsafe fn set_table<'w>(&self, state: &mut ComponentTermState<'w>, table: &'w Table) {
if let StorageType::SparseSet = state.storage {
return;
Expand Down Expand Up @@ -392,7 +392,7 @@ impl<T: Component> ComponentQueryTerm for Without<T> {
ComponentTerm::without(component)
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(_term: &'w FetchedComponent<'w>) -> Self::Item<'w> {}
}

Expand All @@ -405,7 +405,7 @@ impl<T: Component> ComponentQueryTerm for Has<T> {
ComponentTerm::with(component).optional()
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &'w FetchedComponent<'w>) -> Self::Item<'w> {
term.matched
}
Expand All @@ -420,7 +420,7 @@ impl<T: Component> ComponentQueryTerm for Added<T> {
ComponentTerm::added(component)
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(_term: &'w FetchedComponent<'w>) -> Self::Item<'w> {}
}

Expand All @@ -433,7 +433,7 @@ impl<T: Component> ComponentQueryTerm for Changed<T> {
ComponentTerm::changed(component)
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(_term: &FetchedComponent<'w>) -> Self::Item<'w> {}
}

Expand All @@ -446,7 +446,7 @@ impl<T: Component> ComponentQueryTerm for &T {
ComponentTerm::read_id(component)
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
term.pointer.debug_checked_unwrap().deref()
}
Expand All @@ -461,7 +461,7 @@ impl<T: Component> ComponentQueryTerm for Ref<'_, T> {
ComponentTerm::read_id(component).with_change_detection()
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
let change_detection = term.change_ticks.as_ref().debug_checked_unwrap();
Ref {
Expand All @@ -485,7 +485,7 @@ impl ComponentQueryTerm for Ptr<'_> {
ComponentTerm::read()
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
term.pointer.debug_checked_unwrap()
}
Expand All @@ -500,7 +500,7 @@ impl<'r, T: Component> ComponentQueryTerm for &'r mut T {
ComponentTerm::write_id(component)
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
let change_detection = term.change_ticks.as_ref().debug_checked_unwrap();
Mut {
Expand Down Expand Up @@ -528,7 +528,7 @@ impl<'r> ComponentQueryTerm for PtrMut<'r> {
ComponentTerm::read()
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
let change_detection = term.change_ticks.as_ref().debug_checked_unwrap();
MutUntyped {
Expand All @@ -552,7 +552,7 @@ impl<C: ComponentQueryTerm> ComponentQueryTerm for Option<C> {
C::init_term(world).optional()
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedComponent<'w>) -> Self::Item<'w> {
if term.matched {
Some(C::from_fetch(term))
Expand Down
12 changes: 6 additions & 6 deletions crates/bevy_ecs/src/term_query/terms/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ impl Fetchable for EntityTerm {
world
}

#[inline(always)]
#[inline]
unsafe fn set_table<'w>(&self, _state: &mut Self::State<'w>, _table: &'w Table) {}

#[inline(always)]
#[inline]
unsafe fn fetch<'w>(
&self,
world: &Self::State<'w>,
Expand All @@ -73,7 +73,7 @@ impl Fetchable for EntityTerm {
}
}

#[inline(always)]
#[inline]
unsafe fn filter_fetch<'w>(
&self,
_state: &Self::State<'w>,
Expand Down Expand Up @@ -129,7 +129,7 @@ impl QueryTerm for Entity {
Term::Entity(EntityTerm::none())
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedTerm<'w>) -> Self::Item<'w> {
let term = term.entity().debug_checked_unwrap();

Expand All @@ -145,7 +145,7 @@ impl QueryTerm for EntityRef<'_> {
Term::Entity(EntityTerm::read())
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedTerm<'w>) -> Self::Item<'w> {
let term = term.entity().debug_checked_unwrap();
EntityRef::new(term.cell.debug_checked_unwrap())
Expand All @@ -160,7 +160,7 @@ impl<'r> QueryTerm for EntityMut<'r> {
Term::Entity(EntityTerm::write())
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedTerm<'w>) -> Self::Item<'w> {
let term = term.entity().debug_checked_unwrap();
EntityMut::new(term.cell.debug_checked_unwrap())
Expand Down
10 changes: 5 additions & 5 deletions crates/bevy_ecs/src/term_query/terms/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Fetchable for OrTerm {
.collect()
}

#[inline(always)]
#[inline]
unsafe fn set_table<'w>(&self, state: &mut Self::State<'w>, table: &'w Table) {
self.terms
.iter()
Expand All @@ -65,7 +65,7 @@ impl Fetchable for OrTerm {
})
}

#[inline(always)]
#[inline]
unsafe fn fetch<'w>(
&self,
state: &Self::State<'w>,
Expand All @@ -83,7 +83,7 @@ impl Fetchable for OrTerm {
}
}

#[inline(always)]
#[inline]
unsafe fn filter_fetch<'w>(
&self,
state: &Self::State<'w>,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<Q: ComponentQueryTermGroup> QueryTermGroup for Or<Q> {
}));
}

#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(
terms: &mut impl Iterator<Item = &'f FetchedTerm<'w>>,
) -> Self::Item<'w> {
Expand All @@ -165,7 +165,7 @@ impl<Q: ComponentQueryTermGroup> QueryTermGroup for AnyOf<Q> {
}));
}

#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(
terms: &mut impl Iterator<Item = &'f FetchedTerm<'w>>,
) -> Self::Item<'w> {
Expand Down
16 changes: 8 additions & 8 deletions crates/bevy_ecs/src/term_query/terms/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum FetchedTerm<'w> {
}

impl<'w> FetchedTerm<'w> {
#[inline(always)]
#[inline]
pub fn component(&self) -> Option<&<ComponentTerm as Fetchable>::Item<'w>> {
if let FetchedTerm::Component(term) = self {
Some(term)
Expand All @@ -52,7 +52,7 @@ impl<'w> FetchedTerm<'w> {
}
}

#[inline(always)]
#[inline]
pub fn entity(&self) -> Option<&<EntityTerm as Fetchable>::Item<'w>> {
if let FetchedTerm::Entity(term) = self {
Some(term)
Expand All @@ -61,7 +61,7 @@ impl<'w> FetchedTerm<'w> {
}
}

#[inline(always)]
#[inline]
pub fn group(&self) -> Option<&Vec<<ComponentTerm as Fetchable>::Item<'w>>> {
if let FetchedTerm::Or(term) = self {
Some(term)
Expand Down Expand Up @@ -254,7 +254,7 @@ impl<T: ComponentQueryTerm> QueryTerm for T {
Term::Component(T::init_term(world))
}

#[inline(always)]
#[inline]
unsafe fn from_fetch<'w>(term: &FetchedTerm<'w>) -> Self::Item<'w> {
let term = term.component().debug_checked_unwrap();
T::from_fetch(term)
Expand All @@ -280,7 +280,7 @@ impl<T: QueryTerm> QueryTermGroup for T {
terms.push(T::init_term(world));
}

#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(
terms: &mut impl Iterator<Item = &'f FetchedTerm<'w>>,
) -> Self::Item<'w> {
Expand Down Expand Up @@ -309,7 +309,7 @@ impl<T: ComponentQueryTerm> ComponentQueryTermGroup for T {
terms.push(T::init_term(world));
}

#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(
terms: &mut impl Iterator<Item = &'f FetchedComponent<'w>>,
) -> Self::Item<'w> {
Expand All @@ -329,7 +329,7 @@ macro_rules! impl_query_term_tuple {
)*
}

#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(_terms: &mut impl Iterator<Item = &'f FetchedTerm<'w>>) -> Self::Item<'w> {
($(
$term::from_fetches(_terms),
Expand All @@ -353,7 +353,7 @@ macro_rules! impl_component_query_term_tuple {
}


#[inline(always)]
#[inline]
unsafe fn from_fetches<'w: 'f, 'f>(_terms: &mut impl Iterator<Item = &'f FetchedComponent<'w>>) -> Self::Item<'w> {
($(
$term::from_fetches(_terms),
Expand Down