Skip to content

PERF-979: a large library opens in seconds (edited photos are found in the background) - #1277

Merged
emmertex merged 2 commits into
emmertex:mainfrom
RAD-Films:fix/open-big-thumb-cache
Nov 18, 2026
Merged

emmertex merged 2 commits into
emmertex:mainfrom
RAD-Films:fix/open-big-thumb-cache

Conversation

@OneSlips

Copy link
Copy Markdown
Collaborator

Part of #979

Problem

Opening a large library takes about a minute. With 300,000 photos and a 16.5 GB thumbnail cache, library: opened in logs 65 to 70 s, and nearly all of it is the stage that #1160's log line calls thumbnail cache. For a photographer who keeps years of shoots in one library, that is a minute of waiting at every start.

The cache's files are not the cause. Nothing at open reads, lists or counts them (attaching the cache costs 0.2 ms with 300,000 files on disk), which is why clearing the cache left the start as slow as before.

The time goes to Session::keep_edited_previews_last, which ran inside that stage. To tell which photos are edited (their latest previews are the last a full cache lets go) it called looks_as_shot on every photo. That calls camera_defaults() once or twice per photo, and each call runs relative_wb(), which looks the camera up by name in the .dcp registry, the spectral table, the Kelvin presets and camdb, normalising strings as it goes. That is about 200 µs per photo, all on the opening thread.

Change

  • crates/engine library.rs: keep_edited_previews_last takes the photos (reference-counted, no copy) and works out the set on a thread of its own, holding a cancellable TaskGuard row Finding edited photos in the activity stack. It stops when the row is cancelled or the library is closed. A library of up to 2,000 photos (INLINE_EDITED_PHOTOS, under 10 ms) is still done inline, so small libraries show no row. The pass looks each camera up once (relative_wb_key, memoised) instead of once per photo: 300,000 photos take about 1 s in the background instead of 60 to 74 s.
  • crates/preview disk.rs: while the set is being worked out (keep_last_pending), a full cache prunes nothing, so an edited photo's only rendered picture is never let go before it is known. The first write after the set arrives prunes as before. keep_last now adds to the set, so keys added one by one meanwhile stay.
  • crates/catalog Photo::camera_defaults_as(relative) / relative_wb_key(), and crates/engine import::looks_as_shot_given: the same answers as camera_defaults / looks_as_shot, given the camera lookup once.
  • The open log line splits the old stage: thumbnail cache (attach), edited previews, thumbnail cache tidy.
  • examples/open_bench.rs: raws of four bodies with an as-shot white balance, a third of them edited, an optional thumbnail cache of one file of N KB per photo (open_bench <dir> [photos] [thumb KB]), and the time until the background search ends.
  • The new row label in all 17 fork languages; changelog and What's New entries.

Not in this PR: reading the catalog (2.5 to 3.9 s here; 1.9 to 3.3 s in the reporter's log) and building the first view (0.8 to 1.4 s). Both grow with the number of photos and are what is left of the open. The second report in #979 (about 20 s on Windows with a smaller library) was not reproduced, so this PR does not claim it.

How it was checked

  • cargo xtask ci --quick on b3e8846 (Windows, 18 Nov): fmt, brand, quarantine, docs, notes, i18n and clippy pass. In the test step lcc-engine passes 2,518 and lcc-preview passes. Ten tests fail that this change doesn't touch: 1 in lcc-engine (tests_face_recognize::unnamed_faces_come_with_look_alikes…) and 9 in lcc-ui-egui (face and people panels, no_language_has_translation_gaps for the face labels, translated_catalogs_do_not_use_missing_menu_triangles, ui_text_has_no_em_dashes on a What's New line, bundled-font paint without craft-fonts). All ten failed the same way on another branch from the same base that night. Parity, layers, assets, deny and wasm were run one by one and pass. cargo xtask sil was not run on its own. Main has since merged Raw: decode Nikon NEFX (merged pixel shift) #1267; this branch merges with it without conflicts and was not re-run on it.
  • A test that fails without this change: opening_a_large_library_finds_its_edited_photos_off_the_opening_thread (2,500 raws; the opening thread looks at 0 photos, counted, not timed). Forcing the old inline path makes it look at 2,500.
  • Ran the app on a real large library: not done. Measured with open_bench on a synthetic one (below).

Other tests: opening_a_small_library_finds_its_edited_photos_at_once (20 photos inline, no activity row); a_damaged_thumbnail_cache_opens_and_its_bad_files_go; in lcc-preview a_full_cache_waits_for_the_pictures_to_keep_last, a_cache_whose_keep_last_search_stopped_prunes_again and truncated_cache_files_are_misses_that_go (empty, marker-only and cut-short files are misses that are removed, never a panic).

Before and after: 300,000 photos, 16.5 GB thumbnail cache (300,000 × 55 KB files). Ryzen 9 5950X, 64 GB, NVMe. open_bench <dir> 300000 55, release build, CHROMAFORGE_PROFILE=1, empty settings folder, warm page cache. The machine was busy with other builds, so absolute times carry noise. Two opens each, in ms:

Stage Before run 0 Before run 1 After run 0 After run 1
catalog 3,734.9 2,752.0 2,553.2 2,782.5
plug-in fields 153.2 84.4 85.5 100.6
view 1,031.3 865.9 901.9 830.6
thumbnail cache (attach) 0.2 0.2 0.3 0.3
edited previews 60,231.3 66,532.1 26.5 42.9
forget local 117.6 110.9 91.7 118.1
keywords 160.5 109.4 106.3 140.4
opened in 65,430.0 70,455.7 3,766.3 4,016.0
edited photos found (background, after open) n/a n/a about 945 about 1,040

The background times come from a second after-run, where the open itself took 5.3 and 5.6 s because a test build was running alongside. On the first open after the library was made, before: 78.2 s, of which edited previews 73.6 s.

Merging

Checklist

  • No new panic path in non-test code
  • New text on screen goes through tr() and has its key in the locale files (one activity row label, 17 languages)
  • The manual is not updated: the only visible change is one activity row for about a second
  • New code has a caller
  • No assets added
  • Ready to merge

🤖 Generated with Claude Code

OneSlips and others added 2 commits November 18, 2026 23:06
…n the background

Opening a library spent most of a minute (300,000 photos) in keep_edited_previews_last, which asked
every photo's camera defaults, and with them its camera's lookups by name in the profile and camera
tables, on the opening thread to find the edited photos whose previews a full cache keeps last.
The thumbnail cache's files were never read (attaching it costs 0.2 ms), which is why clearing it
changed nothing.

- library: the search runs on a thread of its own with a cancellable "Finding edited photos" row,
  stopped when the library closes; up to 2,000 photos it is done inline (under 10 ms). Each camera
  is looked up once per pass (Photo::relative_wb_key, camera_defaults_as, looks_as_shot_given):
  300,000 photos take about 1 s instead of 60-74 s.
- preview: a full cache prunes nothing while the set is pending, and keep_last adds to the set.
- the open log splits the stage into thumbnail cache / edited previews / thumbnail cache tidy.
- open_bench: realistic raws, an optional thumbnail cache of N KB per photo, background time.

300,000 photos with a 16.5 GB cache: opened in 65.4 / 70.5 s, now 3.8 / 4.0 s.

Refs emmertex#979

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@emmertex emmertex left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed: finds edited photos off the opening thread for large libraries (activity-stack row with cancel, cache prunes nothing until the set is known, per-camera lookup memoised), small libraries stay inline. Tests cover large/small/damaged thumbnail cache. Release notes present. CI green.

@emmertex
emmertex merged commit 54ce292 into emmertex:main Nov 18, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants