From 18eb34b01252d32333e2dedbc98288960d120641 Mon Sep 17 00:00:00 2001 From: Nicolas Falesy <112726756+nicolasfalesy@users.noreply.github.com> Date: Thu, 22 Oct 2026 09:48:43 -0400 Subject: [PATCH] fix(gpu): an exact resident budget returns the loan and lends nothing (CodeRabbit) set_resident_budget now gives the borrowed bytes back to the effect-map share before setting the exact budget, so memory_budget() stays the total, and fit_layers lends nothing until the next set_memory_budget. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01CD2ipSyvx1tYwnHJZyDs59 --- crates/gpu/src/lib.rs | 11 +++++++++-- crates/gpu/tests/parity.rs | 7 +++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/crates/gpu/src/lib.rs b/crates/gpu/src/lib.rs index 25706b969b..d270b55802 100644 --- a/crates/gpu/src/lib.rs +++ b/crates/gpu/src/lib.rs @@ -472,6 +472,8 @@ pub struct Compositor { layer_need: u64, /// Bytes of `fx_budget` lent to `resident_budget` so those pages fit. lent: u64, + /// `resident_budget` was set exactly ([`Compositor::set_resident_budget`]): nothing is lent. + exact_resident: bool, /// What the view shows: a full refresh draws these cells last, so their pages are the /// ones still resident for the edits that follow. focus: Option, @@ -744,6 +746,7 @@ impl Compositor { fx_budget: (FX_BUDGET as u64).saturating_sub(prefix::MAX_BYTES), layer_need: 0, lent: 0, + exact_resident: false, focus: None, frame: 0, stamp: 0, @@ -793,8 +796,11 @@ impl Compositor { /// Set the GPU memory resident layer pages may hold (default [`RESIDENT_BUDGET`]), exactly: /// nothing is lent to it ([`Self::fit_layers`]) until the next [`Self::set_memory_budget`]. pub fn set_resident_budget(&mut self, bytes: u64) { - self.resident_budget = bytes; + // The loan goes back to the effect maps, so the total budget stays what it was. + self.fx_budget = self.fx_budget.saturating_add(self.lent); self.lent = 0; + self.resident_budget = bytes; + self.exact_resident = true; } /// Set the GPU memory the compositor may hold for layer pages and caches together @@ -806,6 +812,7 @@ impl Compositor { self.fx_budget = caches.saturating_sub(self.prefix.budget()); self.resident_budget = bytes.saturating_sub(caches); self.lent = 0; + self.exact_resident = false; self.lend(); } @@ -831,7 +838,7 @@ impl Compositor { self.fx_budget = self.fx_budget.saturating_add(self.lent); self.resident_budget = self.resident_budget.saturating_sub(self.lent); let short = self.layer_need.saturating_sub(self.resident_budget); - self.lent = if short <= self.fx_budget { short } else { 0 }; + self.lent = if short <= self.fx_budget && !self.exact_resident { short } else { 0 }; self.fx_budget = self.fx_budget.saturating_sub(self.lent); self.resident_budget = self.resident_budget.saturating_add(self.lent); } diff --git a/crates/gpu/tests/parity.rs b/crates/gpu/tests/parity.rs index 5a34e88679..4498396eab 100644 --- a/crates/gpu/tests/parity.rs +++ b/crates/gpu/tests/parity.rs @@ -2872,6 +2872,13 @@ fn layer_pages_that_just_miss_borrow_the_effect_map_share() { comp.fit_layers(need, true); assert_eq!(comp.fit_layers(own, true), own); assert_eq!(comp.memory_budget(), budget); + // An exact resident budget returns the loan and lends nothing until the next memory budget. + comp.fit_layers(need, true); + comp.set_resident_budget(own); + assert_eq!(comp.memory_budget(), budget, "the loan went back to the effect maps"); + assert_eq!(comp.fit_layers(need, true), own, "an exact budget stays exact"); + comp.set_memory_budget(budget); + assert_eq!(comp.resident_budget(), need, "the next memory budget lends again"); } // A pass-through group holding an adjustment renders isolated when layers clip to it, on the GPU