Skip to content

LightCraft: An open-source non-destructive photo editor written by scratch in pure Rust

Web: prune the thumbnail cache to the configured budget (#245)

Mohammed Munzir87dd1c201aa1
diff --git a/apps/lightcraft-web/src/web.rs b/apps/lightcraft-web/src/web.rs
index 2efabebb5..93fd8642d 100644
--- a/apps/lightcraft-web/src/web.rs
+++ b/apps/lightcraft-web/src/web.rs
@@ -429,8 +429,17 @@ impl WebApp {
cores.saturating_sub(1).clamp(1, 4)
});
let cache = app.session.media.rendered.clone();
- let workers =
- (n > 0).then(|| Workers::start(n, backend.as_ref().map_or("memory", |b| b.kind()), backend.clone(), index, &cache, cc.egui_ctx.clone()));
+ let workers = (n > 0).then(|| {
+ Workers::start(
+ n,
+ backend.as_ref().map_or("memory", |b| b.kind()),
+ backend.clone(),
+ index,
+ &cache,
+ app.session.cache_bytes(),
+ cc.egui_ctx.clone(),
+ )
+ });
if let Some(w) = &workers {
app.renderer.set_offload(Box::new(w.clone()));
}
@@ -606,6 +615,11 @@ impl eframe::App for WebApp {
self.run_inbox();
self.import_dropped(ctx);
self.load_originals(ctx);
+ if let Some(w) = &self.workers {
+ // `library.preferences ▸ cacheMb` is read by the session; the worker index prunes on
+ // its own thread, so it gets the budget every frame and applies it on the next store.
+ w.set_budget(self.app.session.cache_bytes());
+ }
self.app.logic(ctx);
self.save();
if let Some(b) = self.bench.as_mut()
diff --git a/apps/lightcraft-web/src/workers.rs b/apps/lightcraft-web/src/workers.rs
index a90ed61e5..52a323289 100644
--- a/apps/lightcraft-web/src/workers.rs
+++ b/apps/lightcraft-web/src/workers.rs
@@ -18,7 +18,7 @@ use std::sync::Arc;
use js_sys::{Array, Object, Reflect, Uint8Array, Uint32Array};
use lightcraft_engine::catalog::PhotoId;
-use lightcraft_engine::media::{DISK_CACHE_BYTES, RenderJob, RenderResult, SourceLevel};
+use lightcraft_engine::media::{RenderJob, RenderResult, SourceLevel};
use lightcraft_engine::pipeline::Rendered;
use lightcraft_preview::{Hash128, PreviewCache};
use lightcraft_raster::{Histogram, Rgba8};
@@ -183,6 +183,9 @@ struct Inner {
/// (photo, level) → worker that last decoded it.
affinity: HashMap<(PhotoId, SourceLevel), usize>,
index: ThumbIndex,
+ /// The session's thumbnail cache budget in bytes. Kept here because the prune happens on a
+ /// worker message, where the session is out of reach; the app refreshes it every frame.
+ budget: u64,
backend: Option<Backend>,
ctx: egui::Context,
/// Jobs finished by workers / inline (for diagnostics).
@@ -214,7 +217,15 @@ impl Workers {
/// Start `n` workers (none: every job runs inline). `store` is the backend kind for workers.
/// `index` lists the stored thumbnails of `cache`, the session's rendered-thumbnail cache
/// now: watching it from the start, a clear before the first render request still counts.
- pub fn start(n: usize, store: &str, backend: Option<Backend>, index: ThumbIndex, cache: &Arc<PreviewCache>, ctx: egui::Context) -> Workers {
+ pub fn start(
+ n: usize,
+ store: &str,
+ backend: Option<Backend>,
+ index: ThumbIndex,
+ cache: &Arc<PreviewCache>,
+ budget: u64,
+ ctx: egui::Context,
+ ) -> Workers {
let w = Workers(Rc::new(RefCell::new(Inner {
preview_generation: CacheWatch::new(cache),
workers: Vec::new(),
@@ -222,6 +233,7 @@ impl Workers {
next_id: 0,
affinity: HashMap::new(),
index,
+ budget,
backend,
ctx,
remote_done: 0,
@@ -328,7 +340,10 @@ impl Workers {
}
if stored > 0 {
g.index.insert(&hex, stored);
- let gone = g.index.prune(DISK_CACHE_BYTES);
+ // The session's own budget (`library.preferences ▸ cacheMb`), not the native
+ // default: the browser accepted the setting, so it has to be the one enforced
+ // (#245).
+ let gone = g.index.prune(g.budget);
if !gone.is_empty()
&& let Some(be) = g.backend.clone()
{
@@ -386,6 +401,12 @@ impl Workers {
let ready = g.workers.iter().filter(|w| w.ready && !w.dead).count();
(alive, ready, g.remote_done, g.inline_done)
}
+
+ /// The budget the thumbnail index is pruned to. The app refreshes it from the session every
+ /// frame, so `library.preferences ▸ cacheMb` applies to the next stored thumbnail.
+ pub fn set_budget(&self, bytes: u64) {
+ self.0.borrow_mut().budget = bytes;
+ }
}
impl RenderOffload for Workers {