Skip to content

Commit

Permalink
review
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 10, 2024
1 parent 279aac4 commit bee3f9a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 32 deletions.
4 changes: 2 additions & 2 deletions crates/re_query/src/archetype_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,13 @@ impl ComponentWithInstances {
}
let arr = self
.lookup_arrow(instance_key)
.map_or_else(|| Err(QueryError::ComponentNotFound(C::name())), Ok)?;
.map_or_else(|| Err(crate::ComponentNotFoundError(C::name())), Ok)?;

let mut iter = C::from_arrow(arr.as_ref())?.into_iter();

let val = iter
.next()
.map_or_else(|| Err(QueryError::ComponentNotFound(C::name())), Ok)?;
.map_or_else(|| Err(crate::ComponentNotFoundError(C::name())), Ok)?;
Ok(val)
}

Expand Down
18 changes: 13 additions & 5 deletions crates/re_query/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,17 @@ pub use self::util::query_archetype_with_history;
#[cfg(feature = "testing")]
pub use self::query::__populate_example_store;

#[derive(Debug, Clone, Copy)]
pub struct ComponentNotFoundError(pub re_types_core::ComponentName);

impl std::fmt::Display for ComponentNotFoundError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_fmt(format_args!("Could not find component: {}", self.0))
}
}

impl std::error::Error for ComponentNotFoundError {}

#[derive(thiserror::Error, Debug)]
pub enum QueryError {
#[error("Tried to access a column that doesn't exist")]
Expand All @@ -31,11 +42,8 @@ pub enum QueryError {
#[error("Could not find primary component: {0}")]
PrimaryNotFound(re_types_core::ComponentName),

#[error("Could not find required component: {0}")]
RequiredComponentNotFound(re_types_core::ComponentName),

#[error("Could not find component: {0}")]
ComponentNotFound(re_types_core::ComponentName),
#[error(transparent)]
ComponentNotFound(#[from] ComponentNotFoundError),

#[error("Tried to access component of type '{actual:?}' using component '{requested:?}'")]
TypeMismatch {
Expand Down
4 changes: 2 additions & 2 deletions crates/re_query_cache/benches/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ criterion_main!(benches);
fn mono_points(c: &mut Criterion) {
// Each mono point gets logged at a different path
let paths = (0..NUM_POINTS)
.map(move |point_idx| entity_path!("points", point_idx.to_string()))
.map(move |point_idx| entity_path!("points", point_idx))
.collect_vec();
let msgs = build_points_rows(&paths, 1);

Expand Down Expand Up @@ -83,7 +83,7 @@ fn mono_points(c: &mut Criterion) {
fn mono_strings(c: &mut Criterion) {
// Each mono string gets logged at a different path
let paths = (0..NUM_STRINGS)
.map(move |string_idx| entity_path!("strings", string_idx.to_string()))
.map(move |string_idx| entity_path!("strings", string_idx))
.collect_vec();
let msgs = build_strings_rows(&paths, 1);

Expand Down
25 changes: 6 additions & 19 deletions crates/re_query_cache/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use std::{
};

use ahash::HashMap;
use itertools::Either;
use once_cell::sync::Lazy;
use parking_lot::RwLock;
use paste::paste;
Expand Down Expand Up @@ -175,36 +174,24 @@ impl CacheBucket {
#[inline]
pub fn iter_component<C: Component + Send + Sync + 'static>(
&self,
) -> re_query::Result<impl Iterator<Item = &[C]>> {
) -> Option<impl Iterator<Item = &[C]>> {
let data = self
.components
.get(&C::name())
.map(|data| data.as_any().downcast_ref::<FlatVecDeque<C>>())
.ok_or_else(|| re_query::QueryError::RequiredComponentNotFound(C::name()))?;

let Some(data) = data else {
return Ok(Either::Left(std::iter::empty()));
};

Ok(Either::Right(data.iter()))
.and_then(|data| data.as_any().downcast_ref::<FlatVecDeque<C>>())?;
Some(data.iter())
}

/// Iterate over the batches of the specified optional component.
#[inline]
pub fn iter_component_opt<C: Component + Send + Sync + 'static>(
&self,
) -> re_query::Result<impl Iterator<Item = &[Option<C>]>> {
) -> Option<impl Iterator<Item = &[Option<C>]>> {
let data = self
.components
.get(&C::name())
.map(|data| data.as_any().downcast_ref::<FlatVecDeque<Option<C>>>())
.ok_or_else(|| re_query::QueryError::ComponentNotFound(C::name()))?;

let Some(data) = data else {
return Ok(Either::Left(std::iter::empty()));
};

Ok(Either::Right(data.iter()))
.and_then(|data| data.as_any().downcast_ref::<FlatVecDeque<Option<C>>>())?;
Some(data.iter())
}

/// How many timestamps' worth of data is stored in this bucket?
Expand Down
22 changes: 18 additions & 4 deletions crates/re_query_cache/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,16 @@ pub enum MaybeCachedComponentData<'a, C> {
Raw(Vec<C>),
}

impl<'a, C: Clone> MaybeCachedComponentData<'a, C> {
impl<'a, C> std::ops::Deref for MaybeCachedComponentData<'a, C> {
type Target = [C];

#[inline]
fn deref(&self) -> &Self::Target {
self.as_slice()
}
}

impl<'a, C> MaybeCachedComponentData<'a, C> {
#[inline]
pub fn iter(&self) -> impl ExactSizeIterator<Item = &C> + '_ {
self.as_slice().iter()
Expand Down Expand Up @@ -102,12 +111,13 @@ macro_rules! impl_query_archetype {
// NOTE: Implicitly dropping the write guard here: the LatestAtCache is free once again!

if bucket.is_empty() {
re_tracing::profile_scope!("fill");

let now = web_time::Instant::now();
let arch_view = query_archetype::<A>(store, &query, entity_path)?;

bucket.[<insert_pov $N _comp$M>]::<A, $($pov,)+ $($comp,)*>(query.at, &arch_view)?;

// TODO(cmc): I'd love a way of putting this information into the `puffin` span directly.
let elapsed = now.elapsed();
::re_log::trace!(
store_id=%store.id(),
Expand All @@ -118,11 +128,15 @@ macro_rules! impl_query_archetype {
);
}

re_tracing::profile_scope!("iter");

let it = itertools::izip!(
bucket.iter_pov_times(),
bucket.iter_pov_instance_keys(),
$(bucket.iter_component::<$pov>()?,)+
$(bucket.iter_component_opt::<$comp>()?,)*
$(bucket.iter_component::<$pov>()
.ok_or_else(|| re_query::ComponentNotFoundError(<$pov>::name()))?,)+
$(bucket.iter_component_opt::<$comp>()
.ok_or_else(|| re_query::ComponentNotFoundError(<$comp>::name()))?,)*
).map(|(time, instance_keys, $($pov,)+ $($comp,)*)| {
(
*time,
Expand Down

0 comments on commit bee3f9a

Please sign in to comment.