Skip to content

Commit

Permalink
Make Caches non-mut
Browse files Browse the repository at this point in the history
  • Loading branch information
emilk committed Jun 5, 2023
1 parent 310c214 commit 16a9aed
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 22 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion crates/re_data_ui/src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ impl EntityDataUi for Tensor {
) {
re_tracing::profile_function!();

match ctx.cache.entry::<TensorDecodeCache>().entry(self.clone()) {
let decoded = ctx.cache.entry::<TensorDecodeCache>().entry(self.clone());
match decoded {
Ok(decoded) => {
let annotations = crate::annotations(ctx, query, entity_path);
tensor_ui(
Expand Down
4 changes: 2 additions & 2 deletions crates/re_space_view_spatial/src/scene/parts/images.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ fn to_textured_rect(
let Some([height, width, _]) = tensor.image_height_width_channels() else { return None; };

let debug_name = ent_path.to_string();
let tensor_stats = ctx.cache.entry::<TensorStatsCache>().entry(tensor);
let tensor_stats = *ctx.cache.entry::<TensorStatsCache>().entry(tensor);

match gpu_bridge::tensor_to_gpu(
ctx.render_ctx,
&debug_name,
tensor,
tensor_stats,
&tensor_stats,
annotations,
) {
Ok(colormapped_texture) => {
Expand Down
1 change: 1 addition & 0 deletions crates/re_viewer_context/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ lazy_static.workspace = true
macaw.workspace = true
ndarray.workspace = true
nohash-hasher.workspace = true
parking_lot.workspace = true
serde = "1"
slotmap.workspace = true
thiserror.workspace = true
Expand Down
36 changes: 20 additions & 16 deletions crates/re_viewer_context/src/caches.rs
Original file line number Diff line number Diff line change
@@ -1,38 +1,42 @@
use std::any::{Any, TypeId};

use ahash::HashMap;
use std::any::Any;
use parking_lot::Mutex;

/// Does memoization of different objects for the immediate mode UI.
#[derive(Default)]
pub struct Caches(HashMap<std::any::TypeId, Box<dyn Cache>>);
pub struct Caches(Mutex<HashMap<TypeId, Box<dyn Cache>>>);

impl Caches {
/// Call once per frame to potentially flush the cache(s).
pub fn begin_frame(&mut self) {
for cache in self.0.values_mut() {
pub fn begin_frame(&self) {
re_tracing::profile_function!();
for cache in self.0.lock().values_mut() {
cache.begin_frame();
}
}

/// Attempt to free up memory.
pub fn purge_memory(&mut self) {
for cache in self.0.values_mut() {
pub fn purge_memory(&self) {
re_tracing::profile_function!();
for cache in self.0.lock().values_mut() {
cache.purge_memory();
}
}

/// Retrieves a cache for reading and writing.
///
/// Adds the cache lazily if it wasn't already there.
pub fn entry<T: Cache + Default>(&mut self) -> &mut T {
let cache = self
.0
.entry(std::any::TypeId::of::<T>())
.or_insert(Box::<T>::default());

cache
.as_any_mut()
.downcast_mut::<T>()
.expect("Downcast failed, this indicates a bug in how `Caches` adds new cache types.")
pub fn entry<C: Cache + Default>(&self) -> parking_lot::MappedMutexGuard<'_, C> {
parking_lot::MutexGuard::map(self.0.lock(), |map| {
map.entry(TypeId::of::<C>())
.or_insert(Box::<C>::default())
.as_any_mut()
.downcast_mut::<C>()
.expect(
"Downcast failed, this indicates a bug in how `Caches` adds new cache types.",
)
})
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/re_viewer_context/src/viewer_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub struct ViewerContext<'a> {
/// Things that need caching and are shared across the whole viewer.
///
/// Use this only for things that you expected be shared across different panels and/or space views.
pub cache: &'a mut Caches,
pub cache: &'a Caches,

/// How to display components.
pub component_ui_registry: &'a ComponentUiRegistry,
Expand Down
4 changes: 2 additions & 2 deletions crates/re_viewport/src/view_tensor/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,11 +178,11 @@ fn paint_tensor_slice(
) -> anyhow::Result<(egui::Response, egui::Painter, egui::Rect)> {
re_tracing::profile_function!();

let tensor_stats = ctx.cache.entry::<TensorStatsCache>().entry(tensor);
let tensor_stats = *ctx.cache.entry::<TensorStatsCache>().entry(tensor);
let colormapped_texture = super::tensor_slice_to_gpu::colormapped_texture(
ctx.render_ctx,
tensor,
tensor_stats,
&tensor_stats,
state,
)?;
let [width, height] = colormapped_texture.texture.width_height();
Expand Down

0 comments on commit 16a9aed

Please sign in to comment.