From 9bbc0847adbeef10862bee8da1243477cc4888a5 Mon Sep 17 00:00:00 2001 From: vangel76 Date: Wed, 18 Nov 2026 12:57:37 +0100 Subject: [PATCH] Caching: a still cached both whole and as a frame can be let go, and a plane entry without memory to seal it is skipped (#3750 review) --- crates/media/src/pool.rs | 96 ++++++++++++++++++++++++++------- crates/render/src/disk_cache.rs | 62 ++++++++++++++++++--- 2 files changed, 133 insertions(+), 25 deletions(-) diff --git a/crates/media/src/pool.rs b/crates/media/src/pool.rs index 731b4675b..81fa2f9a2 100644 --- a/crates/media/src/pool.rs +++ b/crates/media/src/pool.rs @@ -71,11 +71,23 @@ enum Pixels { } impl Pixels { - /// Nobody but the cache holds the pixels: dropping them frees their memory. - fn unshared(&self) -> bool { + /// Nobody but the cache holds the pixels: dropping the cache's references frees their memory. + /// `own` counts the cache's own references to them ([`CacheState::own_refs`]): one picture can + /// be cached under two keys, or both as a frame and as a whole still (#3750 review: a still + /// asked for reduced and then whole is one picture in both), and those are not other holders. + fn unshared(&self, own: impl FnOnce(*const ()) -> usize) -> bool { + let holders = match self { + Pixels::F32(img) => Arc::strong_count(img), + Pixels::Q8(f) => Arc::strong_count(f), + }; + holders <= 1 || holders <= own(self.addr()) + } + + /// Where the pixels are (the same for every reference to them). + fn addr(&self) -> *const () { match self { - Pixels::F32(img) => Arc::strong_count(img) == 1, - Pixels::Q8(f) => Arc::strong_count(f) == 1, + Pixels::F32(img) => Arc::as_ptr(img).cast(), + Pixels::Q8(f) => Arc::as_ptr(f).cast(), } } @@ -208,23 +220,21 @@ impl Lru { } } /// The least recently used frame it may drop and its last use: not the newest (it always - /// stays) and not one whose pixels someone else holds (dropping it would free nothing yet). - fn oldest(&self) -> Option<(&Key, u64)> { + /// stays) and not one whose pixels someone outside the cache holds (dropping it would free + /// nothing yet). `own`: the cache's own references to pixels ([`CacheState::own_refs`]). + fn oldest(&self, own: impl Fn(*const ()) -> usize) -> Option<(&Key, u64)> { if self.map.len() <= 1 { return None; } let newest = self.order.last_key_value().map(|(s, _)| *s); self.order.iter().filter(|(s, _)| Some(**s) != newest).find_map(|(_, k)| { let e = self.map.get(k)?; - e.img.unshared().then_some((k, e.used)) + e.img.unshared(&own).then_some((k, e.used)) }) } - /// Drop the least recently used frame it may drop; the bytes freed. - fn drop_oldest(&mut self) -> usize { - let Some(k) = self.oldest().map(|(k, _)| k.clone()) else { return 0 }; - let had = self.bytes; - self.remove(&k); - had.saturating_sub(self.bytes) + /// The frames holding the pixels at `addr`. + fn refs(&self, addr: *const ()) -> usize { + self.map.values().filter(|e| e.img.addr() == addr).count() } /// The bytes of the recycled buffers ([`Lru::spare`]). fn spare_bytes(&self) -> usize { @@ -398,10 +408,33 @@ impl CacheState { self.lru.bytes.saturating_add(self.lru.spare_bytes()).saturating_add(self.recent_bytes()) } - /// The oldest whole still in [`CacheState::recent`] that nobody else holds (dropping one in - /// use would free nothing yet). + /// The oldest whole still in [`CacheState::recent`] that nobody outside the cache holds + /// (dropping one in use would free nothing yet; one also cached as a frame is the cache's). fn oldest_recent(&self) -> Option { - self.recent.iter().position(|r| Arc::strong_count(&r.img) == 1) + self.recent.iter().position(|r| { + let holders = Arc::strong_count(&r.img); + holders <= 1 || holders <= self.own_refs(Arc::as_ptr(&r.img).cast()) + }) + } + + /// The references the cache itself holds to the pixels at `addr`: its frames and whole + /// stills holding them (#3750 review: a whole still kept for a reduced size and then asked + /// for whole is in both). + fn own_refs(&self, addr: *const ()) -> usize { + self.lru.refs(addr).saturating_add(self.recent.iter().filter(|r| Arc::as_ptr(&r.img).cast::<()>() == addr).count()) + } + + /// The least recently used frame it may drop ([`Lru::oldest`]) and its last use. + fn oldest_frame(&self) -> Option<(&Key, u64)> { + self.lru.oldest(|addr| self.own_refs(addr)) + } + + /// Drop the least recently used frame it may drop; the bytes freed. + fn drop_oldest_frame(&mut self) -> usize { + let Some(k) = self.oldest_frame().map(|(k, _)| k.clone()) else { return 0 }; + let had = self.lru.bytes; + self.lru.remove(&k); + had.saturating_sub(self.lru.bytes) } /// The bytes of the whole stills [`CacheState::recent`] holds. @@ -1776,7 +1809,7 @@ impl effectcraft_render::memory::MemoryHolder for Inner { return Some(Candidate { used: 0, bytes: b.len().saturating_mul(std::mem::size_of::()).max(1), cost_us: 0, protected: false }); } let frame = - c.lru.oldest().and_then(|(k, _)| c.lru.map.get(k)).map(|e| Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us, protected: false }); + c.oldest_frame().and_then(|(k, _)| c.lru.map.get(k)).map(|e| Candidate { used: e.used, bytes: e.bytes, cost_us: e.cost_us, protected: false }); let still = c.oldest_recent().and_then(|i| c.recent.get(i)).map(|r| Candidate { used: r.used, bytes: r.bytes.max(1), cost_us: 0, protected: false }); match (frame, still) { (Some(f), Some(s)) => Some(if s.used < f.used { s } else { f }), @@ -1788,13 +1821,13 @@ impl effectcraft_render::memory::MemoryHolder for Inner { if !c.lru.spare.is_empty() { return c.lru.drop_spare(); } - let frame = c.lru.oldest().map(|(_, t)| t); + let frame = c.oldest_frame().map(|(_, t)| t); let at = c.oldest_recent(); let still = at.and_then(|i| c.recent.get(i)).map(|r| r.used); match (frame, still, at) { (Some(f), Some(s), Some(i)) if s < f => c.recent.remove(i).map_or(0, |r| r.bytes.max(1)), (None, Some(_), Some(i)) => c.recent.remove(i).map_or(0, |r| r.bytes.max(1)), - _ => c.lru.drop_oldest(), + _ => c.drop_oldest_frame(), } } } @@ -2241,6 +2274,31 @@ mod tests { drop((held, held_still)); } + /// #3750 review: a still asked for reduced and then whole is one picture both kept as a whole + /// still and cached as a frame; once nobody else holds it, those two references are the + /// cache's own, and the accountant can let it go. + #[test] + fn a_still_cached_both_as_frame_and_whole_still_is_offered() { + let pool = MediaPool::unregistered(usize::MAX); + let holder = pool.memory_holder(); + let f = plate(&pool, "/both.exr"); + Inner::frame_at(&pool.inner, &f, Tick::ZERO, true, Some((8, 4)), FloatCurve::Srgb).unwrap(); + let whole = Inner::frame_at(&pool.inner, &f, Tick::ZERO, true, None, FloatCurve::Srgb).unwrap(); + { + let c = lock(&pool.inner.cache); + assert!(c.recent.iter().any(|r| Arc::ptr_eq(&r.img, &whole)), "kept as a whole still"); + assert!(c.lru.map.values().any(|e| matches!(&e.img, Pixels::F32(i) if Arc::ptr_eq(i, &whole))), "and cached as a frame"); + } + assert!(holder.oldest().is_some()); + // Held by the caller: the whole still is not offered (only the reduced frame is). + assert_eq!(holder.drop_oldest(), 8 * 4 * std::mem::size_of::() + 64, "the reduced frame"); + assert!(holder.oldest().is_none(), "the picture the caller holds stays"); + drop(whole); + let still = 16 * 8 * std::mem::size_of::(); + assert_eq!(holder.drop_oldest(), still, "nobody else holds it: the whole still goes"); + assert!(lock(&pool.inner.cache).recent.is_empty()); + } + /// A frame kept as 8-bit levels is its own entry beside the float one, counted at 4 bytes /// a pixel. #[test] diff --git a/crates/render/src/disk_cache.rs b/crates/render/src/disk_cache.rs index 4821a5632..4754644a5 100644 --- a/crates/render/src/disk_cache.rs +++ b/crates/render/src/disk_cache.rs @@ -1106,7 +1106,9 @@ impl DiskCache { self.write_failed(&e); return; } - let data = seal_format(kind, format, payload); + // (no memory for the sealed entry just now: it isn't written; a plane is decoded from its + // file next time) + let Some(data) = try_seal_format(kind, format, payload) else { return }; // An entry that cannot fit would be evicted straight away. if data.len() as u64 > self.limit_bytes() { return; @@ -1578,13 +1580,32 @@ fn writer(dc: std::sync::Weak, q: Arc<(Mutex, Condvar)>) { /// [`FORMAT_LAYER_FILES`] for layer buffers), payload, checksum. fn seal_format(kind: Kind, format: u8, payload: &[u8]) -> Vec { let mut data = Vec::with_capacity(payload.len() + 16); + seal_into(&mut data, kind, format, payload); + data +} + +/// [`seal_format`] with its memory taken fallibly: `None` when the system can't give it (a +/// poorly compressible 16K plane's entry is about 1 GiB), and the entry is then not written. +fn try_seal_format(kind: Kind, format: u8, payload: &[u8]) -> Option> { + let len = payload.len().checked_add(16)?; + #[cfg(test)] + if len > NO_SEAL_OVER.with(std::cell::Cell::get) { + return None; + } + let mut data = Vec::new(); + data.try_reserve_exact(len).ok()?; + seal_into(&mut data, kind, format, payload); + Some(data) +} + +/// The header, `payload` and checksum of an entry appended to `data` (room for them reserved). +fn seal_into(data: &mut Vec, kind: Kind, format: u8, payload: &[u8]) { data.extend_from_slice(MAGIC); data.push(kind.tag()); data.extend_from_slice(&[format, 0, 0]); data.extend_from_slice(payload); - let sum = checksum(&data); + let sum = checksum(data.as_slice()); data.extend_from_slice(&sum.to_le_bytes()); - data } /// A sealed 8-bit frame entry with the source files it was made of: what the browser's frame @@ -1827,11 +1848,12 @@ fn encode_plane(w: u32, h: u32, name: &str, plane: &[f32]) -> Vec { /// channel: half the bytes); type 2: `f32` (bit-exact: Cryptomatte IDs are float bit patterns). /// The scratch memory and the result are taken fallibly: `None` when the system can't give them. fn encode_plane_after(mut out: Vec, w: u32, h: u32, name: &str, plane: &[f32]) -> Option> { - out.try_reserve(64).ok()?; - out.extend_from_slice(&w.to_le_bytes()); - out.extend_from_slice(&h.to_le_bytes()); let name = name.as_bytes(); let len = name.len().min(u16::MAX as usize); + // (the size, the name and the sample type) + out.try_reserve(len.checked_add(64)?).ok()?; + out.extend_from_slice(&w.to_le_bytes()); + out.extend_from_slice(&h.to_le_bytes()); out.extend_from_slice(&(len as u16).to_le_bytes()); out.extend_from_slice(name.get(..len).unwrap_or_default()); let n = plane.len(); @@ -1876,6 +1898,9 @@ thread_local! { /// Allocations [`zeroed`] refuses on this thread: those over this many bytes (tests: memory /// the system can't give). static NO_MEMORY_OVER: std::cell::Cell = const { std::cell::Cell::new(usize::MAX) }; + /// Sealed entries [`try_seal_format`] can't get memory for on this thread: those over this + /// many bytes (tests). + static NO_SEAL_OVER: std::cell::Cell = const { std::cell::Cell::new(usize::MAX) }; } /// The name and plane of a plane entry's pixels ([`encode_plane_after`]) of `expect` (width, height); @@ -2420,6 +2445,31 @@ mod tests { let _ = std::fs::remove_dir_all(&dir); } + /// #3750 review: a plane entry that can't get the memory to be sealed (a poorly compressible + /// 16K plane's is about 1 GiB) is not written, and the planes after it are; a sealed entry + /// is the same bytes either way. + #[test] + fn a_plane_entry_without_memory_to_seal_is_skipped_and_the_rest_written() { + let dir = tmpdir("planes-no-seal"); + let dc = DiskCache::open(&dir, 1 << 30).unwrap(); + let (files, _) = plane_source(&dir.join("src")); + // (noise compresses poorly: its entry is over 2 KiB; the flat plane's is far under) + let noise: Vec = (0..1024u32).map(|i| f32::from_bits(0x3f00_0000 | i.wrapping_mul(2_654_435_761) >> 9)).collect(); + let flat = vec![0.5f32; 1024]; + let job = Payload::Planes(32, 32, vec![(1, "A.id".to_string(), noise.clone()), (2, "A.R".to_string(), flat)], files.clone()); + NO_SEAL_OVER.with(|c| c.set(2048)); + job.write(&dc, Kind::Plane, 0, FORMAT_PLANES_FILES); + NO_SEAL_OVER.with(|c| c.set(usize::MAX)); + assert!(!dc.contains(Kind::Plane, 1), "no memory to seal it: not written"); + assert!(dc.get_plane(2, 32, 32).is_some(), "the next plane is written"); + let mut entry = Vec::new(); + files.encode(&mut entry); + let entry = encode_plane_after(entry, 32, 32, "A.id", &noise).unwrap(); + assert!(entry.len() > 2048); + assert_eq!(try_seal_format(Kind::Plane, FORMAT_PLANES_FILES, &entry), Some(seal_format(Kind::Plane, FORMAT_PLANES_FILES, &entry)), "the same bytes"); + let _ = std::fs::remove_dir_all(&dir); + } + /// #3271: a queued plane write owns its planes, counts them for the memory accountant (all /// of them, not a share of the store's set) and frees them when the accountant drops it; /// frames stay. 69 channels are queued without loss, each written as an entry of its own