From 858b5455d7142a4df01a7d09d2ec2af769a0df0e Mon Sep 17 00:00:00 2001 From: Clement Rey Date: Thu, 18 Apr 2024 15:18:58 +0200 Subject: [PATCH] introduce non-cacheable components --- crates/re_query/src/latest_at/query.rs | 19 +++++++++++----- crates/re_query/src/lib.rs | 31 ++++++++++++++++++++++++++ crates/re_query/src/range/query.rs | 18 ++++++++++----- 3 files changed, 56 insertions(+), 12 deletions(-) diff --git a/crates/re_query/src/latest_at/query.rs b/crates/re_query/src/latest_at/query.rs index b4ca7cdfe059..8107647fc3a1 100644 --- a/crates/re_query/src/latest_at/query.rs +++ b/crates/re_query/src/latest_at/query.rs @@ -33,12 +33,19 @@ impl Caches { for component_name in component_names { let key = CacheKey::new(entity_path.clone(), query.timeline(), component_name); - let cache = Arc::clone( - self.latest_at_per_cache_key - .write() - .entry(key.clone()) - .or_insert_with(|| Arc::new(RwLock::new(LatestAtCache::new(key.clone())))), - ); + + let cache = if crate::cacheable(component_name) { + Arc::clone( + self.latest_at_per_cache_key + .write() + .entry(key.clone()) + .or_insert_with(|| Arc::new(RwLock::new(LatestAtCache::new(key.clone())))), + ) + } else { + // If the component shouldn't be cached, simply instantiate a new cache for it. + // It will be dropped when the user is done with it. + Arc::new(RwLock::new(LatestAtCache::new(key.clone()))) + }; let mut cache = cache.write(); cache.handle_pending_invalidation(); diff --git a/crates/re_query/src/lib.rs b/crates/re_query/src/lib.rs index d75b866986dd..d9c9e5d26098 100644 --- a/crates/re_query/src/lib.rs +++ b/crates/re_query/src/lib.rs @@ -117,3 +117,34 @@ impl From<(RangeQuery, RangeResults)> for Results { Self::Range(query, results) } } + +// --- + +/// Returns `true` if the specified `component_name` can be cached. +/// +/// Used internally to avoid unnecessarily caching components that are already cached in other +/// places, for historical reasons. +pub fn cacheable(component_name: re_types::ComponentName) -> bool { + use std::sync::OnceLock; + static NOT_CACHEABLE: OnceLock = OnceLock::new(); + + use re_types_core::Loggable as _; + let not_cacheable = NOT_CACHEABLE.get_or_init(|| { + [ + // TODO(#5303): instance keys are on their way out anyhow. + re_types::components::InstanceKey::name(), + // TODO(#5974): tensors might already be cached in the ad-hoc JPEG cache, we don't + // want yet another copy. + re_types::components::TensorData::name(), + // TODO(#5974): meshes are already cached in the ad-hoc mesh cache, we don't + // want yet another copy. + re_types::components::MeshProperties::name(), + // TODO(#5974): blobs are used for assets, which are themselves already cached in + // the ad-hoc mesh cache -- we don't want yet another copy. + re_types::components::Blob::name(), + ] + .into() + }); + + !not_cacheable.contains(&component_name) +} diff --git a/crates/re_query/src/range/query.rs b/crates/re_query/src/range/query.rs index 649aa7ef8700..28277cc16e94 100644 --- a/crates/re_query/src/range/query.rs +++ b/crates/re_query/src/range/query.rs @@ -33,12 +33,18 @@ impl Caches { for component_name in component_names { let key = CacheKey::new(entity_path.clone(), query.timeline(), component_name); - let cache = Arc::clone( - self.range_per_cache_key - .write() - .entry(key.clone()) - .or_insert_with(|| Arc::new(RwLock::new(RangeCache::new(key.clone())))), - ); + let cache = if crate::cacheable(component_name) { + Arc::clone( + self.range_per_cache_key + .write() + .entry(key.clone()) + .or_insert_with(|| Arc::new(RwLock::new(RangeCache::new(key.clone())))), + ) + } else { + // If the component shouldn't be cached, simply instantiate a new cache for it. + // It will be dropped when the user is done with it. + Arc::new(RwLock::new(RangeCache::new(key.clone()))) + }; let mut cache = cache.write(); cache.handle_pending_invalidation();