Skip to content

PERF-IMPORT: a scan, an import and a sidecar write no longer make a table of the whole library first - #1583

Merged
echelon merged 1 commit into
storytold:mainfrom
nledford:import-setup-scale
Oct 14, 2026
Merged

echelon merged 1 commit into
storytold:mainfrom
nledford:import-setup-scale

Conversation

@nledford

@nledford nledford commented Oct 13, 2026 •

Copy link
Copy Markdown
Contributor

Problem

In a large library, Synchronize Folder, the import review and an import sit for seconds before they look at the first file, and each XMP sidecar written (Save Metadata to File, or automatically after an edit) takes seconds too. The time does not depend on the folder: it is spent on the whole library, on the session's thread.

The cause is three tables made of every photo in the library:

  • ScanInput::new built a candidate record and a content-hash entry for every photo (import review, and Synchronize Folder through it).
  • ImportJob::new built a path and a content-hash entry for every photo.
  • StemOwners::of worked out who owns every shared sidecar name in the library (IMG_1.CR3 + IMG_1.JPG → one IMG_1.xmp), to name the sidecar of one photo.

Change

Engine only (crates/engine/src/import.rs, sidecar.rs, sync.rs, cmd/xmp.rs).

  • Scan: ScanInput keeps the library as it is (photos are Arcs, shared, not copied). scan_with looks up the files it found (library_files) and, once they are probed, who has their contents (library_hashes): one pass over the library each, on the scan's thread, and a record only for what matched.
  • Import: ImportJob keeps the library and whether trashed photos are replaced, as they were when it was set up, and makes its two tables with the first files (look_up_library, first thing in prepare_files).
  • Sidecar names: StemOwners::among(catalog, ids) works out the owners of the stems of the photos asked about. All five callers (save_sidecar, sidecar_naming, sidecar_namings, the automatic write, xmp.save) only ever name those photos. A plain path's key is written into a reused buffer instead of being allocated (stem_key_into); any other shape of path goes the long way.
  • Synchronize Folder: SyncInput carries the folder's photo ids; the scan names their sidecars (sidecar::namings_in) after it has seen that the folder is there, and shares one copy of the library with the file scan.

Not in this PR:

  • About 0.8 s of Synchronize Folder's set-up at 3.5 million photos is left: the query for the photos of the folder (catalog.query with a folder filter), which passes over the library on the session's thread. Any pass over the library costs about that much at this size; removing it needs an index of photos by folder in the catalog.
  • The interface still calls these set-up functions on its own thread. With an import that prepares its first batch inline (ui-egui/src/import.rs), the import's tables are made there with that batch.
  • A sidecar write still passes over the library once (under 1 s at 3.5 million). The same index would remove it.
  • No change to how many files are probed or copied at once, or to the folder walk.

How it was checked

  • A test that fails without this change
  • cargo xtask ci on this branch: run gate by gate, see below

Numbers. N=<photos> cargo run --release -p lightcraft-engine --example scan_setup_bench (added here; a synthetic library of unedited JPEG records, a folder of 500 of them, Apple M1 Max, nothing else running; last of three runs):

Photos in the library main (b3d392b4) this branch
100,000 Synchronize Folder set-up (SyncInput::new) 129 ms 26 ms
import review set-up (ScanInput::new) 55 ms 2 ms
import set-up (ImportJob::new) 30 ms 2 ms
naming one photo's sidecar 52 ms 22 ms
1,000,000 Synchronize Folder set-up 1,896 ms 238 ms
import review set-up 909 ms 15 ms
import set-up 600 ms 15 ms
naming one photo's sidecar 770 ms 216 ms
3,500,000 Synchronize Folder set-up 5,827 ms 862 ms
import review set-up 2,585 ms 73 ms
import set-up 1,740 ms 75 ms
naming one photo's sidecar 2,397 ms 763 ms

What moved to the scan's thread: its two look-ups took 826 ms together at 3.5 million photos (scan_with on a folder that is not on the disk), where the scan took 1,406 ms on main after its 2,585 ms of set-up.

Tests (8 new, in import/tests_known.rs and sidecar/tests_stem_owners.rs):

  • The owners worked out for some photos name each of them as the table of the whole library does: all at once, one at a time and every pair, over 26 paths that try the key (case, no ending, dot files, names that are all dots, endings in folder names, letters outside ASCII, one of which lower-cases to ASCII). The table of the whole library is made the way main makes it (kept as a test-only reference).
  • The key written into a buffer is the key, for those paths, for odd shapes, and for every string of up to six of a B . / \ é (55,987 strings).
  • The table for one folder of 200 holds that folder's one shared stem.
  • A scan's look-ups hold the files it found and the contents it asked about, and name the later photo where two have one path or one content, as the old tables did. A whole scan of a folder on disk finds a file the library has by path, one it has by content and a new one.
  • An import makes no table when it is set up, makes them with its first files, finds a duplicate by path, and passes over a trashed photo (or not) as its options said when it was set up, whatever is made of them after.

Run on this branch: cargo fmt --all --check; cargo clippy --workspace --all-targets -- -D warnings (with -A clippy::chunks_exact_to_as_chunks, which rustc 1.99 raises in crates/pipeline on main here); cargo xtask parity, layers, assets; cargo test for lightcraft-catalog, lightcraft-engine, lightcraft-ui-egui, lightcraft, lightcraft-mcp and lightcraft-cli (1,976 tests pass); cargo check -p lightcraft-engine --target wasm32-unknown-unknown. Not run: cargo xtask wasm, and the other crates' tests (they do not depend on what changed).

Reviews. Two independent reviews. The first found that the key made without allocating differed from the key for two shapes of path (..jpg, and a./.), and that the reference in the test was the new code checked against itself; both are fixed (plain_cut, the test-only StemOwners::of). The second checked the fixes: 2,015,539 strings of up to eight characters and 111,111 over a wider alphabet, no difference; and reasoned through Windows paths, where it found none either (not run on Windows).

Merging

Checklist

  • No new panic path in non-test code
  • New code has a caller
  • docs/whats-new.md has its entry. No row of docs/parity-checklist.md describes this (no feature changes); docs/gaps.md has no entry for it
  • Ready to merge

🤖 Generated with Claude Code

…able of the whole library first

Setting up Synchronize Folder, an import review or an import made a table
of every photo in the library on the session's thread before the first
file was looked at, and naming one photo's sidecar worked out who owns
every shared sidecar name in the library. In a library of 3.5 million
photos (`scan_setup_bench`, release, a folder of 500): 5.8 s before a
Synchronize scan, 2.6 s before an import review, 1.7 s before an import,
2.4 s for each sidecar written.

- A scan keeps the library as it is (photos are shared, not copied) and
  looks up the files it finds (`library_files`) and the contents it probes
  (`library_hashes`) on its own thread.
- An import looks the library up with its first files
  (`ImportJob::look_up_library`), from the library and the options as they
  were when it was set up.
- The owners of shared sidecar names are worked out for the photos asked
  about (`StemOwners::among`): all five callers name only those photos.
- Synchronize Folder names its photos' sidecars in the scan, after it has
  seen that the folder is there.

Now 0.9 s, 0.05 s, 0.05 s and under 1 s. What is found, and how sidecars
are named, is unchanged: the tests hold the owners against the table of
the whole library as it was made before, and the key made without
allocating against the key, for every string of up to six of the
characters that decide it.

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

echelon commented Oct 14, 2026

Copy link
Copy Markdown
Member

Thank you @nledford! This landed on main in integration batch 35 (#1682).

If you haven't already, please join our Discord: https://discord.gg/artcraft. We'd love to have you there.

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