Thumbnails and Scan Pipeline
Categories:
Thumbnails and Scan Pipeline
RawCull separates catalog scanning, thumbnail preloading, UI-driven thumbnail requests, and full-size preview extraction. They share RawParserKit and disk-cache infrastructure, but they have deliberately different memory-admission rules.
The key current invariant is: catalog preloading warms the 200 px grid cache and disk cache, but only UI demand may admit an image to the preview-size RAM cache. This keeps scan order from displacing the images the user is actively viewing. Cache reuse is also replacement-safe: a file written over the same path is a different source when its size or modification date changes.
Source Map
Catalog Load Flow
flowchart TD
A["User selects catalog folder"] --> B["startCatalogLoad"]
B --> C["Acquire security-scoped access"]
C --> D["ScanFiles.scanFiles"]
D --> E["RawImageLoading.fileMetadata per file"]
E --> F["Sort and filter on MainActor"]
F --> G["Load ratings and valid saved scores"]
G --> H["ScanAndCreateThumbnails.preloadCatalog"]
H --> Gate["ThumbnailPreloadGate: active catalog"]
Gate --> I["200 px grid RAM cache"]
Gate --> J["JPEG thumbnail disk cache"]
F --> K["SwiftUI grid and detail views"]
K --> L["ThumbnailLoader / RequestThumbnail"]
L -. "grid miss waits during preload" .-> Gate
L --> M["Preview RAM cache"]
L --> JCatalog Load Ownership
CatalogController.startCatalogLoad(for:) delegates the transition to
CatalogLifecycleCoordinator. It flushes the previous culling store before
replacing the catalog. A failed flush restores the previous selection. Once
admitted, beginCatalogLoad(for:) cancels old work, resets catalog state, starts
access, and asks the coordinator to own a background-priority loadTask running
handleSourceChange(url:). The task retains the catalog grant until it exits.
handleSourceChange(url:) is main-actor orchestration. File I/O, metadata work,
decoding, and cache I/O are awaited through actors or detached work. After every
significant suspension, isActiveCatalogLoad(_:) and cancellation checks
prevent an old catalog from publishing into the current UI.
Changing catalogs also cancels thumbnail/full-preview work, clears burst analysis state, stops the old security scope, and releases the current scanning actors.
File Discovery
DiscoverFiles enumerates a directory off the main actor and filters extensions
through RawFormatRegistry.allExtensions. Its recursive parameter controls
whether subdirectories are traversed.
The RawParserKit 1.3.2 registry contains:
| Format | Extension | Conformer |
|---|---|---|
| Sony ARW | .arw | SonyRawFormat |
| Nikon NEF | .nef | NikonRawFormat |
| Adobe DNG | .dng | DNGRawFormat |
| Other registered RAW formats | Canon, Fujifilm, Olympus, Panasonic and other extensions | GenericRawFormat |
ScanFiles performs its own non-recursive directory listing and asks
RawFormatRegistry.format(for:) whether each item is supported. Discovery and
scan therefore share registry policy rather than maintaining separate hard-coded
extension lists.
Metadata Scan
ScanFiles.scanFiles(url:onProgress:) starts its own security-scoped access and
uses a sliding task-group window rather than admitting the whole directory at
once. maximumConcurrentReads defaults to
min(8, max(1, activeProcessorCount)) and is injectable for tests. Each child:
- reads URL resource values for name, byte size, content type, and modification date;
- calls the injected
RawImageLoading.fileMetadata(for:)abstraction; - builds a
FileItemwith EXIF metadata and the normalized AF point; - records the package focus-location string when available.
The production adapter, RawParserKitImageLoader, maps
RawParserKit.RawImageLoader.metadata(for:) into the app’s ExifMetadata.
RawParserKit reads ImageIO EXIF/TIFF data, dispatches format-specific details
through RawFormatRegistry, and resolves MakerNote or EXIF subject-area focus
evidence. DNG preview discovery classifies TIFF IFDs with NewSubFileType and
Compression before using its legacy positional fallback, so a JPEG-compressed
raw strip is not mistaken for a rendered preview.
Progress reports the first admitted file, every 32 admissions, and the final non-multiple of 32. This is discovery/admission progress, not a count of finished decodes. Cancellation returns an empty scan result. See RawCullTests/ScanAdmissionTests.swift for the bound and progress contract.
This is a single metadata pass per file. The app no longer runs separate EXIF and MakerNote extraction passes.
If no native focus-location strings are found for the catalog, ScanFiles
falls back to focuspoints.json. This is catalog-wide fallback behavior; it
does not merge JSON entries into a partly populated native result.
FileItem is an app typealias for RawCullCore.RawCullFileItem, keeping scan
output usable by the pure package engines and tests.
Thumbnail Preload
After scan, sort, ratings, and valid saved scores are applied,
ScanAndCreateThumbnails.preloadCatalog(at:targetSize:) walks the catalog again
to warm browsing caches. A catalog URL already recorded in processedURLs is
not preloaded again during the same app session. The actor registers the catalog
with ThumbnailPreloadGate for exactly the lifetime of this preload and always
ends the gate after the task returns or is cancelled.
The outer task group admits at most:
RawImageLoadingConcurrency.thumbnailPreloadLimit
The app-level limit bounds catalog work. RawParserKit adds its own thumbnail decode limiter and coalesces identical URL/size requests, preventing simultaneous RAW decodes from growing without bound at the package boundary.
Before any lookup, preload resolves a preview ThumbnailCacheKey from current
file metadata and derives its 200 px grid representation. For each file, preload
then uses this path:
flowchart LR
A["RAW URL"] --> B{"Preview RAM hit?"}
B -->|"yes"| C["Reuse preview thumbnail"]
B -->|"no"| D{"Disk JPEG hit?"}
D -->|"yes"| E["Decode oriented JPEG"]
D -->|"no"| F["RawImageLoading.thumbnailImage"]
C --> G["Populate 200 px grid cache"]
E --> G
F --> G
F --> H["Encode JPEG data"]
H --> I["Background atomic disk save"]All three branches populate the dedicated grid cache when needed. Disk and
source branches do not insert into the preview-size RAM cache.
RequestThumbnail is its only admitter, so preview RAM admission follows user
demand instead of scan order. NSCache controls eviction; RawCull does not
implement an explicit LRU ordering. After a cold decode, preload resolves the key
again before caching; if the source changed while decoding, the result is
discarded rather than being stored under stale identity.
On a cache-miss extraction, processSingleFile encodes the actor-owned image
and returns an optional (data: Data, key: ThumbnailCacheKey) payload. The
worker calls ThumbnailPreloadGate.finished before awaiting its disk save,
then returns its task-group slot only after saving completes. Pending saves
and encoded payloads share the existing per-group worker bound:
min(4, max(1, activeProcessorCount / 2)). This is not a global write limit.
DiskCacheManager.save already awaits its detached atomic writer. Preload no
longer wraps it in another detached task. The boundary carries Data and a
cache key, not image references. Interactive RequestThumbnail retains its
asynchronous save behavior. Cancellation is checked after waiting for a slot
and before encoding/saving. Already-started atomic writes are awaited, not
interrupted. The catalog grant remains retained until the group drains.
Progress counters mark image processing, not a durable-write receipt. Slow
storage can lengthen total preload time while pending write work stays bounded.
The Two Memory Caches
SharedMemoryCache exposes two synchronous TrackedThumbnailCache wrappers.
Each owns a private NSCache and serializes storage with count/cost accounting
under one recursive lock:
| Cache | Content | Admission policy |
|---|---|---|
Preview cache (memoryCache) | Preview-size images used for detail/list demand | Only RequestThumbnail inserts; lookup uses NSCache |
Grid cache (gridThumbnailCache) | Downscaled 200 px images | Preload populates it; grid requests can return immediately |
Each wrapper owns its eviction delegate, and entry UUIDs prevent a callback for a replaced thumbnail from subtracting its replacement’s cost. Clears and limit changes use the same lock. See Synchronizing Cache Storage and Accounting for the callback ordering and concurrency tests.
Both caches are cost-limited. The preview item count limit is intentionally high so byte cost is the normal binding constraint. Under warning memory pressure both limits shrink to 60%; under critical pressure both caches are cleared and the preview cache is temporarily capped at 50 MiB.
UI-Driven Thumbnail Loading
ThumbnailImageView has two routes:
.gridcalls the sharedThumbnailLoaderactor;.listcallsRequestThumbnaildirectly.
For grid targets of 200 px or less, ThumbnailLoader first checks the grid
cache without acquiring a concurrency slot. A miss joins its FIFO-like slot
queue, which allows at most six active requests and removes cancelled waiters
safely.
There is one step before that queue: a grid miss for the catalog currently being
preloaded waits on ThumbnailPreloadGate, then checks the grid cache again.
The gate tracks completion per file and preload session: finished(sourceURL:sessionID:)
releases that file after processing, including a failed read the interactive
path can retry. For a newly encoded thumbnail this is before awaiting its disk
save, not before JPEG encoding. Other unfinished files remain gated.
end(_:) releases the session remainder; overlapping sessions must all stop
blocking a file before it is eligible. This avoids waiting for an entire large
catalog when the requested file is already ready.
This prevents the visible grid from launching a duplicate cold decode while
preload is already producing the same catalog. Only grid misses in that catalog
wait; AI indexing, semantic search, Review, model downloads, non-grid
requests, and other catalogs are independent of this gate. Cancellation removes
and resumes the gate waiter safely.
After a slot is acquired, the loader reads saved settings and asks
RequestThumbnail for settings.thumbnailSizePreview. The passed grid target
controls the grid-cache fast path; the slower preview request uses the
configured preview size.
RequestThumbnail resolves a request in this order:
- preview RAM cache;
- oriented JPEG disk cache;
RawImageLoading.thumbnailCGImagesource decode.
A disk hit is promoted into preview RAM. A source decode is inserted into preview RAM, encoded to JPEG within the owning actor, then asynchronously saved to the thumbnail disk cache. The cache accounting records current entry count and cost. The current implementation has no separate eviction-history, hit-rate or “boomerang” diagnostics store; use targeted profiling to measure those behaviors.
Requests with the same complete ThumbnailCacheKey share one producer in
RequestThumbnail. Each caller has its own continuation. Cancelling one waiter
does not cancel work needed by other waiters; the producer is cancelled only
when its last waiter leaves.
Replacement-Safe Thumbnail Identity
ThumbnailCacheKey is the identity shared by grid RAM, preview RAM, in-flight
request coalescing, and the disk cache. It has two layers:
ThumbnailSourceFingerprint
standardized file URL
file size
modification date
ThumbnailCacheKey
source fingerprint
purpose: grid or preview
requested pixel size
orientation policy
schema version
The source fingerprint is optional. If filesystem size or modification date cannot be read, RawCull still decodes the image but does not reuse it through a potentially unsafe key. Purpose and requested size prevent a 200 px grid representation from satisfying a larger preview request. The schema version invalidates all older representation semantics without coupling thumbnail invalidation to PhotoAIKit artifacts.
Disk Thumbnail Cache
DiskCacheManager stores quality-0.7 JPEGs under a directory named for
ThumbnailCacheKey.schemaVersion. The filename is an MD5 hash of the complete,
locale-independent cacheIdentifier: schema, source path bytes, size,
modification-date bits, purpose, requested pixels, and orientation policy. MD5
is used only as a compact filesystem name, not for security.
Older root-level JPEG entries are removed when the current representation-aware
cache is initialized. Loads use OrientationNormalizedImageLoader; a corrupt or
partial JPEG is deleted and treated as a recoverable miss. Writes accept
pre-encoded Data and are atomic, so CGImage does not cross an actor/task
boundary.
Full-Size And Zoom Preview Paths
Full-size inspection does not use the normal thumbnail disk cache.
flowchart TD
A["Zoom / comparison source"] --> B{"Source choice"}
B -->|"Thumbnail"| C["RequestThumbnail"]
B -->|"Embedded JPEG"| D["FullSizePreviewLoader"]
B -->|"Developed RAW"| E["ZoomPreviewHandler"]
D --> F{"Sidecar JPEG exists?"}
F -->|"yes"| G["Load oriented sidecar"]
F -->|"no"| H{"Full-size disk cache hit?"}
H -->|"yes"| I["Load cached embedded preview"]
H -->|"no"| J["RawImageLoading.previewCGImage"]
J --> K["Save embeddedJPG variant"]
E --> L{"developedRAW cache hit?"}
L -->|"no"| M["SonyRawFormat.createFullSizeJPEG"]FullSizePreviewLoader prefers a same-basename .jpg sidecar, then the
full-size disk cache, then RawParserKit extraction. RawParserKit itself
coalesces duplicate preview requests and limits expensive full-size decode work
to two concurrent operations.
The normal developed-RAW route calls the Sony-named development helper and uses its own cache variant. The separate exposure-inspection renderer bypasses that cache. The actual decoder capability remains input- and OS-dependent. The full-size cache stores quality-0.85 JPEG data and has a versioned orientation-aware key. Keeping these larger images on disk prevents a few pixel-peeping previews from evicting many browsing thumbnails from RAM.
App Loading Abstraction
The app depends on the RawImageLoading protocol rather than calling vendor
conformers directly:
| Requirement | Use |
|---|---|
fileMetadata | Scan metadata and normalized focus evidence |
thumbnailCGImage / thumbnailImage | On-demand and preload thumbnail generation |
previewCGImage | Embedded/sidecar full-size preview loading |
RawParserKitImageLoader is the production adapter. Tests can inject alternate
loaders without performing real RAW decoding.
Within RawParserKit, RawFormat provides vendor policy:
| Requirement | Use |
|---|---|
extensions, displayName | Dedicated and generic RAW registry lookup and diagnostics |
extractThumbnail | Vendor thumbnail fallback |
extractEmbeddedPreview | Largest usable embedded preview |
focusLocation | MakerNote AF location |
rawFileTypeString | Compression labels |
sizeClassThresholds, rawSizeClass | Body-specific S/M/L size labels |
extractFullJPEG remains only as a deprecated compatibility requirement; new
code uses extractEmbeddedPreview.
What To Check When Changing This Area
- Preserve the rule that scan/preload does not admit disk or source results to preview RAM.
- Keep
ThumbnailPreloadGatescoped only to grid misses for the actively preloading catalog. - Build memory, disk, and coalescing identity from the same complete
ThumbnailCacheKey. - Do not fall back to path-only reuse when source metadata is unavailable.
- Check both the grid-cache fast path and configured preview-size path when changing thumbnail settings.
- Keep
RawFormatRegistry.allExtensionsandRawFormatRegistry.allaligned when adding a format. - Keep expensive decode limits and cancellation behavior covered by concurrency tests.
- Convert actor-owned images to
Databefore detached cache writes. - Version cache keys when orientation or encoded-image semantics change.
- Run cache-identity, scan-admission, contention, cancellation, and replacement-at-same-path tests together after changing this pipeline.
- Inspect
isActiveCatalogLoad(_:)guards if stale results appear after switching folders.
Presentation identity after an asynchronous load
ThumbnailImageView constructs a ThumbnailRequest from URL, target size,
style, file size and modification date. ThumbnailPresentationModel owns the
visible image and a generation UUID. A new URL clears the previous image;
a late completion can publish only if its generation is still current and the
task is not cancelled. This is a UI publication guard in addition to cache-key
identity and producer coalescing. See RawCullTests/ThumbnailPresentationModelTests.swift
and RawCullTests/ThumbnailPreloadGateTests.swift.
The loading bounds are deliberately separate: metadata reads use at most eight
workers, preload uses min(4, max(1, activeProcessorCount / 2)), and explicit
batch extraction uses max(1, activeProcessorCount * 2). Their shared policy is
Model/RawImageLoadingConcurrency.swift; changing one limit does not change all
other workloads.
Format admission and viewer ownership
Registry admission now includes GenericRawFormat; it does not guarantee that
the installed macOS decoder supports a particular camera. Generic formats have
no vendor MakerNote or binary embedded-JPEG fallback. See
RAW Formats and Exposure Inspection for
the exact extension list and why generic display support does not enable the
ARW/NEF/DNG byte-export path.
The pipeline ends at a presentation owner, not directly in a large view body. Loupe, zoom, burst and comparison compose focused sections, with task and keyboard lifetimes described in Viewer Architecture.
Verify ownership separately from throughput
ThumbnailPreloadSaveOwnershipTests.swift stalls saves to check worker admission, gate release, completion ownership, cancellation, preview-RAM admission and disk-backed warm reload. These are fixture contracts. A cached catalog may produce no writes and cannot establish backpressure savings; see Performance Measurement.
Catalog progress publication and measurement
Since October 24, each catalog load owns separate main-actor
CatalogProgressCoalescer instances
for metadata counts and thumbnail completed/total/ETA state. Intermediate UI
publication is coalesced at 100 ms; initial values, thumbnail total and terminal
completion publish immediately. Metadata still reports admission counts, not
completed decodes. The final thumbnail state comes from progressCounts() with
ETA zero before progress is hidden. Cancellation cancels pending ticks, rejects
queued callbacks and checks catalog/coalescer identity before publication.
This changes presentation cadence, not per-file accounting, gate readiness,
preview-RAM admission or save ownership. ProgressCount animates only its ring
for 100 ms, disables that animation for Reduce Motion, and updates counts immediately.
The grid bitmap helper is GridThumbnailResizer (200 px cap); the later grid
investigation retained its production resize policy. See
Performance Measurement for the October 25
reference and the distinction between readiness and first painted frames.
Current decoder execution boundary
RawParserKit 1.3.3 moves the thumbnail decode body to a private @concurrent
helper inside its existing six-slot limiter. RawCull’s preload worker bound
remains separate; this dependency update does not increase it. Decode ordering,
coalescing and cancellation checks are retained. See the
package delta for evidence and limits.
Close-ups does not change preload admission
The November integration leaves scan/preload worker limits and thumbnail identity unchanged. Close-ups loads full-size preview/developed sources through its own host renderer, separately from the main stage’s three-frame working set and the grid/preview thumbnail caches. A small displayed crop is not proof that only a small source bitmap was decoded. See viewer ownership and cache lifetimes.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.