Font menu: keep at most 256 sample textures (#339 follow-up)

Each sample can take ~370 KB of texture; 3000 of them could hold about a gigabyte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
(cherry picked from commit d2daeefb6ef442a472696e78498deeedc03ca823)
This commit is contained in:
Brandon Thomas 2026-10-07 16:58:33 -04:00
parent b33e964aec
commit 1a5b3f00ba
1 changed files with 5 additions and 1 deletions

View File

@ -418,6 +418,9 @@ pub(crate) fn menu_item_sample(ui: &Ui, item: egui::Rect, family: &str) {
type Key = (String, String, String, u32);
/// Samples kept before the cache starts over.
const MAX_SAMPLES: usize = 256;
#[derive(Default)]
struct Samples {
ready: HashMap<Key, Option<egui::TextureHandle>>,
@ -447,7 +450,8 @@ fn sample_texture(
if let Some(t) = s.ready.get(&key) {
return t.clone();
}
if s.ready.len() > 3000 {
// Up to ~370 KB of texture each (96 px high, 10:1): keep a few screens' worth.
if s.ready.len() > MAX_SAMPLES {
s.ready.clear();
}
#[cfg(not(target_arch = "wasm32"))]