Hold only the table's shown portraits (#32)
CI / core (push) Successful in 1m37s

The champion table drew its 128 px portraits through GPUI's app-wide
image cache, which keeps every image it ever loaded: once « Statistiques »
had been scrolled through, all 173 stayed decoded and in the atlas, about
25 MB on the reference laptop (budget 4). The table now reads them
through its own CardImages, as the card grids do: only the rows it paints
keep theirs, and it gives them back when « Défi » or « Paramètres » is
shown.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0192He3DgZBggc9wJG9c1nqA
This commit is contained in:
nessarandClaude Opus 5.5 committed 2026-11-23 10:20:30 +01:00
1 parent f53db09d3e
commit b8e49276ab
4 files changed
+95 -5

No files matched your search

+2 -1
View File
@@ -1,5 +1,6 @@
//! The portraits of a card grid: decoded only for the cards it paints, and freed, in memory and
//! on the GPU, once it paints them no more (rule #1: memory in proportion to what is shown).
//! on the GPU, once it paints them no more (rule #1: memory in proportion to what is shown). The
//! champion table holds its rows' portraits the same way.
//!
//! Each grid owns one `CardImages`, which its cards' `img` elements read through (`provide`),
//! never through GPUI's app-wide asset cache, which keeps every image it ever loaded. A grid may
+85 -2
View File
@@ -9,6 +9,7 @@ use gpui_kit::component::{ActiveTheme as _, Icon, IconName, StyledExt as _};
use gpui_kit::prelude::FluentBuilder as _;
use gpui_kit::*;
use crate::card_images::{self, CardImages};
use crate::text::{decimal, state_label};
struct Spec {
@@ -49,6 +50,9 @@ pub struct ChampionTable {
/// The clicked champion's id: it stays selected through sorting and reloads.
selected: Option<u32>,
scroll: UniformListScrollHandle,
/// The portraits of the rows painted, never GPUI's app-wide cache, which would keep every
/// one scrolled past (budget 4). Created at the first render.
images: Option<Entity<CardImages>>,
/// Guards the cache in tests.
#[cfg(test)]
pub(crate) renders: usize,
@@ -62,11 +66,25 @@ impl ChampionTable {
sort: None,
selected: None,
scroll: UniformListScrollHandle::new(),
images: None,
#[cfg(test)]
renders: 0,
}
}
/// The table is not shown any more: its portraits are given back, in memory and in the
/// window's atlas, and load again when it is drawn.
pub fn release_images(&mut self, window: &mut Window, cx: &mut Context<Self>) {
let released = self
.images
.as_ref()
.is_some_and(|images| images.update(cx, |images, cx| images.release(window, cx)));
if released {
// A cached view replays its last paint unless it is drawn again.
cx.notify();
}
}
/// Replaces the rows (already filtered) and keeps them sorted.
pub fn set_rows(&mut self, rows: Vec<ChampionRow>, in_season: bool) {
self.rows = rows;
@@ -74,6 +92,11 @@ impl ChampionTable {
self.resort();
}
#[cfg(test)]
pub(crate) fn images(&self) -> Option<&Entity<CardImages>> {
self.images.as_ref()
}
#[cfg(test)]
pub(crate) fn names(&self) -> Vec<String> {
self.rows.iter().map(|r| r.champion.name.clone()).collect()
@@ -232,11 +255,15 @@ fn cell_text(row: &ChampionRow, key: SortKey) -> String {
}
impl Render for ChampionTable {
fn render(&mut self, _: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
#[cfg(test)]
{
self.renders += 1;
}
let images = (self.images)
.get_or_insert_with(|| CardImages::new(cx))
.clone();
images.update(cx, |images, cx| images.start_render(window, cx));
let theme = cx.theme();
let (background, border, radius, muted) = (
theme.table,
@@ -265,7 +292,7 @@ impl Render for ChampionTable {
.track_scroll(&self.scroll)
.into_any_element()
};
div()
card_images::provide(div(), images)
.v_flex()
.relative()
.size_full()
@@ -321,6 +348,62 @@ pub(crate) mod tests {
}
}
// Only the rows painted keep their portraits, and none is kept once the table is not shown.
#[gpui::test]
fn the_table_holds_only_the_portraits_of_the_rows_it_paints(cx: &mut gpui_kit::TestAppContext) {
use gpui_kit::{
AppContext as _, Bounds, VisualTestContext, WindowBounds, WindowOptions, point, px,
size,
};
use crate::card_images::tests::{PORTRAIT_JPEG, TestDir};
let dir = TestDir::new("table-portraits");
let rows: Vec<ChampionRow> = (1..=170)
.map(|id| {
let path = dir.path().join(format!("{id}.jpg"));
std::fs::write(&path, PORTRAIT_JPEG).unwrap();
let mut row = row(&format!("Champion {id:03}"));
row.champion.id = id;
row.portrait = Some(path);
row
})
.collect();
cx.update(gpui_kit::init);
let (window, table) = cx.update(|cx| {
let options = WindowOptions {
window_bounds: Some(WindowBounds::Windowed(Bounds {
origin: point(px(0.), px(0.)),
size: size(px(1280.), px(860.)),
})),
..Default::default()
};
gpui_kit::open_window(options, cx, |_, cx| {
cx.new(|_| {
let mut table = ChampionTable::new();
table.set_rows(rows, false);
table
})
})
.unwrap()
});
let cx = &mut VisualTestContext::from_window(window, cx);
cx.update(|window, cx| window.draw(cx).clear(cx));
cx.run_until_parked();
cx.update(|window, cx| window.draw(cx).clear(cx));
let images = table.read_with(cx, |table, _| table.images().cloned().unwrap());
let held = images.read_with(cx, |images, _| images.loaded().len());
// The rows in the window's 860 px at 32 px each, not the 170.
assert!((10..=30).contains(&held), "{held} portraits held");
// Checked before the next frame: here the table is still the window, and draws again.
cx.update(|window, cx| {
table.update(cx, |table, cx| table.release_images(window, cx));
assert!(images.read(cx).held().is_empty());
});
}
#[test]
fn missing_values_show_a_dash() {
let row = row("Ahri");
+4
View File
@@ -407,6 +407,10 @@ impl Page {
// Nor is the banner: its illustration too.
self.banner_art
.update(cx, |art, cx| art.release(window, cx));
} else {
// Nor is the table: its portraits.
self.table
.update(cx, |table, cx| table.release_images(window, cx));
}
}
+4 -2
View File
@@ -10,8 +10,8 @@ impl Page {
}
/// The page gives way to another screen (« Paramètres »): what it shows is given back, the
/// grids' portraits and the banner's splash, in memory and in the window's atlas. They load
/// again when it is drawn.
/// grids' and the table's portraits and the banner's splash, in memory and in the window's
/// atlas. They load again when it is drawn.
pub(crate) fn hidden(&mut self, window: &mut Window, cx: &mut Context<Self>) {
self.covered = true;
self.end_stage(cx);
@@ -22,6 +22,8 @@ impl Page {
}
self.banner_art
.update(cx, |art, cx| art.release(window, cx));
self.table
.update(cx, |table, cx| table.release_images(window, cx));
}
/// The page is back in front of « Paramètres ».