Skip to content

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

Perf: cache the left panel's counts / By Date tree and the filter bar's value lists

Brandon Thomasc24ed6e94a46
diff --git a/crates/ui-egui/src/lib.rs b/crates/ui-egui/src/lib.rs
index 35bfba5ac..5f421e845 100644
--- a/crates/ui-egui/src/lib.rs
+++ b/crates/ui-egui/src/lib.rs
@@ -631,6 +631,25 @@ pub struct Caches {
keyword_tree: Option<(u64, std::sync::Arc<Vec<lightcraft_catalog::KeywordNode>>)>,
date_runs: Option<(u64, std::sync::Arc<Vec<lightcraft_catalog::DateRun>>)>,
suggestions: Option<(u64, std::sync::Arc<Vec<String>>)>,
+ counts: Option<(u64, LibraryCounts)>,
+ date_groups: Option<(u64, std::sync::Arc<Vec<lightcraft_catalog::DateGroup>>)>,
+ filter_values: Option<(u64, std::sync::Arc<FilterValues>)>,
+}
+
+/// The left panel's counts.
+#[derive(Clone, Copy, Debug, Default)]
+pub struct LibraryCounts {
+ pub total: usize,
+ pub picks: usize,
+ pub deleted: usize,
+}
+
+/// The values the filter bar's pickers offer.
+#[derive(Clone, Debug, Default)]
+pub struct FilterValues {
+ pub cameras: Vec<String>,
+ pub lenses: Vec<String>,
+ pub keywords: Vec<String>,
}
fn key_of(parts: impl std::hash::Hash) -> u64 {
@@ -650,6 +669,60 @@ impl Caches {
}
}
}
+ /// All Photos / Picks / Recently Deleted counts.
+ pub fn counts(&mut self, cat: &lightcraft_catalog::Catalog) -> LibraryCounts {
+ if let Some((r, c)) = self.counts
+ && r == cat.revision
+ {
+ return c;
+ }
+ let mut c = LibraryCounts::default();
+ for p in cat.photos() {
+ if p.in_library() {
+ c.total += 1;
+ if p.flag == lightcraft_catalog::Flag::Pick {
+ c.picks += 1;
+ }
+ } else if p.deleted && !p.local {
+ c.deleted += 1;
+ }
+ }
+ self.counts = Some((cat.revision, c));
+ c
+ }
+ /// The By Date tree.
+ pub fn date_groups(&mut self, cat: &lightcraft_catalog::Catalog) -> std::sync::Arc<Vec<lightcraft_catalog::DateGroup>> {
+ match &self.date_groups {
+ Some((r, g)) if *r == cat.revision => g.clone(),
+ _ => {
+ let g = std::sync::Arc::new(cat.date_groups());
+ self.date_groups = Some((cat.revision, g.clone()));
+ g
+ }
+ }
+ }
+ /// Cameras, lenses and keywords in the library (filter bar pickers).
+ pub fn filter_values(&mut self, cat: &lightcraft_catalog::Catalog) -> std::sync::Arc<FilterValues> {
+ match &self.filter_values {
+ Some((r, v)) if *r == cat.revision => v.clone(),
+ _ => {
+ let distinct = |mut v: Vec<String>| {
+ v.retain(|s| !s.trim().is_empty());
+ v.sort_by_key(|s| s.to_lowercase());
+ v.dedup();
+ v
+ };
+ let lib = || cat.photos().filter(|p| p.in_library());
+ let v = std::sync::Arc::new(FilterValues {
+ cameras: distinct(lib().map(|p| p.meta.camera.clone()).collect()),
+ lenses: distinct(lib().map(|p| p.meta.lens.clone()).collect()),
+ keywords: cat.keywords().into_iter().map(|(k, _)| k).collect(),
+ });
+ self.filter_values = Some((cat.revision, v.clone()));
+ v
+ }
+ }
+ }
/// Date headers for `ids` in the grid.
pub fn date_runs(
&mut self,
diff --git a/crates/ui-egui/src/panels/filterbar.rs b/crates/ui-egui/src/panels/filterbar.rs
index 378792cf3..2bef1dd95 100644
--- a/crates/ui-egui/src/panels/filterbar.rs
+++ b/crates/ui-egui/src/panels/filterbar.rs
@@ -84,13 +84,6 @@ fn picker(app: &mut LightcraftApp, ui: &mut egui::Ui, id: &str, current: &str, a
}
}
-fn distinct(mut v: Vec<String>) -> Vec<String> {
- v.retain(|s| !s.trim().is_empty());
- v.sort_by_key(|s| s.to_lowercase());
- v.dedup();
- v
-}
-
pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
// one row when there is room, else the metadata pickers and actions go to a second row
@@ -250,10 +243,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
// metadata pickers
let ui: &mut egui::Ui = if two_rows { &mut row2 } else { &mut row1 };
- let photos: Vec<_> = app.session.catalog.photos().filter(|p| p.in_library()).cloned().collect();
- let cameras = distinct(photos.iter().map(|p| p.meta.camera.clone()).collect());
- let lenses = distinct(photos.iter().map(|p| p.meta.lens.clone()).collect());
- let keywords: Vec<String> = app.session.catalog.keywords().into_iter().map(|(k, _)| k).collect();
+ let values = app.caches.filter_values(&app.session.catalog);
+ let (cameras, lenses, keywords) = (values.cameras.clone(), values.lenses.clone(), values.keywords.clone());
for (id, key, all_label, current, values) in [
("camera", "camera", "Camera", f.camera.clone(), cameras),
("lens", "lens", "Lens", f.lens.clone(), lenses),
diff --git a/crates/ui-egui/src/panels/left.rs b/crates/ui-egui/src/panels/left.rs
index 730300777..c7ffc5af6 100644
--- a/crates/ui-egui/src/panels/left.rs
+++ b/crates/ui-egui/src/panels/left.rs
@@ -64,10 +64,8 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
ui.spacing_mut().item_spacing.y = 0.0;
let (hr, _) = ui.allocate_exact_size(vec2(ui.available_width(), 44.0), Sense::hover());
ui.painter().text(pos2(hr.left() + 18.0, hr.center().y), Align2::LEFT_CENTER, "My Photos", t.semibold(15.0), t.text);
- let stats: Vec<_> = app.session.catalog.photos().filter(|p| p.in_library()).map(|p| p.flag).collect();
- let total = stats.len();
- let picks = stats.iter().filter(|f| **f == lightcraft_catalog::Flag::Pick).count();
- let deleted = app.session.catalog.photos().filter(|p| p.deleted && !p.local).count();
+ let counts = app.caches.counts(&app.session.catalog);
+ let (total, picks, deleted) = (counts.total, counts.picks, counts.deleted);
egui::ScrollArea::vertical().id_salt("left-scroll").auto_shrink([false, false]).show(ui, |ui| {
let src = app.session.source;
for (id, icon, label, count, s) in [
@@ -121,7 +119,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
// By date
let (dr, _) = ui.allocate_exact_size(vec2(ui.available_width(), 34.0), Sense::hover());
ui.painter().text(pos2(dr.left() + 18.0, dr.center().y), Align2::LEFT_CENTER, "By Date", t.semibold(13.5), t.text_label);
- for g in app.session.catalog.date_groups() {
+ for g in app.caches.date_groups(&app.session.catalog).iter() {
// year → month → day; a click filters by that prefix, the triangle opens a level
if date_row(app, ui, &g.year, &g.year, g.count, 0.0) {
for (m, n) in &g.months {