From bbcc832103c4367be3c01ffac2cd1839cf2c0e50 Mon Sep 17 00:00:00 2001 From: Higgs32584 Date: Tue, 20 Oct 2026 20:54:05 -0400 Subject: [PATCH] Background redraw: keep finished composites within Memory Usage as it is now A continued redraw reused the cap from when it began, and the finished composite was stored with it; after Memory Usage dropped (or the document grew) it could be kept over the current allowance. The cap is taken again when a redraw continues and when it finishes (once, not per frame). Co-Authored-By: Claude Opus 5.5 --- crates/ui-egui/src/background_redraw.rs | 21 ++++++++++++------- crates/ui-egui/src/background_redraw_tests.rs | 15 +++++++++++++ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/crates/ui-egui/src/background_redraw.rs b/crates/ui-egui/src/background_redraw.rs index 5b8b5d7d867..1fafd98cb2b 100644 --- a/crates/ui-egui/src/background_redraw.rs +++ b/crates/ui-egui/src/background_redraw.rs @@ -208,18 +208,21 @@ pub(crate) fn start( ) { let all = blocks(doc.bounds()); let display_key = display.as_deref().map_or(0, |d| d.texture_key); + // Taken again when a running redraw continues: the edit (or Memory Usage) may have changed it. + let cap = keep_cap(app, doc.id); + app.kept.trim(cap); let (mut pending, since, fallback, mut keep) = match app.redraws.remove(&doc.id) { Some(mut old) if old.blocks == all => { // Blocks drawn from another state aren't part of this one's composite. let same = old.display == display_key && (Arc::ptr_eq(&old.doc, &doc) || *old.doc == *doc); - let (collected, bytes) = if same { (std::mem::take(&mut old.collected), old.collected_bytes) } else { (vec![None; all.len()], 0) }; - (std::mem::take(&mut old.pending), (old.started_ms, old.drawn), fallback.or_else(|| old.fallback.take()), (collected, bytes, old.cap)) - } - _ => { - let cap = keep_cap(app, doc.id); - app.kept.trim(cap); - (vec![false; all.len()], (crate::gpu_canvas::now_ms(), 0), fallback, (vec![None; all.len()], 0, cap)) + let (collected, bytes) = if same && old.collected_bytes <= cap { + (std::mem::take(&mut old.collected), old.collected_bytes) + } else { + (vec![None; all.len()], 0) + }; + (std::mem::take(&mut old.pending), (old.started_ms, old.drawn), fallback.or_else(|| old.fallback.take()), (collected, bytes, cap)) } + _ => (vec![false; all.len()], (crate::gpu_canvas::now_ms(), 0), fallback, (vec![None; all.len()], 0, cap)), }; for (p, b) in pending.iter_mut().zip(&all) { *p |= !b.intersect(®ion).is_empty(); @@ -293,7 +296,9 @@ pub(crate) fn poll(app: &mut PhotocraftApp, gpu: &GpuCanvas, doc: DocId) { texels, bytes: r.collected_bytes, }; - app.kept.store(k, &r.doc, r.cap); + // The cap as it is now, not when the redraw began (once per redraw, not per frame). + let cap = keep_cap(app, doc); + app.kept.store(k, &r.doc, cap); } let ms = crate::gpu_canvas::now_ms() - r.started_ms; app.perf.record("background", r.drawn, ms, 0.0); diff --git a/crates/ui-egui/src/background_redraw_tests.rs b/crates/ui-egui/src/background_redraw_tests.rs index 72dcf94adcc..0d8d465d5ed 100644 --- a/crates/ui-egui/src/background_redraw_tests.rs +++ b/crates/ui-egui/src/background_redraw_tests.rs @@ -266,6 +266,21 @@ fn showing_a_hidden_layer_again_reuses_the_redraw_from_before_it_was_hidden() { assert_matches_synchronous(&mut g, doc); } +/// Memory Usage lowered while a redraw runs: the finished composite is kept within what it leaves +/// now, not what it left when the redraw began. +#[test] +fn a_redraw_finishing_after_memory_usage_drops_keeps_nothing_over_it() { + let _gpu = photocraft_testkit::gpu_lock(); + let Some(mut g) = gpu_app() else { return }; + let doc = cpu_only_document(g.harness.state_mut(), 4096, 3072); + g.harness.step(); + assert!(g.harness.state().redraws.contains_key(&doc)); + // Less than the document's own pixels: nothing is left for kept composites. + g.harness.state_mut().session.edit_prefs(|p| p.performance.memory_usage_mb = 1); + settle(&mut g); + assert!(g.harness.state().kept.entries.is_empty(), "a composite was kept over the current cap"); +} + #[test] fn closing_the_document_stops_its_redraw() { let _gpu = photocraft_testkit::gpu_lock();