Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions apps/lightcraft/src/native_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ impl NativeMenu {
/// app can crash in winit's teardown, PR #444).
pub fn update(&mut self, app: &mut LightcraftApp, ctx: &egui::Context) -> bool {
lightcraft_ui_egui::i18n::set_language(app.ui.language);
app.session.begin_frame();
let mut quit = false;
while let Ok(key) = self.rx.try_recv() {
// Resolve the native id to its command, then let the shared policy decide. Native menu
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ pub fn has_selection(s: &Session) -> std::result::Result<(), String> {
/// can take (issue #684; Add to My Photos first).
pub fn has_library_selection(s: &Session) -> std::result::Result<(), String> {
has_selection(s)?;
if s.selected().iter().any(|id| s.catalog.photo(*id).is_none_or(|p| !p.local)) { Ok(()) } else { Err(LOCAL_ONLY.into()) }
if s.selection_facts().any_in_library(s) { Ok(()) } else { Err(LOCAL_ONLY.into()) }
}
/// Why an album command refused a Local-only selection.
pub const LOCAL_ONLY: &str = "only Local photos: albums hold library photos (Add to My Photos first)";
Expand Down
4 changes: 1 addition & 3 deletions crates/engine/src/cmd/develop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ fn already_current() -> String {
/// checked by the command instead (`explicit_targets`).
fn has_outdated_process(s: &Session) -> std::result::Result<(), String> {
has_selection(s)?;
let sel = &s.selection;
let targets = if sel.ids.is_empty() { sel.active.as_slice() } else { sel.ids.as_slice() };
if targets.iter().any(|id| outdated(s, *id)) { Ok(()) } else { Err(already_current()) }
if s.selection_facts().any_outdated_process(s) { Ok(()) } else { Err(already_current()) }
}

/// The photos a call names (`ids`, or `id`), each of which must be in the library; `None` when it
Expand Down
2 changes: 1 addition & 1 deletion crates/engine/src/cmd/organize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fn stacks_of(s: &Session, targets: &[PhotoId]) -> Vec<StackId> {

fn in_stack(s: &Session) -> std::result::Result<(), String> {
has_selection(s)?;
if stacks_of(s, &s.selected()).is_empty() { Err("no stacked photo selected".into()) } else { Ok(()) }
if s.selection_facts().any_stacked(s) { Ok(()) } else { Err("no stacked photo selected".into()) }
}

fn has_stacks(s: &Session) -> std::result::Result<(), String> {
Expand Down
10 changes: 10 additions & 0 deletions crates/engine/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub mod profile_refs;
pub mod proof;
pub mod rename;
pub mod segment;
mod selection_facts;
pub mod sidecar;
pub mod slide;
pub mod slide_overlay;
Expand All @@ -91,6 +92,7 @@ pub use fonts::{CRAFT_FONTS, CraftFont};
use lightcraft_catalog::{Catalog, Filter, Op, PhotoId, Sort};
use lightcraft_develop::DevelopSettings;
pub use media::{RenderJob, SourceLevel};
pub use selection_facts::{SelectionFacts, SelectionIndex, SelectionView};
use serde_json::Value;
pub use view::{Browse, FilterChip, LibrarySource, Selection, SelectionState, filter_chips};
pub use {lightcraft_catalog as catalog, lightcraft_develop as develop, lightcraft_gpu as gpu, lightcraft_pipeline as pipeline};
Expand Down Expand Up @@ -236,6 +238,8 @@ pub struct Session {
pub(crate) shelved_filter: Option<Filter>,
pub sort: Sort,
pub selection: Selection,
/// See [`Session::selection_facts`].
selection_cache: std::sync::Mutex<selection_facts::SelectionCache>,
/// The grid order for the current source/filter/sort (cached by catalog revision).
/// Shared, so frontends can hold it across frames without copying ([`Session::visible_shared`]).
visible: Arc<[PhotoId]>,
Expand Down Expand Up @@ -454,6 +458,7 @@ impl Session {
shelved_filter: None,
sort: Sort::default(),
selection: Selection::default(),
selection_cache: Default::default(),
visible: Vec::new().into(),
visible_key: None,
visible_clock_minute: None,
Expand Down Expand Up @@ -556,6 +561,7 @@ impl Session {

/// Run a command by id. THE entry point for every frontend.
pub fn execute(&mut self, id: &str, params: &Value) -> Result<Value> {
self.begin_frame();
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
let spec = find_command(id).ok_or_else(|| EngineError::UnknownCommand(id.to_string()))?;
// a command that validates the photos a call names isn't held back by the selection
if !(spec.explicit_targets && cmd::names_photos(params)) {
Expand All @@ -574,6 +580,7 @@ impl Session {
/// panic guard, auto versions, XMP sidecar auto-write and the durable save (a failed save is
/// `NotSaved`). Not journaled. The app's import task commits its batches this way.
pub fn execute_fn(&mut self, id: &str, f: impl FnOnce(&mut Session) -> Result<Value>) -> Result<Value> {
self.begin_frame();
self.run_command(id, None, f)
}

Expand All @@ -584,6 +591,9 @@ impl Session {
// last-resort guard: a panic in a command is that command's error, not a crash
let r = guard::catch(&format!("`{id}`"), || f(self)).unwrap_or_else(|e| Err(EngineError::Other(e)));
self.depth -= 1;
// the command may have read the selection's index and then changed its ids to others of the same
// length and ends: the caller must not be answered from what was cached before
self.begin_frame();
if self.depth == 0 && was_active.is_some() && self.active() != was_active {
self.previous_active = was_active;
if let Some(left) = was_active {
Expand Down
207 changes: 207 additions & 0 deletions crates/engine/src/selection_facts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
//! Facts about the selection that enablement checks and panels ask every frame.
//!
//! Walking a big selection photo by photo costs milliseconds per thousand photos, so a menu that
//! asks "is any selected photo in a stack?" for every command would stall a 300,000-photo
//! selection on every frame. The answers are worked out once for a given selection and catalog
//! revision, and only when asked, stopping at the first photo that settles them.

use std::collections::HashSet;
use std::sync::{Arc, OnceLock};

use lightcraft_catalog::PhotoId;

use crate::{Selection, SelectionState, Session};

/// Selections shorter than this are fingerprinted on every ask (microseconds); longer ones are
/// fingerprinted once per frame ([`Session::begin_frame`]) and between frames only when a command ran
/// or the list's length or ends changed.
const FINGERPRINT_EVERY_ASK: usize = 1024;

/// What a [`SelectionFacts`] was worked out for: the exact ids (their fingerprint), the active photo
/// (it stands in for an empty selection) and the catalog as it was.
#[derive(Clone, Copy, PartialEq, Eq)]
struct Key {
ids: Fp,
active: Option<PhotoId>,
revision: u64,
}

/// What names a list of ids: its length (so no two lengths share one, whatever the hash) and its hash.
type Fp = (usize, u64);

/// A fast, order-sensitive, non-cryptographic hash of the ids (Fx-style).
fn fingerprint(ids: &[PhotoId]) -> u64 {
ids.iter().fold(ids.len() as u64, |h, id| (h.rotate_left(5) ^ id.0).wrapping_mul(0x517c_c1b7_2722_0a95))
}

/// The last fingerprint of `Selection::ids`, and what it was for.
struct Memo {
len: usize,
first: Option<PhotoId>,
last: Option<PhotoId>,
hash: u64,
/// Not worked out again until the frame ends, a command runs or `len`, `first` or `last` differ.
trusted: bool,
}

/// What [`Session`] keeps about the selection between asks; the selection itself stays a plain
/// `ids` list that anything may change.
#[derive(Default)]
pub(crate) struct SelectionCache {
memo: Option<Memo>,
index: Option<Arc<SelectionIndex>>,
facts: Option<Arc<SelectionFacts>>,
}

impl SelectionCache {
fn fingerprint(&mut self, sel: &Selection) -> Fp {
let ids = sel.ids.as_slice();
let (first, last) = (ids.first().copied(), ids.last().copied());
if let Some(m) = &self.memo
&& m.trusted
&& (m.len, m.first, m.last) == (ids.len(), first, last)
{
return (m.len, m.hash);
}
let hash = fingerprint(ids);
self.memo = Some(Memo { len: ids.len(), first, last, hash, trusted: ids.len() >= FINGERPRINT_EVERY_ASK });
(ids.len(), hash)
}
}

/// The selected photos as a set, for a whole frame of asks (a grid draws hundreds of cells).
pub struct SelectionIndex {
ids: Fp,
members: OnceLock<HashSet<PhotoId>>,
}

/// The selection with its membership index: `contains` and `state_of` in constant time.
pub struct SelectionView<'a> {
sel: &'a Selection,
index: Arc<SelectionIndex>,
}

impl SelectionView<'_> {
pub fn contains(&self, id: PhotoId) -> bool {
self.index.members.get_or_init(|| self.sel.ids.iter().copied().collect()).contains(&id)
}

/// [`Selection::state_of`], without walking the ids.
pub fn state_of(&self, id: PhotoId) -> SelectionState {
if self.sel.active == Some(id) {
SelectionState::Active
} else if self.contains(id) {
SelectionState::Selected
} else {
SelectionState::NotSelected
}
}
}

/// Answers about the photos a command acts on when a call names none: the selection, else the
/// active photo ([`Session::selected_ids`]).
pub struct SelectionFacts {
key: Key,
in_library: OnceLock<bool>,
stacked: OnceLock<bool>,
outdated: OnceLock<bool>,
deleted: OnceLock<bool>,
}

impl SelectionFacts {
fn new(key: Key) -> Self {
SelectionFacts { key, in_library: OnceLock::new(), stacked: OnceLock::new(), outdated: OnceLock::new(), deleted: OnceLock::new() }
}

/// Some photo is in the library: not a browsed (Local) one. A photo the catalog doesn't have counts as in the library.
pub fn any_in_library(&self, s: &Session) -> bool {
*self.in_library.get_or_init(|| s.selected_ids().iter().any(|id| s.catalog.photo(*id).is_none_or(|p| !p.local)))
}

/// Some photo is in a stack.
pub fn any_stacked(&self, s: &Session) -> bool {
*self.stacked.get_or_init(|| {
if s.selection.ids.is_empty() {
s.selection.active.is_some_and(|id| s.catalog.stack_of(id).is_some())
} else if s.selection.ids.len() <= s.catalog.stacks().count() {
s.selection.ids.iter().any(|id| s.catalog.stack_of(*id).is_some())
} else {
// more selected photos than stacks: ask the selection's index about each stacked photo
let view = s.selection_view();
s.catalog.stacks().any(|st| st.photos.iter().any(|p| view.contains(*p)))
}
})
}

/// Some photo is on an older rendering process.
pub fn any_outdated_process(&self, s: &Session) -> bool {
*self.outdated.get_or_init(|| s.selected_ids().iter().any(|id| s.catalog.photo(*id).is_some_and(|p| p.develop.process.is_outdated())))
}

/// Some selected photo, or the active one, is in Recently Deleted.
pub fn any_deleted(&self, s: &Session) -> bool {
*self
.deleted
.get_or_init(|| s.selection.ids.iter().copied().chain(s.selection.active).any(|id| s.catalog.photo(id).is_some_and(|p| p.deleted)))
}
}

impl Session {
/// The photos commands act on when a call names none, without copying: the selection, else the active photo.
pub fn selected_ids(&self) -> &[PhotoId] {
if self.selection.ids.is_empty() { self.selection.active.as_slice() } else { &self.selection.ids }
}

/// How many photos [`Self::selected_ids`] has.
pub fn selected_count(&self) -> usize {
self.selected_ids().len()
}

fn cache(&self) -> std::sync::MutexGuard<'_, SelectionCache> {
self.selection_cache.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
}

/// Starts a frame: a long selection is fingerprinted again at the next ask, so changes made
/// between frames by anything (the UI sets `selection.ids` directly) are seen.
pub fn begin_frame(&self) {
if let Some(m) = &mut self.cache().memo {
m.trusted = false;
}
}

/// Names the photos [`Self::selected_ids`] has (the active photo only when it stands in for an empty selection):
/// another value after any change to them, so a cache keyed on it can't go stale.
pub fn selection_key(&self) -> (Fp, Option<PhotoId>) {
let ids = self.cache().fingerprint(&self.selection);
(ids, if self.selection.ids.is_empty() { self.selection.active } else { None })
}

/// The selection with a membership index, rebuilt only when the selected ids changed.
pub fn selection_view(&self) -> SelectionView<'_> {
let mut c = self.cache();
let ids = c.fingerprint(&self.selection);
let index = match &c.index {
Some(i) if i.ids == ids => i.clone(),
_ => {
let i = Arc::new(SelectionIndex { ids, members: OnceLock::new() });
c.index = Some(i.clone());
i
}
};
SelectionView { sel: &self.selection, index }
}

/// The facts about the selection, shared while neither it nor the catalog changes.
pub fn selection_facts(&self) -> Arc<SelectionFacts> {
let mut c = self.cache();
let key = Key { ids: c.fingerprint(&self.selection), active: self.selection.active, revision: self.catalog.revision };
match &c.facts {
Some(f) if f.key == key => f.clone(),
_ => {
let f = Arc::new(SelectionFacts::new(key));
c.facts = Some(f.clone());
f
}
}
}
}
1 change: 1 addition & 0 deletions crates/engine/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ mod print;
mod process;
mod recheck;
mod segment;
mod selection_facts;
mod settings_files;
mod slide;
mod slideshow_export;
Expand Down
Loading
Loading