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

Optimize out unnecessary joins when querying archetypes #3377

Merged
merged 6 commits into from
Sep 20, 2023
Merged
Changes from 4 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
54 changes: 45 additions & 9 deletions crates/re_query/src/archetype_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,24 +322,60 @@ impl<A: Archetype> ArchetypeView<A> {
let component = self.components.get(&C::name());

if let Some(component) = component {
let primary_instance_key_iter = self.iter_instance_keys();
// NOTE: Autogenerated instance keys are interned: if two or more rows in the store
// share the same keys, then they will literally point to the same memory address.
// Therefore we can compare those addresses, and early out if they match.
let primary_instance_key_buffer =
self.required_comp().instance_keys.as_arrow_ref() as *const dyn Array;
let component_instance_key_buffer =
component.instance_keys.as_arrow_ref() as *const dyn Array;

// TODO(rust-lang/rust#81513): We want to compare just the data part of these fat pointers,
// because comparing the vtable pointers, while defined, can lead to unstable results.
//
// We'd like to use `std::ptr::to_raw_parts` for that (or any of the myriad of tools
// for dealing with smart pointers available in the stdlib)… but everything that
// relates to fat pointers is nightly only.
// So, for now, we do it the old way.
if std::ptr::eq(
primary_instance_key_buffer.cast::<u8>(),
component_instance_key_buffer.cast::<u8>(),
teh-cmc marked this conversation as resolved.
Show resolved Hide resolved
) {
// NOTE: A component instance cannot be optional in itself, and if we're on this
// path then we know for a fact that both batches can be intersected 1-to-1.
// Therefore there cannot be any null values, therefore we can go through the fast
// deserialization path.
let component_value_iter = {
re_tracing::profile_scope!("try_from_arrow", C::name());
C::try_from_arrow(component.values.as_arrow_ref())?
.into_iter()
.map(Some)
};

let mut component_instance_key_iter = component.instance_keys().into_iter();
return Ok(itertools::Either::Left(itertools::Either::Left(
component_value_iter,
)));
}

let component_value_iter = {
re_tracing::profile_scope!("try_from_arrow_opt", C::name());
C::try_from_arrow_opt(component.values.as_arrow_ref())?.into_iter()
};

let primary_instance_key_iter = self.iter_instance_keys();
let mut component_instance_key_iter = component.instance_keys().into_iter();

let next_component_instance_key = component_instance_key_iter.next();

Ok(itertools::Either::Left(ComponentJoinedIterator {
primary_instance_key_iter,
component_instance_key_iter,
component_value_iter,
next_component_instance_key,
splatted_component_value: None,
}))
Ok(itertools::Either::Left(itertools::Either::Right(
ComponentJoinedIterator {
primary_instance_key_iter,
component_instance_key_iter,
component_value_iter,
next_component_instance_key,
splatted_component_value: None,
},
)))
} else {
let primary = self.required_comp();
let nulls = (0..primary.len()).map(|_| None);
Expand Down