Render: a buffer kept by another worker comes with its own files

LayerCache::insert returns the files of the buffer it keeps, and Renderer::keep
notes them in the render's log and returns them, so a render that uses another
worker's buffer lists that buffer's files (a file changed between the two reads
then has two identities and the list is incomplete).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
vangel76andClaude Opus 5.5 committed 2026-11-07 01:58:15 +01:00
1 parent 551ae89c6a
commit 8d948c231c
4 files changed
+89 -30

No files matched your search

+1 -1
View File
@@ -358,7 +358,7 @@ impl<'a> Renderer<'a> {
Some(foot)
});
// The buffer the cache keeps (another worker's, if it kept one first).
Some(self.keep(key, Arc::new(foot?), &files))
Some(self.keep(key, Arc::new(foot?), &files).0)
}
/// Draw one 2D layer into `canvas` on the CPU (any kind: adjustment, wireframe, styled…).
+48 -15
View File
@@ -177,7 +177,7 @@ impl LayerCache {
let mut files = ReadFiles::default();
files.mark_incomplete();
let files = Arc::new(files);
Some((self.insert_mem(key, buf, files.clone()), files))
Some(self.insert_mem(key, buf, files))
}
}
}
@@ -188,12 +188,12 @@ impl LayerCache {
/// and packs it once instead of once per worker, which ran it out of memory). Renders don't
/// wait for each other: on the frame workers' shared thread pool a wait can end up under
/// the very work it waits for. `files`: the source files `buf` was made of (see
/// [`crate::reads`]).
pub fn insert(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> Arc<Buf> {
/// [`crate::reads`]); the files returned are the ones the returned buffer was made of.
pub fn insert(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> (Arc<Buf>, Arc<ReadFiles>) {
if self.gated() {
return buf;
return (buf, files);
}
if let Some(kept) = self.inner.lock().ok().and_then(|g| g.map.get(&key).map(|e| e.buf.clone())) {
if let Some(kept) = self.inner.lock().ok().and_then(|g| g.map.get(&key).map(|e| (e.buf.clone(), e.files.clone()))) {
return kept;
}
let started = self.inner.lock().ok().and_then(|mut g| g.missed_at.remove(&key));
@@ -206,21 +206,21 @@ impl LayerCache {
self.insert_mem(key, buf, files)
}
/// See [`LayerCache::insert`]: the buffer kept under `key` (`buf`, unless one was kept first
/// or `buf` is too large to keep).
fn insert_mem(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> Arc<Buf> {
/// See [`LayerCache::insert`]: the buffer kept under `key` and its files (`buf` and `files`,
/// unless one was kept first or `buf` is too large to keep).
fn insert_mem(&self, key: u64, buf: Arc<Buf>, files: Arc<ReadFiles>) -> (Arc<Buf>, Arc<ReadFiles>) {
let bytes = buf.img.data.len() * std::mem::size_of::<effectcraft_raster::Px>();
let Ok(mut g) = self.inner.lock() else { return buf };
let Ok(mut g) = self.inner.lock() else { return (buf, files) };
if bytes > g.budget / 4 {
return buf;
return (buf, files);
}
g.clock += 1;
let now = g.clock;
if let Some(e) = g.map.get_mut(&key) {
e.last_use = now;
return e.buf.clone();
return (e.buf.clone(), e.files.clone());
}
g.map.insert(key, Entry { buf: buf.clone(), files, bytes, last_use: now });
g.map.insert(key, Entry { buf: buf.clone(), files: files.clone(), bytes, last_use: now });
g.bytes += bytes;
if g.bytes > g.budget {
// Evict least-recently used entries down to 3/4 of the budget.
@@ -236,7 +236,7 @@ impl LayerCache {
}
}
}
buf
(buf, files)
}
/// The memory budget in bytes.
@@ -796,8 +796,41 @@ mod shared_insert_tests {
let cache = LayerCache::default();
let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 });
let (a, b) = (buf(), buf());
assert!(Arc::ptr_eq(&cache.insert(3, a.clone(), Default::default()), &a));
assert!(Arc::ptr_eq(&cache.insert(3, b, Default::default()), &a), "the later render gets the first buffer");
assert!(Arc::ptr_eq(&cache.insert(3, a.clone(), Default::default()).0, &a));
assert!(Arc::ptr_eq(&cache.insert(3, b, Default::default()).0, &a), "the later render gets the first buffer");
assert!(Arc::ptr_eq(&cache.get(3).unwrap().0, &a));
}
/// A render that gets another render's buffer gets that buffer's files too (#3184): when a
/// file changed between the two reads, its frame lists the file with both identities, so the
/// list is incomplete and the frame is not kept on disk with the new identity and the old
/// pixels.
#[test]
fn the_buffer_kept_first_brings_its_files() {
use crate::reads::{FileStamp, ReadFile, ReadFiles, ReadLog};
use crate::{NoFootage, RenderOpts, Renderer};
let cache = LayerCache::default();
let buf = || Arc::new(Buf { img: Image::new(4, 4), offset: [0.0; 2], scale: 1.0 });
let files = |len| Arc::new([ReadFile::new("/a.png", FileStamp::Native { len, modified: Some(1) })].into_iter().collect::<ReadFiles>());
let (first, second) = (files(1), files(2));
let a = buf();
cache.insert(3, a.clone(), first.clone());
let project = effectcraft_project::Project::default();
let log = ReadLog::new();
let mut r = Renderer::new(&project, &NoFootage, RenderOpts::default());
r.cache = Some(&cache);
r.reads = Some(&log);
// (what `Renderer::recorded` noted for the second render)
log.note_all(&second);
let (kept, kept_files) = r.keep(Some(3), buf(), &second);
assert!(Arc::ptr_eq(&kept, &a));
assert_eq!(kept_files, first);
assert!(!log.files().is_complete());
// The same files: nothing changes.
let same = ReadLog::new();
r.reads = Some(&same);
same.note_all(&first);
let _ = r.keep(Some(3), buf(), &files(1));
assert!(same.files().is_complete());
}
}
+21 -4
View File
@@ -59,10 +59,27 @@ impl<'a> Renderer<'a> {
t.effects.push(("layerStyles".into(), t0.elapsed().as_secs_f64() * 1e3));
}
// The buffers the cache keeps (another worker's, if it kept them first), made of the
// content's files.
let body = self.keep(key, Arc::new(st.body), &files);
let passes: Vec<_> =
st.passes.into_iter().enumerate().map(|(i, (b, m))| (self.keep(key.map(|k| cache::derive(k, i as u64 + 1)), Arc::new(b), &files), m)).collect();
// content's files; the styled layer then lists the files of every buffer it holds.
let mut kept_files = vec![];
let (body, f) = self.keep(key, Arc::new(st.body), &files);
kept_files.push(f);
let passes: Vec<_> = st
.passes
.into_iter()
.enumerate()
.map(|(i, (b, m))| {
let (b, f) = self.keep(key.map(|k| cache::derive(k, i as u64 + 1)), Arc::new(b), &files);
kept_files.push(f);
(b, m)
})
.collect();
let files = if kept_files.iter().all(|f| Arc::ptr_eq(f, &files)) {
files
} else {
let mut all = (*files).clone();
kept_files.iter().for_each(|f| all.extend(f));
Arc::new(all)
};
Some(StyledLayer { passes, body, content, key, ckey, plain: false, files })
}
+19 -10
View File
@@ -151,12 +151,19 @@ impl<'a> Renderer<'a> {
}
/// Keep `buf`, made of `files`, under `key` (see [`Renderer::recorded`]): the buffer the cache
/// keeps (another worker's, if it kept one first), or `buf` when nothing is kept.
pub(crate) fn keep(&self, key: Option<u64>, buf: Arc<Buf>, files: &Arc<ReadFiles>) -> Arc<Buf> {
match (self.cache_to_keep(), key) {
(Some(c), Some(k)) => c.insert(k, buf, files.clone()),
_ => buf,
/// keeps (another worker's, if it kept one first), or `buf` when nothing is kept, with the
/// files that buffer was made of. Another worker's files are noted in [`Renderer::reads`]
/// too: its pixels are the ones this render uses (a file that changed between the two reads
/// then has two identities, and the list is incomplete).
pub(crate) fn keep(&self, key: Option<u64>, buf: Arc<Buf>, files: &Arc<ReadFiles>) -> (Arc<Buf>, Arc<ReadFiles>) {
let (Some(c), Some(k)) = (self.cache_to_keep(), key) else { return (buf, files.clone()) };
let (kept, kept_files) = c.insert(k, buf, files.clone());
if !Arc::ptr_eq(&kept_files, files)
&& let Some(log) = self.reads
{
log.note_all(&kept_files);
}
(kept, kept_files)
}
/// Run `make`, which makes a buffer to keep under `key` in the layer cache, on this renderer
@@ -923,7 +930,7 @@ impl<'a> Renderer<'a> {
});
if let (Some(a), Some(b)) = (a, b) {
let img = blend_frames(mode, &a, &b, w as f32);
self.keep(key, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }), &files);
let _kept = self.keep(key, Arc::new(Buf { img: img.clone(), offset: [0.0; 2], scale: 1.0 }), &files);
return Some(Arc::new(img));
}
}
@@ -967,7 +974,7 @@ impl<'a> Renderer<'a> {
}
let mut nb = (*b).clone();
color::convert(&mut nb.img, &c);
self.keep(key, Arc::new(nb), files)
self.keep(key, Arc::new(nb), files).0
}
/// The flattened layer buffer, its cache key and the source files it was made of.
@@ -981,7 +988,8 @@ impl<'a> Renderer<'a> {
return Some((b, key, files));
}
let flat = Arc::new(styles::Styled { passes: st.passes.iter().map(|(b, m)| ((**b).clone(), *m)).collect(), body: (*st.body).clone() }.flatten());
Some((self.keep(key, flat, &st.files), key, st.files))
let (flat, files) = self.keep(key, flat, &st.files);
Some((flat, key, files))
}
/// A layer's source pixels at the context time, before masks, effects and the transform
@@ -1039,7 +1047,8 @@ impl<'a> Renderer<'a> {
let buf = r.masked_source(ctx, layer)?;
Some(r.apply_effects_timed(ctx, layer, buf, false, usize::MAX, timing.map(|t| &mut t.effects)))
});
Some((self.keep(key, Arc::new(buf?), &files), key, files))
let (buf, files) = self.keep(key, Arc::new(buf?), &files);
Some((buf, key, files))
}
/// A layer's input at the context time: source → masks → its first `effects` effects
@@ -1061,6 +1070,6 @@ impl<'a> Renderer<'a> {
}
Some(buf)
});
Some(self.keep(key, Arc::new(buf?), &files))
Some(self.keep(key, Arc::new(buf?), &files).0)
}
}