Skip to content

Commit

Permalink
implement runtime-togglable caching
Browse files Browse the repository at this point in the history
  • Loading branch information
teh-cmc committed Jan 4, 2024
1 parent 21a1e1a commit 1cd38c0
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 11 deletions.
2 changes: 1 addition & 1 deletion crates/re_data_store/src/store_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl LatestAtQuery {
/// interval.
///
/// Motivation: all data is considered alive until the next logging to the same component path.
#[derive(Clone)]
#[derive(Clone, PartialEq, Eq, Hash)]
pub struct RangeQuery {
pub timeline: Timeline,
pub range: TimeRange,
Expand Down
2 changes: 2 additions & 0 deletions crates/re_query_cache/benches/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ fn query_and_visit_points(store: &DataStore, paths: &[EntityPath]) -> Vec<SavePo
// TODO(jleibs): Add Radius once we have support for it in field_types
for path in paths {
query_archetype_pov1_comp1::<Points2D, Position2D, Color, _>(
true,
store,
&query.clone().into(),
path,
Expand Down Expand Up @@ -309,6 +310,7 @@ fn query_and_visit_strings(store: &DataStore, paths: &[EntityPath]) -> Vec<SaveS

for path in paths {
query_archetype_pov1_comp1::<Points2D, Position2D, Text, _>(
true,
store,
&query.clone().into(),
path,
Expand Down
12 changes: 10 additions & 2 deletions crates/re_query_cache/src/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use parking_lot::RwLock;
use paste::paste;
use seq_macro::seq;

use re_data_store::LatestAtQuery;
use re_data_store::{LatestAtQuery, RangeQuery};
use re_log_types::{EntityPath, RowId, StoreId, TimeInt, Timeline};
use re_query::ArchetypeView;
use re_types_core::{components::InstanceKey, Archetype, ArchetypeName, Component, ComponentName};
Expand All @@ -22,15 +22,23 @@ use crate::{ErasedFlatVecDeque, FlatVecDeque};
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum AnyQuery {
LatestAt(LatestAtQuery),
// TODO(cmc): range queries support.
Range(RangeQuery),
}

impl From<LatestAtQuery> for AnyQuery {
#[inline]
fn from(query: LatestAtQuery) -> Self {
Self::LatestAt(query)
}
}

impl From<RangeQuery> for AnyQuery {
#[inline]
fn from(query: RangeQuery) -> Self {
Self::Range(query)
}
}

// ---

// TODO(cmc): Centralize and harmonize all caches (query, jpeg, mesh).
Expand Down
71 changes: 64 additions & 7 deletions crates/re_query_cache/src/query.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use paste::paste;
use seq_macro::seq;

use re_data_store::{DataStore, LatestAtQuery, TimeInt, Timeline};
use re_data_store::{DataStore, LatestAtQuery, RangeQuery, TimeInt, TimeRange, Timeline};
use re_entity_db::{ExtraQueryHistory, VisibleHistory};
use re_log_types::{EntityPath, RowId};
use re_query::query_archetype;
Expand Down Expand Up @@ -48,6 +48,7 @@ impl<'a, C: Clone> MaybeCachedComponentData<'a, C> {
/// Alias for [`query_archetype_pov1_comp0`].
#[inline]
pub fn query_archetype_pov1<'a, A, R1, F>(
cached: bool,
store: &'a DataStore,
query: &AnyQuery,
entity_path: &'a EntityPath,
Expand All @@ -64,7 +65,7 @@ where
),
),
{
query_archetype_pov1_comp0::<A, R1, F>(store, query, entity_path, f)
query_archetype_pov1_comp0::<A, R1, F>(cached, store, query, entity_path, f)
}

macro_rules! impl_query_archetype {
Expand All @@ -73,6 +74,7 @@ macro_rules! impl_query_archetype {
#[doc = "(combined) for `" $N "` point-of-view components and `" $M "` optional components."]
#[allow(non_snake_case)]
pub fn [<query_archetype_pov$N _comp$M>]<'a, A, $($pov,)+ $($comp,)* F>(
cached: bool,
store: &'a DataStore,
query: &AnyQuery,
entity_path: &'a EntityPath,
Expand All @@ -94,17 +96,58 @@ macro_rules! impl_query_archetype {
// NOTE: not `profile_function!` because we want them merged together.
re_tracing::profile_scope!(
"query_archetype",
format!("arch={} pov={} comp={}", A::name(), $N, $M)
format!("cached={cached} arch={} pov={} comp={}", A::name(), $N, $M)
);

match &query {
// TODO(cmc): cached range support
AnyQuery::Range(query) => {
re_tracing::profile_scope!("range", format!("{query:?}"));

// NOTE: `+ 2` because we always grab the indicator component as well as the
// instance keys.
let arch_views = ::re_query::range_archetype::<A, { $N + $M + 2 }>(store, query, entity_path);

for (time, arch_view) in arch_views {
let data = (
// TODO(cmc): `ArchetypeView` should indicate its pov time.
(time.unwrap_or(TimeInt::MIN), arch_view.primary_row_id()),
MaybeCachedComponentData::Raw(arch_view.iter_instance_keys().collect()),
$(MaybeCachedComponentData::Raw(arch_view.iter_required_component::<$pov>()?.collect()),)+
$(MaybeCachedComponentData::Raw(arch_view.iter_optional_component::<$comp>()?.collect()),)*
);

f(data);
}

Ok(())
}

AnyQuery::LatestAt(query) if !cached => {
re_tracing::profile_scope!("latest_at", format!("{query:?}"));

let arch_view = ::re_query::query_archetype::<A>(store, query, entity_path)?;

let data = (
// TODO(cmc): `ArchetypeView` should indicate its pov time.
(TimeInt::MIN, arch_view.primary_row_id()),
MaybeCachedComponentData::Raw(arch_view.iter_instance_keys().collect()),
$(MaybeCachedComponentData::Raw(arch_view.iter_required_component::<$pov>()?.collect()),)+
$(MaybeCachedComponentData::Raw(arch_view.iter_optional_component::<$comp>()?.collect()),)*
);

f(data);

Ok(())
}

AnyQuery::LatestAt(query) => {
Caches::with_latest_at::<A, _, _>(
store.id().clone(),
entity_path.clone(),
query,
|cache| {
re_tracing::profile_scope!("latest_at");
re_tracing::profile_scope!("latest_at", format!("{query:?}"));

let bucket = cache.entry(query.at).or_default();
// NOTE: Implicitly dropping the write guard here: the LatestAtCache is
Expand Down Expand Up @@ -172,6 +215,7 @@ seq!(NUM_COMP in 0..10 {
/// Alias for [`query_archetype_with_history_pov1_comp0`].
#[inline]
pub fn query_archetype_with_history_pov1<'a, A, R1, F>(
cached: bool,
store: &'a DataStore,
timeline: &'a Timeline,
time: &'a TimeInt,
Expand All @@ -190,7 +234,9 @@ where
),
),
{
query_archetype_with_history_pov1_comp0::<A, R1, F>(store, timeline, time, history, ent_path, f)
query_archetype_with_history_pov1_comp0::<A, R1, F>(
cached, store, timeline, time, history, ent_path, f,
)
}

/// Generates a function to cache a (potentially historical) query with N point-of-view components and M
Expand All @@ -200,6 +246,7 @@ macro_rules! impl_query_archetype_with_history {
#[doc = "Cached implementation of [`re_query::query_archetype_with_history`] for `" $N "` point-of-view"]
#[doc = "components and `" $M "` optional components."]
pub fn [<query_archetype_with_history_pov$N _comp$M>]<'a, A, $($pov,)+ $($comp,)* F>(
cached: bool,
store: &'a DataStore,
timeline: &'a Timeline,
time: &'a TimeInt,
Expand All @@ -223,7 +270,7 @@ macro_rules! impl_query_archetype_with_history {
// NOTE: not `profile_function!` because we want them merged together.
re_tracing::profile_scope!(
"query_archetype_with_history",
format!("arch={} pov={} comp={}", A::name(), $N, $M)
format!("cached={cached} arch={} pov={} comp={}", A::name(), $N, $M)
);

let visible_history = match timeline.typ() {
Expand All @@ -234,13 +281,23 @@ macro_rules! impl_query_archetype_with_history {
if !history.enabled || visible_history == VisibleHistory::OFF {
let query = LatestAtQuery::new(*timeline, *time);
$crate::[<query_archetype_pov$N _comp$M>]::<A, $($pov,)+ $($comp,)* _>(
cached,
store,
&query.clone().into(),
ent_path,
f,
)
} else {
unimplemented!("TODO(cmc): range support");
let min_time = visible_history.from(*time);
let max_time = visible_history.to(*time);
let query = RangeQuery::new(*timeline, TimeRange::new(min_time, max_time));
$crate::[<query_archetype_pov$N _comp$M>]::<A, $($pov,)+ $($comp,)* _>(
cached,
store,
&query.clone().into(),
ent_path,
f,
)
}
} }
};
Expand Down
1 change: 1 addition & 0 deletions crates/re_query_cache/tests/latest_at.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ fn query_and_compare(store: &DataStore, query: &LatestAtQuery, ent_path: &Entity
let mut got_colors = Vec::new();

query_archetype_pov1_comp1::<Points2D, Position2D, Color, _>(
true,
store,
&query.clone().into(),
ent_path,
Expand Down
8 changes: 8 additions & 0 deletions crates/re_viewer/src/ui/rerun_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,14 @@ fn experimental_feature_ui(
- Add the 'Add space view/container' modal, accessible from the selection panel.",
);

re_ui
.checkbox(
ui,
&mut app_options.experimental_primary_caching_point_clouds,
"Primary caching: 2D & 3D point clouds",
)
.on_hover_text("Toggle primary caching for the 2D & 3D point cloud space views.");

re_ui
.checkbox(
ui,
Expand Down
7 changes: 7 additions & 0 deletions crates/re_viewer_context/src/app_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ pub struct AppOptions {
/// Enable the experimental support for the container addition workflow.
pub experimental_additive_workflow: bool,

/// Toggle primary caching for the 2D & 3D point cloud space views.
pub experimental_primary_caching_point_clouds: bool,

/// Displays an overlay for debugging picking.
pub show_picking_debug_overlay: bool,

Expand Down Expand Up @@ -54,6 +57,10 @@ impl Default for AppOptions {

experimental_additive_workflow: cfg!(debug_assertions),

// TODO(cmc): default to true for debug/rerun-workspace once minimal features have been
// merged in.
experimental_primary_caching_point_clouds: false,

show_picking_debug_overlay: false,

show_blueprint_in_timeline: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl VisualizerSystem for InstanceColorSystem {
) -> Result<Vec<re_renderer::QueueableDrawData>, SpaceViewSystemExecutionError> {
// For each entity in the space view that should be displayed with the `InstanceColorSystem`…
for data_result in query.iter_visible_data_results(Self::identifier()) {
// ...gather all colors and their instance ids.
// gather all colors and their instance ids.
if let Ok(arch_view) = query_archetype::<ColorArchetype>(
ctx.entity_db.store(),
&ctx.current_query(),
Expand Down

0 comments on commit 1cd38c0

Please sign in to comment.