PERF-CATALOG: loading a library keeps equal settings shared (#2812)

Catalog::repair() (also run when a journal opens) resolves segmentation-grid
references on every settings holder of every photo. Since #2203 that began with
Arc::make_mut on each one, which copies a shared value, so every load unshared
all settings. Holders with no grid reference are now left alone, and settings
that were resolved go through shared::settings again, so equal ones share a
value once more.

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
Nathaniel LedfordandClaude committed 2026-11-19 08:49:55 -05:00
1 parent 384e998db1
commit 418072dee6
3 files changed
+58

No files matched your search

+10
View File
@@ -382,8 +382,17 @@ impl std::fmt::Debug for PlansMemo {
/// grid's data and the reference is cleared. A reference past the table (a damaged snapshot)
/// leaves the mask empty — it renders nothing, like any other damaged grid. Data present on the
/// mask itself wins over a stray reference. Returns the dangling count added to `dangling`.
///
/// Settings that refer to no grid (nearly all of them) are left alone, so a value shared by many
/// holders stays shared: [`Arc::make_mut`] would copy it for each. Settings that were changed are
/// handed to [`shared::settings`] again, so holders that are equal once resolved share one value.
fn resolve_grids(settings: &mut Arc<DevelopSettings>, grids: &[String], dangling: usize) -> usize {
let mut dangling = dangling;
let mut referenced = false;
settings.visit_seg_masks(&mut |m| referenced |= m.grid.is_some());
if !referenced {
return dangling;
}
Arc::make_mut(settings).visit_seg_masks_mut(&mut |m| {
if let Some(i) = m.grid.take() {
match (m.data.is_empty(), grids.get(i as usize)) {
@@ -393,6 +402,7 @@ fn resolve_grids(settings: &mut Arc<DevelopSettings>, grids: &[String], dangling
}
}
});
*settings = shared::settings(settings.clone());
dangling
}
+2
View File
@@ -6,6 +6,8 @@
//! raw with the same import look. Here every
//! settings value that enters a catalog, read from a saved library or given by a change, is
//! replaced by the one already held when an equal one is.
//! Settings whose masks refer to a snapshot's grid table are shared again once the references are
//! resolved (`resolve_grids` in `lib.rs`); settings without a reference are left as they were read.
//!
//! Sharing never lets one photo's edit reach another: settings behind an `Arc` are not changed
//! in place (the catalog replaces them; `Arc::make_mut` copies a value that is shared).
+46
View File
@@ -390,3 +390,49 @@ fn omitted_shared_grid_table_clears_dangling_references() {
let MaskShape::Object { seg: Some(seg), .. } = &p.history[0].settings.masks[0].components[0].shape else { panic!("an Object mask") };
assert_eq!(seg.data, blob, "the history step's inline grid is untouched");
}
/// Equal settings that refer to the same grid in a snapshot's table are held once after the
/// snapshot is read: resolving the references gives every holder its grid's data, and the
/// holders that are equal again share one value (a library with many masked photos is not
/// held once per photo and step). What is written back is unchanged.
#[test]
fn resolved_grid_references_share_equal_settings() {
let seg = SegMask::from_logits(32, &vec![1.0f32; 32 * 32]);
let settings = Arc::new(settings_with_seg(seg, 0.5));
let mut live = Catalog::new();
let mut ops = Vec::new();
for n in 1..=2u64 {
let id = PhotoId(n);
let name = format!("{n}.jpg");
ops.push(Op::AddPhoto { photo: Box::new(Photo::new(id, Source::Demo { scene: 0 }, &name, "JPEG", 60, 40, "2026-01-01")) });
ops.push(Op::SetDevelop { id, settings: settings.clone(), label: "Exposure".into(), edited: Some("t".into()) });
ops.push(Op::PushHistory { id, step: HistoryStep { label: "Exposure".into(), settings: settings.clone() } });
ops.push(Op::SetVersions {
id,
versions: vec![Version { name: "v".into(), created: "2026-01-01".into(), settings: settings.clone(), auto: false }],
});
}
for op in &ops {
live.apply(op.clone()).unwrap();
}
let m = MemStore::new();
let (mut j, _, _) = open(&m);
j.append(&ops).unwrap();
j.snapshot(&live).unwrap();
drop(j);
let snap_bytes = m.get(SNAPSHOT).unwrap();
let v: serde_json::Value = serde_json::from_slice(&snap_bytes).unwrap();
assert_eq!(v["catalog"]["grids"].as_array().unwrap().len(), 1, "the snapshot refers to its one grid");
assert!(std::str::from_utf8(&snap_bytes).unwrap().contains("\"grid\":0"), "and its masks hold references");
let (_, loaded, report) = open(&m);
assert_eq!(report.repaired, Vec::<String>::new(), "{:?}", report.repaired);
assert_eq!(loaded.to_snapshot(), live.to_snapshot());
let first = &loaded.photo(PhotoId(1)).unwrap().develop;
for n in 1..=2 {
let p = loaded.photo(PhotoId(n)).unwrap();
assert!(Arc::ptr_eq(&p.develop, first), "photo {n}'s settings");
assert!(p.history.iter().all(|h| Arc::ptr_eq(&h.settings, first)), "photo {n}'s history");
assert!(p.versions.iter().all(|v| Arc::ptr_eq(&v.settings, first)), "photo {n}'s versions");
}
}