diff --git a/crates/treehouse/src/vfs.rs b/crates/treehouse/src/vfs.rs index 9ff75d9..20015c9 100644 --- a/crates/treehouse/src/vfs.rs +++ b/crates/treehouse/src/vfs.rs @@ -81,7 +81,7 @@ pub struct DirEntry { pub path: VPathBuf, } -#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)] +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub struct ImageSize { pub width: u32, pub height: u32, diff --git a/crates/treehouse/src/vfs/content_cache.rs b/crates/treehouse/src/vfs/content_cache.rs index ae7e16d..0007878 100644 --- a/crates/treehouse/src/vfs/content_cache.rs +++ b/crates/treehouse/src/vfs/content_cache.rs @@ -11,7 +11,7 @@ use super::{walk_dir_rec, Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf}; pub struct ContentCache { inner: T, - cache: DashMap>>, + cache: DashMap>, } impl ContentCache { @@ -48,10 +48,13 @@ where } fn content(&self, path: &VPath) -> Option> { - self.cache - .entry(path.to_owned()) - .or_insert_with(|| self.inner.content(path)) - .clone() + self.cache.get(path).map(|x| x.clone()).or_else(|| { + let content = self.inner.content(path); + if let Some(content) = &content { + self.cache.insert(path.to_owned(), content.clone()); + } + content + }) } fn content_version(&self, path: &VPath) -> Option { diff --git a/crates/treehouse/src/vfs/content_version_cache.rs b/crates/treehouse/src/vfs/content_version_cache.rs index 69cd967..7ec1a5e 100644 --- a/crates/treehouse/src/vfs/content_version_cache.rs +++ b/crates/treehouse/src/vfs/content_version_cache.rs @@ -6,7 +6,7 @@ use super::{Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf}; pub struct Blake3ContentVersionCache { inner: T, - cache: DashMap>, + cache: DashMap, } impl Blake3ContentVersionCache { @@ -31,15 +31,16 @@ where } fn content_version(&self, path: &VPath) -> Option { - self.cache - .entry(path.to_owned()) - .or_insert_with(|| { - self.content(path).map(|content| { - let hash = blake3::hash(&content).to_hex(); - format!("b3-{}", &hash[0..8]) - }) - }) - .clone() + self.cache.get(path).map(|x| x.clone()).or_else(|| { + let version = self.inner.content(path).map(|content| { + let hash = blake3::hash(&content).to_hex(); + format!("b3-{}", &hash[0..8]) + }); + if let Some(version) = &version { + self.cache.insert(path.to_owned(), version.clone()); + } + version + }) } fn image_size(&self, path: &VPath) -> Option { diff --git a/crates/treehouse/src/vfs/image_size_cache.rs b/crates/treehouse/src/vfs/image_size_cache.rs index 2255f64..5a7b008 100644 --- a/crates/treehouse/src/vfs/image_size_cache.rs +++ b/crates/treehouse/src/vfs/image_size_cache.rs @@ -10,7 +10,7 @@ use super::{Dir, DirEntry, EditPath, ImageSize, VPath, VPathBuf}; pub struct ImageSizeCache { inner: T, - cache: DashMap>, + cache: DashMap, } impl ImageSizeCache { @@ -59,15 +59,17 @@ where } fn image_size(&self, path: &VPath) -> Option { - self.cache - .entry(path.to_owned()) - .or_insert_with(|| { - self.compute_image_size(path) - .inspect_err(|err| warn!("compute_image_size({path}) failed: {err:?}")) - .ok() - .flatten() - }) - .clone() + self.cache.get(path).map(|x| *x).or_else(|| { + let image_size = self + .compute_image_size(path) + .inspect_err(|err| warn!("compute_image_size({path}) failed: {err:?}")) + .ok() + .flatten(); + if let Some(image_size) = image_size { + self.cache.insert(path.to_owned(), image_size); + } + image_size + }) } fn anchor(&self, path: &VPath) -> Option {