mirror of
https://github.com/storytold/lightcraft.git
synced 2026-11-28 09:19:25 +01:00
PERF-CATALOG: a value with grid references resolves once per load (#2812)
Resolving each holder on its own let a long history push the photo's own settings out of the thread's short list of recent values, so a masked photo with five or more history steps held its settings twice after a load. A value read with references is now resolved once per repair and every holder that held it takes the same result. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
418072dee6
commit
2b509136db
2 files changed
+53
-7
No files matched your search
@@ -377,6 +377,12 @@ impl std::fmt::Debug for PlansMemo {
|
||||
}
|
||||
}
|
||||
|
||||
/// What [`resolve_grids`] has made of each value with references so far: the value as read (held
|
||||
/// here so its address stays its own), what it resolved to and how many dangling references it
|
||||
/// held. Holders that shared a value before resolving share the result, whatever else is between
|
||||
/// them (a photo's settings and its last history step, with the earlier steps in between).
|
||||
type ResolvedGrids = std::collections::HashMap<*const DevelopSettings, (Arc<DevelopSettings>, Arc<DevelopSettings>, usize)>;
|
||||
|
||||
/// Resolves the shared-grid references of one settings holder (see [`Photo::settings_holders`])
|
||||
/// against a snapshot's grid table: each referenced mask takes its
|
||||
/// grid's data and the reference is cleared. A reference past the table (a damaged snapshot)
|
||||
@@ -384,26 +390,33 @@ impl std::fmt::Debug for PlansMemo {
|
||||
/// 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;
|
||||
/// holders stays shared: [`Arc::make_mut`] would copy it for each. A value with references is
|
||||
/// resolved once (`done`): every holder that held it takes the same result, which is handed to
|
||||
/// [`shared::settings`], so holders that are equal once resolved share one value too.
|
||||
fn resolve_grids(settings: &mut Arc<DevelopSettings>, grids: &[String], dangling: usize, done: &mut ResolvedGrids) -> usize {
|
||||
let mut referenced = false;
|
||||
settings.visit_seg_masks(&mut |m| referenced |= m.grid.is_some());
|
||||
if !referenced {
|
||||
return dangling;
|
||||
}
|
||||
if let Some((_, resolved, dangled)) = done.get(&Arc::as_ptr(settings)) {
|
||||
*settings = resolved.clone();
|
||||
return dangling + dangled;
|
||||
}
|
||||
let original = settings.clone();
|
||||
let mut dangled = 0usize;
|
||||
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)) {
|
||||
(true, Some(data)) => m.data = data.clone(),
|
||||
(true, None) => dangling += 1,
|
||||
(true, None) => dangled += 1,
|
||||
(false, _) => {}
|
||||
}
|
||||
}
|
||||
});
|
||||
*settings = shared::settings(settings.clone());
|
||||
dangling
|
||||
done.insert(Arc::as_ptr(&original), (original, settings.clone(), dangled));
|
||||
dangling + dangled
|
||||
}
|
||||
|
||||
/// A short memory of what the latest ops changed, so that what is derived from the catalog (the grid's list) can
|
||||
@@ -1244,10 +1257,11 @@ impl Catalog {
|
||||
// an empty table, which is always dangling, so the table being empty does not skip it.
|
||||
let grids = std::mem::take(&mut self.grids);
|
||||
let mut dangling = 0usize;
|
||||
let mut done = ResolvedGrids::new();
|
||||
for p in self.photos.values_mut() {
|
||||
let photo = Arc::make_mut(p);
|
||||
for settings in photo.settings_holders_mut() {
|
||||
dangling = resolve_grids(settings, &grids, dangling);
|
||||
dangling = resolve_grids(settings, &grids, dangling, &mut done);
|
||||
}
|
||||
}
|
||||
if dangling > 0 {
|
||||
|
||||
@@ -436,3 +436,35 @@ fn resolved_grid_references_share_equal_settings() {
|
||||
assert!(p.versions.iter().all(|v| Arc::ptr_eq(&v.settings, first)), "photo {n}'s versions");
|
||||
}
|
||||
}
|
||||
|
||||
/// A masked photo whose history is longer than the few values sharing remembers still holds its
|
||||
/// settings and its last step once: the references of equal holders resolve to one value, not to
|
||||
/// a copy per holder that is shared only when it happens to be remembered.
|
||||
#[test]
|
||||
fn a_masked_photos_settings_share_with_its_last_step_after_a_long_history() {
|
||||
let seg = SegMask::from_logits(32, &vec![1.0f32; 32 * 32]);
|
||||
let id = PhotoId(1);
|
||||
let mut live = Catalog::new();
|
||||
let mut ops = vec![Op::AddPhoto { photo: Box::new(Photo::new(id, Source::Demo { scene: 0 }, "1.jpg", "JPEG", 60, 40, "2026-01-01")) }];
|
||||
let mut last = Arc::new(DevelopSettings::default());
|
||||
for i in 1..=10u32 {
|
||||
last = Arc::new(settings_with_seg(seg.clone(), f64::from(i) * 0.1));
|
||||
ops.push(Op::PushHistory { id, step: HistoryStep { label: "Exposure".into(), settings: last.clone() } });
|
||||
}
|
||||
ops.push(Op::SetDevelop { id, settings: last, label: "Exposure".into(), edited: Some("t".into()) });
|
||||
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 (_, loaded, report) = open(&m);
|
||||
assert_eq!(report.repaired, Vec::<String>::new(), "{:?}", report.repaired);
|
||||
assert_eq!(loaded.to_snapshot(), live.to_snapshot());
|
||||
let p = loaded.photo(id).unwrap();
|
||||
assert_eq!(p.history.len(), 10);
|
||||
let last = p.history.last().unwrap();
|
||||
assert!(Arc::ptr_eq(&p.develop, &last.settings), "the photo's settings and its last step are one value");
|
||||
}
|
||||
Reference in new issue
Block a user