Merge pull request #3018 from emmertex/upstream-pr/box-embedded-lens

perf(catalog): box Photo::embedded_lens (~240 MB less per 1M photos)
This commit is contained in:
FredApps authored and GitHub committed 2026-11-26 14:41:28 +01:00
commit 47759acc22
24 files changed
+48 -33

No files matched your search

+1 -1
View File
@@ -1153,7 +1153,7 @@ impl Catalog {
}
Op::SetEmbeddedLens { id, lens } => {
let p = self.photo_mut(id)?;
Op::SetEmbeddedLens { id, lens: std::mem::replace(&mut p.embedded_lens, lens.map(|l| *l)).map(Box::new) }
Op::SetEmbeddedLens { id, lens: std::mem::replace(&mut p.embedded_lens, lens) }
}
Op::SetAsShotWb { id, wb } => {
if wb.is_some_and(|(t, tint)| !(t.is_finite() && tint.is_finite())) {
+1 -1
View File
@@ -310,7 +310,7 @@ pub struct Photo {
/// Lens corrections embedded in the file (DNG `WarpRectilinear` / `FixVignetteRadial`), applied when
/// "Enable Profile Corrections" is on.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub embedded_lens: Option<lightcraft_develop::EmbeddedLens>,
pub embedded_lens: Option<Box<lightcraft_develop::EmbeddedLens>>,
/// A virtual copy: the photo it was copied from (it shares that photo's file but has its own
/// settings, metadata and history).
#[serde(default, skip_serializing_if = "Option::is_none")]
+16 -1
View File
@@ -661,7 +661,7 @@ fn set_embedded_lens_is_undoable_and_journaled() {
let op = Op::SetEmbeddedLens { id: a, lens: Some(Box::new(lens)) };
let op: Op = serde_json::from_str(&serde_json::to_string(&op).unwrap()).unwrap();
let inv = c.apply(op).unwrap();
assert_eq!(c.photo(a).unwrap().embedded_lens, Some(lens));
assert_eq!(c.photo(a).unwrap().embedded_lens, Some(Box::new(lens)));
let inv: Op = serde_json::from_str(&serde_json::to_string(&inv).unwrap()).unwrap();
c.apply(inv).unwrap();
assert_eq!(c.photo(a).unwrap().embedded_lens, None);
@@ -1187,3 +1187,18 @@ fn a_batch_that_cannot_be_rolled_back_whole_makes_earlier_changes_unknown() {
assert_eq!(c.touched_since(start), None, "so nothing is said of what changed");
assert_eq!(c.touched_since(c.revision), Some(vec![]), "from after it, it is known again");
}
/// `Photo::embedded_lens` is boxed to keep photo records small; the box doesn't change the serialized form:
/// a photo written with the lens reads back with it, and its JSON is the bare lens, as before the box.
#[test]
fn boxed_embedded_lens_serializes_like_the_bare_lens() {
let lens = lightcraft_develop::EmbeddedLens { warp: Some(Default::default()), vignette: None, stages: Default::default(), ..Default::default() };
let mut p = Photo::new(PhotoId(1), Source::File { path: "/x.dng".into() }, "x.dng", "DNG", 4000, 3000, "2026-10-06T00:00:00");
p.embedded_lens = Some(Box::new(lens));
let json = serde_json::to_value(&p).unwrap();
assert_eq!(json["embedded_lens"], serde_json::to_value(lens).unwrap());
let back: Photo = serde_json::from_value(json).unwrap();
assert_eq!(back.embedded_lens.as_deref(), Some(&lens));
p.embedded_lens = None;
assert!(serde_json::to_value(&p).unwrap().get("embedded_lens").is_none(), "no lens, no field");
}
+3 -3
View File
@@ -294,7 +294,7 @@ fn legacy_shutter_text(t: f64) -> Option<String> {
/// what import gave it, the lens correction is turned on as import would; an edited photo keeps
/// its settings.
pub(crate) fn lens_ops(id: lightcraft_catalog::PhotoId, ph: &lightcraft_catalog::Photo, info: &crate::media::ProbeInfo) -> Vec<Op> {
if info.embedded_lens == ph.embedded_lens {
if info.embedded_lens.as_ref() == ph.embedded_lens.as_deref() {
return Vec::new();
}
let mut ops = vec![Op::SetEmbeddedLens { id, lens: info.embedded_lens.map(Box::new) }];
@@ -601,7 +601,7 @@ mod tests {
c.apply(op).unwrap();
}
let after = c.photo(lightcraft_catalog::PhotoId(1)).unwrap();
assert_eq!(after.embedded_lens, Some(lens()));
assert_eq!(after.embedded_lens, Some(Box::new(lens())));
assert!(!after.is_edited());
assert_eq!(*after.develop, after.camera_defaults());
assert!(lens_ops(after.id, after, &info).is_empty(), "nothing more to do the second time");
@@ -622,7 +622,7 @@ mod tests {
#[test]
fn reload_leaves_a_photo_whose_user_switched_the_correction_off_alone() {
let mut ph = old_rw2();
ph.embedded_lens = Some(lens());
ph.embedded_lens = Some(Box::new(lens()));
let mut d = ph.camera_defaults();
d.optics.lens_profile = false;
ph.develop = std::sync::Arc::new(d);
+2 -2
View File
@@ -33,7 +33,7 @@ pub struct Missing {
pub fn missing(cat: &Catalog, db: &lightcraft_lensdb::LensDb, ids: Option<&[PhotoId]>, again: bool) -> Vec<Missing> {
// a photo and its virtual copies list the photo once, in library order
let mut seen = std::collections::HashSet::new();
let from_database = |p: &Photo| p.embedded_lens.is_some_and(|l| l.source == LensSource::Database);
let from_database = |p: &Photo| p.embedded_lens.as_deref().is_some_and(|l| l.source == LensSource::Database);
cat.photos()
.filter(|p| ids.is_none_or(|ids| ids.contains(&p.id) || p.copy_of.is_some_and(|c| ids.contains(&c))))
.map(|p| p.copy_of.and_then(|c| cat.photo(c)).map_or(&**p, |orig| &**orig))
@@ -82,7 +82,7 @@ pub fn apply(s: &mut Session, results: Vec<(Missing, std::result::Result<ProbeIn
for id in family {
let Some(p) = s.catalog.photo(id) else { continue };
// nothing found for a photo without a database profile: a file's own correction is Reload's business
let from_database = p.embedded_lens.is_some_and(|l| l.source == LensSource::Database);
let from_database = p.embedded_lens.as_deref().is_some_and(|l| l.source == LensSource::Database);
if info.embedded_lens.is_some() || from_database {
let mine = crate::cmd::convert::lens_ops(id, p, &info);
if !mine.is_empty() {
+1 -1
View File
@@ -2104,7 +2104,7 @@ fn rw2_and_dng_defaults_unchanged() {
let name = format!("x.{format}");
let mut p = Photo::new(PhotoId(1), Source::File { path: format!("/{name}") }, &name, format, 64, 48, "");
p.kind = MediaKind::Raw;
p.embedded_lens = embedded;
p.embedded_lens = embedded.map(Box::new);
crate::import::apply_import_defaults(&s, &mut p);
assert_eq!(p.develop.optics.lens_profile, embedded.is_some(), "{format} {embedded:?}");
}
+1 -1
View File
@@ -275,7 +275,7 @@ pub fn commit_prepared(s: &mut Session, opts: &ImportOptions, now: &str, prepare
p.file_size = info.file_size;
p.as_shot_wb = info.as_shot_wb;
p.content_hash = info.content_hash;
p.embedded_lens = info.embedded_lens;
p.embedded_lens = info.embedded_lens.map(Box::new);
p.set_preview_only(info.preview_only.clone());
if let Some(sc) =
start_from_file(s, &mut p, FileFacts { meta: info.meta, captured: info.captured, orientation: info.orientation, sidecar }, &now)
+1 -1
View File
@@ -518,7 +518,7 @@ impl Session {
p.kind = c.kind;
if let Some(info) = self.import_probes.get(&c.path) {
p.as_shot_wb = info.as_shot_wb;
p.embedded_lens = info.embedded_lens;
p.embedded_lens = info.embedded_lens.map(Box::new);
p.preview_only = info.preview_only.clone();
}
p.develop = std::sync::Arc::new(p.import_defaults());
+2 -2
View File
@@ -1013,7 +1013,7 @@ impl Session {
photo.height.max(1) as usize,
&d,
true,
photo.embedded_lens.as_ref(),
photo.embedded_lens.as_deref(),
);
d.crop.geometry.rect = frame.crop.rect;
d
@@ -1138,7 +1138,7 @@ impl Session {
p.height.max(1) as usize,
&d.effective(),
false,
p.embedded_lens.as_ref(),
p.embedded_lens.as_deref(),
))
}
+1 -1
View File
@@ -1272,7 +1272,7 @@ pub(crate) fn apply_prepared(s: &mut crate::Session, mut data: CatalogImport, co
p.duration = duplicate.duration;
p.as_shot_wb = duplicate.as_shot_wb;
p.content_hash = duplicate.content_hash.clone();
p.embedded_lens = duplicate.embedded_lens;
p.embedded_lens = duplicate.embedded_lens.clone();
p.preview_only = duplicate.preview_only.clone();
p.preview_checked = duplicate.preview_checked.clone();
let file = file.cloned().unwrap_or_else(|| {
+1 -1
View File
@@ -265,7 +265,7 @@ fn a_content_duplicate_raw_starts_from_its_camera_defaults() {
p.format = "DNG".into();
p.kind = lightcraft_catalog::MediaKind::Raw;
p.as_shot_wb = Some((5200.0, 4.0));
p.embedded_lens = Some(lightcraft_develop::EmbeddedLens::default());
p.embedded_lens = Some(Box::default());
});
let mut data = sample();
data.photos.retain(|p| number(&p.image, "masterImage") == 0);
+1 -1
View File
@@ -209,7 +209,7 @@ fn header_info(p: &Photo) -> SourceInfo {
raw: true,
as_shot_temp: temp,
as_shot_tint: tint,
lens: p.embedded_lens,
lens: p.embedded_lens.as_deref().copied(),
relative_wb: relative,
// (a fitted-look raw edited in Kelvin: its camera's matrices give the scale, see `files::WbScaleOf`)
legacy_relative_wb: p.fitted_look() && !relative,
+1 -1
View File
@@ -367,7 +367,7 @@ impl crate::Session {
p.height.max(1) as usize,
&job.settings,
apply_crop,
p.embedded_lens.as_ref(),
p.embedded_lens.as_deref(),
);
let ppl = frame.px_per_long(full_w);
lightcraft_pipeline::spots::window_for_reads_checked(&job.settings, &frame, full_w, full_h, ppl, window.clamped(full_w, full_h))?;
+1 -1
View File
@@ -87,7 +87,7 @@ impl crate::Session {
p.height.max(1) as usize,
&p.develop,
true,
p.embedded_lens.as_ref(),
p.embedded_lens.as_deref(),
);
let a = frame.aspect();
Some(if a.is_finite() && a > 0.0 { a } else { 1.5 })
+1 -1
View File
@@ -17,7 +17,7 @@ fn fits(s: &Session) -> bool {
let p = s.catalog.photo(s.active().unwrap()).unwrap();
let mut d = (*p.develop).clone();
d.geometry.constrain_crop = true;
let f = Frame::with_lens(p.width as usize, p.height as usize, &d, true, p.embedded_lens.as_ref());
let f = Frame::with_lens(p.width as usize, p.height as usize, &d, true, p.embedded_lens.as_deref());
let (a, b) = (f.crop.rect, p.develop.crop.geometry.rect);
[a.x0 - b.x0, a.y0 - b.y0, a.x1 - b.x1, a.y1 - b.y1].iter().all(|d| d.abs() < 1e-9)
}
+2 -2
View File
@@ -19,7 +19,7 @@ fn lens(k: f64) -> EmbeddedLens {
}
fn lens_of(s: &Session, id: PhotoId) -> Option<EmbeddedLens> {
s.catalog.photo(id).and_then(|p| p.embedded_lens)
s.catalog.photo(id).and_then(|p| p.embedded_lens.as_deref().copied())
}
/// Two demo photos "taken with different lenses".
@@ -157,7 +157,7 @@ fn photos_get_their_profile_when_the_database_arrives() {
}
fn with_lens(mut p: Photo, l: Option<EmbeddedLens>, on: bool) -> Photo {
p.embedded_lens = l;
p.embedded_lens = l.map(Box::new);
let mut d = (*p.develop).clone();
d.optics.lens_profile = on;
p.develop = Arc::new(d);
+2 -2
View File
@@ -100,8 +100,8 @@ fn hdr_merge_keeps_the_reference_frames_lens_corrections() {
let reference = s.catalog.photo(lightcraft_catalog::PhotoId(ids[1])).unwrap().clone();
let expect = if orientation == Orientation::Normal { (452, 304) } else { (304, 452) };
assert_eq!(((merged.width, merged.height), (reference.width, reference.height)), (expect, expect));
let a = reference.embedded_lens.expect("the source's correction");
let b = merged.embedded_lens.expect("the merged DNG carries the reference frame's correction");
let a = reference.embedded_lens.as_deref().copied().expect("the source's correction");
let b = merged.embedded_lens.as_deref().copied().expect("the merged DNG carries the reference frame's correction");
let (wa, wb) = (a.warp.unwrap(), b.warp.unwrap());
let (va, vb) = (a.vignette.unwrap(), b.vignette.unwrap());
let (w, h) = (expect.0 as f64, expect.1 as f64);
@@ -207,7 +207,7 @@ fn real() {
p.kind = probe.kind;
p.meta = probe.meta;
p.as_shot_wb = probe.as_shot_wb;
p.embedded_lens = probe.embedded_lens;
p.embedded_lens = probe.embedded_lens.map(Box::new);
p.content_hash = probe.content_hash;
p.preview_only = probe.preview_only;
lightcraft_engine::import::apply_import_defaults(&session, &mut p);
+3 -3
View File
@@ -185,7 +185,7 @@ fn database_name(camera: &str, lens: &str) -> Option<String> {
/// none.
pub fn readout(app: &LightcraftApp, p: &lightcraft_catalog::Photo) -> String {
use lightcraft_develop::LensSource;
match p.embedded_lens.map(|l| l.source) {
match p.embedded_lens.as_deref().map(|l| l.source) {
Some(LensSource::File) => crate::i18n::tr("Profile: from the file (the camera's own correction)").to_string(),
Some(LensSource::Database) => {
let name = database_name(&p.meta.camera, &p.meta.lens).unwrap_or_else(|| p.meta.lens.trim().to_string());
@@ -219,10 +219,10 @@ mod tests {
assert_eq!(super::readout(&app, &p), "No lens profile found for EF35mm f/1.4L II USM.");
p.meta.lens.clear();
assert_eq!(super::readout(&app, &p), "This file names no lens.");
p.embedded_lens = Some(EmbeddedLens::default());
p.embedded_lens = Some(Box::default());
assert_eq!(super::readout(&app, &p), "Profile: from the file (the camera's own correction)");
p.meta.lens = "EF70-200mm f/2.8L IS II USM".into();
p.embedded_lens = Some(EmbeddedLens { source: LensSource::Database, ..Default::default() });
p.embedded_lens = Some(Box::new(EmbeddedLens { source: LensSource::Database, ..Default::default() }));
// the database's name for it, or the file's own without a database (LIGHTCRAFT_LENSFUN_DIR may be set)
let r = super::readout(&app, &p);
assert!(r.starts_with("Profile: ") && r.contains("70-200mm f/2.8L IS II USM") && r.ends_with(" (lens database)"), "{r}");
+1 -1
View File
@@ -222,7 +222,7 @@ fn frame_of(photo: &lightcraft_catalog::Photo) -> lightcraft_pipeline::geometry:
photo.height.max(1) as usize,
&photo.develop,
true,
photo.embedded_lens.as_ref(),
photo.embedded_lens.as_deref(),
)
}
+2 -2
View File
@@ -164,7 +164,7 @@ pub fn show(app: &mut LightcraftApp, ui: &mut egui::Ui) {
// the full-screen preview shows the photo only: no tool overlays
let right = if fullscreen { RightPanel::None } else { app.ui.right };
let crop_tool = right == RightPanel::Crop;
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &d, !crop_tool, photo.embedded_lens.as_ref());
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &d, !crop_tool, photo.embedded_lens.as_deref());
let aspect = frame.aspect() as f32;
let ppp = ui.ctx().pixels_per_point();
let area = canvas.shrink(loupe_margin(fullscreen, crop_tool));
@@ -538,7 +538,7 @@ fn neighbour_job(
with_crop: bool,
) -> Option<lightcraft_engine::media::RenderJob> {
let np = app.session.catalog.photo(nid)?;
let nf = Frame::with_lens(np.width.max(1) as usize, np.height.max(1) as usize, &np.develop, with_crop, np.embedded_lens.as_ref());
let nf = Frame::with_lens(np.width.max(1) as usize, np.height.max(1) as usize, &np.develop, with_crop, np.embedded_lens.as_deref());
let na = nf.aspect() as f32;
let nr = fit_rect(main_area, na, app.ui.zoom, output_px(&nf), ppp, app.ui.pan);
let nw = app.ui.settings.prefetch_edge(nr.width().max(nr.height()) * ppp, output_px(&nf).into_iter().max().unwrap_or(1), texture_side);
+1 -1
View File
@@ -253,7 +253,7 @@ pub(crate) fn output_px(frame: &Frame) -> [usize; 2] {
pub(crate) fn focus_box(photo: &lightcraft_catalog::Photo) -> Option<lightcraft_geom::Rect> {
let f = photo.meta.focus.as_ref()?;
let d = &photo.develop;
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, d, true, photo.embedded_lens.as_ref());
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, d, true, photo.embedded_lens.as_deref());
let [w, h] = output_px(&frame);
let to_out = frame.out_to_norm(w, h).inverse()?;
// stored on the upright photo: the user's Rotate / Flip on top, then the crop
+1 -1
View File
@@ -213,7 +213,7 @@ pub fn tick(app: &mut LightcraftApp, ctx: &egui::Context, now: f64) {
/// [`SlideSettings::is_plain`]: lightcraft_engine::slide::SlideSettings::is_plain
fn slide_job(app: &mut LightcraftApp, id: PhotoId, area: Rect, ppp: f32, texture_side: usize) -> Option<lightcraft_engine::RenderJob> {
let photo = app.session.catalog.photo(id)?.clone();
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &photo.develop, true, photo.embedded_lens.as_ref());
let frame = Frame::with_lens(photo.width.max(1) as usize, photo.height.max(1) as usize, &photo.develop, true, photo.embedded_lens.as_deref());
let native = super::detail::output_px(&frame);
if !app.session.slides_plain() {
let (w, h) = slide_px(area, ppp, texture_side);
+1 -1
View File
@@ -306,7 +306,7 @@ fn compare_zoom_window_tracks_the_zoomed_pane() {
photo.height.max(1) as usize,
&photo.develop,
true,
photo.embedded_lens.as_ref(),
photo.embedded_lens.as_deref(),
);
let aspect = frame.aspect() as f32;
let native = crate::panels::detail::output_px(&frame);