Memory Cache
Categories:
Cache System
RawCull uses several caches because RAW decoding is expensive and because different UI surfaces need different representations and lifetimes. The normal thumbnail path is layered RAM -> disk -> RAW extraction. The zoom path has its own disk cache for larger embedded JPEGs. Similarity has reusable per-file artifacts plus a separate catalog-wide burst snapshot. These layers must not share keys merely because they originate from the same RAW file.
Source Map
| Cache | Main files | Stores |
|---|---|---|
| Thumbnail identity | Model/Cache/ThumbnailCacheKey.swift | Source fingerprint plus representation purpose, size, orientation, and schema |
| Preview memory cache | Actors/SharedMemoryCache.swift, Model/Cache/CachedThumbnail.swift | Larger NSImage thumbnails admitted by UI demand |
| Grid memory cache | Actors/SharedMemoryCache.swift | Small 200 px thumbnails populated by preload |
| Thumbnail disk cache | Actors/DiskCacheManager.swift | Representation-aware JPEG thumbnail files under the app cache directory |
| Full-size JPEG disk cache | Actors/FullSizeJPGDiskCache.swift, Model/Handlers/ZoomPreviewHandler.swift | Larger embedded JPEG previews for zoom |
| Per-file similarity artifacts | Intelligence/Persistence/PerFileAnalysisArtifactStore.swift | Individually validated Vision/CLIP SimilarityArtifact records |
| Burst analysis cache | Intelligence/Persistence/BurstAnalysisCache.swift | Derived catalog snapshots of artifacts, scores, groups, rankings, and review state |
| Cache accounting | Model/Cache/TrackedThumbnailCache.swift, Actors/SharedMemoryCache.swift | Lock-backed preview/grid item counts and byte costs kept correct across eviction |
Thumbnail Lookup
flowchart TD
A["Request a purpose and pixel size"] --> Key["Resolve ThumbnailCacheKey"]
Key --> B{"Matching memory representation?"}
B -->|"hit"| C["Return NSImage"]
B -->|"miss"| D{"Matching disk representation?"}
D -->|"hit"| E["Decode JPEG and refill memory"]
D -->|"miss"| F["RawParserKit extractThumbnail"]
F --> G["Create NSImage"]
G --> H["Store preview memory for UI demand"]
G --> I["Store grid memory copy for preload"]
G --> J["Save disk JPEG"]The same lookup order is used by preload and on-demand requests, but admission
differs. ScanAndCreateThumbnails warms disk and the 200 px grid cache after a
catalog opens. ThumbnailLoader and RequestThumbnail resolve UI demand; only
RequestThumbnail admits disk or source results into preview RAM.
ThumbnailPreloadGate prevents a grid miss from duplicating cold work already
being performed by preload for the active catalog.
Cache Identity
ThumbnailCacheKey is the single identity used by memory lookup, disk lookup,
and exact-key request coalescing. It contains:
- a standardized source URL;
- source byte size and modification date;
- representation purpose (
gridorpreview); - requested pixel size;
- orientation policy;
- thumbnail schema version.
This answers two separate questions: “Are these still the same source bytes?” and “Is this the representation the caller requested?” A path-only key fails the first question when a file is replaced in place. A key without purpose or size fails the second by allowing a small grid image to satisfy a preview request.
If source metadata cannot be resolved, the key initializer returns nil. The
image may still be decoded for the caller, but RawCull avoids persistent or
coalesced reuse rather than assigning unsafe identity.
ThumbnailCacheIdentityTests covers file replacement, requested-size
separation, purpose separation, orientation policy, and schema participation.
CachedThumbnail
CachedThumbnail wraps an immutable NSImage, its cache cost, and the
standardized source URL associated with the representation.
The cost is calculated from image representations:
cost = sum(rep.pixelsWide * rep.pixelsHigh * 4) * 1.1
The 4 is SharedMemoryCache.costPerPixel, fixed for RGBA. The 1.1
multiplier gives a small overhead buffer for the wrapper and image metadata.
CachedThumbnail is @unchecked Sendable. The project invariant is: construct
the NSImage fully before caching it, then treat it as immutable.
The class intentionally does not conform to NSDiscardableContent. Earlier
versions did, but diagnostics showed NSCache discarded entries too
aggressively and destroyed the RAM hit rate. Eviction is now controlled by
explicit cache limits and memory-pressure handling.
SharedMemoryCache
SharedMemoryCache is an actor singleton:
actor SharedMemoryCache {
nonisolated static let shared = SharedMemoryCache()
}
It is still an actor because it owns configuration, pressure monitoring,
disk-cache references, and statistics. Its two TrackedThumbnailCache wrappers are exposed as nonisolated let
properties because the app needs synchronous lookup APIs. Each wrapper protects
its private NSCache and count/cost metadata with one recursive lock.
That gives RawCull both:
- actor isolation for mutable app-owned state,
- fast synchronous cache reads for hot thumbnail paths.
Both memory caches are keyed by the complete ThumbnailCacheKey. The preview
and grid APIs remain separate even though they use the same identity type,
making admission policy visible at the call site.
Adaptive Limits
CacheRecommendationPolicy chooses cache caps from physical memory, current
used memory, user maximums, and memory pressure.
Baseline limits:
| Machine memory | Preview baseline | Grid baseline |
|---|---|---|
| 64 GB or more | 8000 MB | 2000 MB |
| 32 GB or more | 4096 MB | 1024 MB |
| Less than 32 GB | 2048 MB | 768 MB |
At normal pressure, RawCull calculates available headroom:
freeMB = physicalMB - usedMB
expandableMB = max(0, freeMB - 3072)
extraBudgetMB = expandableMB * 0.5
previewMB = baselinePreview + extraBudgetMB * 0.65
gridMB = baselineGrid + extraBudgetMB * 0.35
Values are rounded up to 256 MB steps and capped by both the machine tier and user settings.
At warning or critical pressure, the policy falls back toward 60 percent of the baseline, again respecting user limits and minimum settings.
Eviction Accounting
Each TrackedThumbnailCache owns its eviction delegate and serializes storage
mutations with count/cost accounting. Metadata is registered before insertion,
and a unique entry ID prevents a callback for a replaced value from subtracting
the new value’s cost. Metadata retains no thumbnail references, so it does not
keep evicted image memory alive. Clears and limit changes use the same lock.
See Synchronizing Cache Storage and Accounting
for the replacement sequence, recursive-lock rationale, delegate lifetime, and
concurrency regression tests. NSCache does not expose current cost or item
count; the wrapper supplies those values. The current tree does not maintain a
separate eviction-history or hit-rate diagnostics store.
Memory Pressure
SharedMemoryCache owns a DispatchSourceMemoryPressure.
| Kernel event | RawCull response |
|---|---|
.normal | Restore cache configuration from settings/adaptive policy |
.warning | Reduce preview and grid cache cost limits to 60 percent of their current limits |
.critical | Clear both memory caches, reset accounting, set preview cache limit to 50 MiB until recovery |
The current pressure level is stored behind an OSAllocatedUnfairLock and
exposed as a synchronous nonisolated property. This lets MemoryViewModel
read pressure state without an actor hop.
Disk Caches
DiskCacheManager stores generated quality-0.7 thumbnail JPEGs in a
schema-specific directory. Its filename is an MD5 hash of the complete cache
identifier. MD5 is a fixed-width filesystem key here, not a security primitive.
A corrupt JPEG is removed and treated as a cache miss, and writes are atomic.
Callers encode to Sendable Data before crossing the actor/task boundary.
FullSizeJPGDiskCache stores larger embedded previews for the zoom overlay. It
is deliberately separate from the normal thumbnail cache because zoom images are
larger and should not compete with grid scrolling for memory.
Similarity And Burst Analysis Caches
Similarity persistence has two levels under Application Support:
| Level | Owner | Reuse unit |
|---|---|---|
| Per-file artifact store | PerFileAnalysisArtifactStore | One source fingerprint and backend descriptor |
| Burst analysis snapshot | BurstAnalysisCache | One compatible catalog analysis context |
The per-file store lets RawCull reuse valid SimilarityArtifact values when a
catalog-wide snapshot is stale or incomplete. Records include their own schema,
source identity, backend/model descriptor, and pipeline signature. Invalid
records are ignored or removed individually.
BurstAnalysisCache is not an image cache. Its current schema is 9. It stores:
- typed Vision or CLIP similarity artifacts,
- sharpness scores and saliency info,
- burst groups and boundary evidence,
- ranking results,
- review states,
- a digest of the descriptor-and-payload artifact set.
A snapshot is accepted only when the catalog, file count, every file’s size/modification date, thumbnail size, sharpness descriptor, grouping configuration, backend and artifact descriptors, artifact schema, pipeline version, artifact digest, cache schema, and grouping algorithm version still match. File UUIDs are remapped by path after a new scan. A legacy schema may supply migration candidates, but it is not accepted as a current snapshot.
What To Check When Changing This Area
- Keep
CachedThumbnailimmutable after insertion. - Use
ThumbnailCacheKeyconsistently for RAM, disk, and in-flight coalescing; never fall back to path-only reuse. - Keep grid and preview purposes separate and bump the thumbnail schema when representation semantics change.
- Route memory-cache mutations through
TrackedThumbnailCacheso storage and accounting stay synchronized. - Use Instruments or temporary signposts to distinguish eviction churn from decoding or I/O regressions; the production tree does not export hit-rate logs.
- Do not feed zoom-preview images into the normal thumbnail RAM cache unless you intentionally want them competing with grid thumbnails.
- Keep per-file AI artifact persistence separate from the catalog-wide derived snapshot.
- When changing scoring, similarity, or burst algorithms, update descriptors, signatures, digests, and schema/algorithm versions so stale analysis data is rejected.
Settings deletion includes durable review evidence
CacheSettingsTab.swift now presents a combined
cache total: preview/grid RAM, thumbnail/full-size disk data, similarity artifacts,
burst snapshots and saved Combined Reviews. Its Delete Cache action first calls
CombinedReviewFeature.clearAllReviews(), then clears shared caches, per-file
analysis artifacts and burst snapshots. A held Combined lease disables the
button; review deletion also checks the lease at the operation boundary.
Saved Combined reports, masks, retained inputs and history are user-visible evidence, even though this action groups them under cache cleanup. They are deleted by the confirmed action. Do not describe every Settings cache entry as losslessly regenerable: rerunning inference need not reproduce the same report. Original photos, settings and downloaded model packs are outside this purge.
Saved Files has its own confirmed deletion action and size accounting. Read File Read and Write before changing either path. The burst workspace’s three-frame session cache is a separate working set described in Viewer Architecture.
Preload write lifetime and whole-process memory
Preload releases a file’s grid gate before awaiting its disk save, and retains the worker slot until saving returns. The per-group concurrency limit covers pending saves and encoded JPEGs. The background atomic writer and interactive thumbnail-save behavior are unchanged. A started write may finish during cancellation.
Thumbnail-cache limits are not a whole-process memory cap. Loupe, zoom, comparison, full-size sources and framework scratch allocations have separate lifetimes. See Performance Measurement for the October 25 reference, unresolved resource signals and the historical October 23 workload. These workloads are not interchangeable memory comparisons.
Completed ZoomView RAW previews
RAWPreviewRenderer.shared owns a separate 256 MiB byte-bounded LRU of completed
pixels. Cost is bytesPerRow × height; this is not a total-process memory cap.
Keys include standardized source URL, size, modification date, requested dimension
and optional inspection EV. Fit/native and normal/inspection variants are distinct;
obsolete revisions and prior inspection exposures at the same size are replaced.
Oversized images can be returned without being retained.
Closing ZoomView releases the source filter and Core Image context while retaining
bounded completed previews. Warning/critical memory pressure and clearCaches()
clear both previews and source/context. Normal native previews retain the existing
persistent developed-JPEG policy. These caches are independent of thumbnail budgets,
export output and burst’s three-frame working set.
Close-up artifacts and source identity
Close-ups adds a separate inspection working set. Its renderer retains no decoded source cache of its own, although the host preview/developed loaders can reuse their existing caches. PhotoEvidenceKit redraws crop and overview into owned, bounded raster buffers, and card disappearance releases those artifacts. A crop must not accidentally retain the whole source through a shared image backing.
This does not extend the burst stage’s three-frame cache budget to every card. Source decoding can allocate full-size transient images; serialization applies within the close-up renderer, not across every image consumer. Thumbnail budgets and the RAWPreviewRenderer LRU are not a cap on the complete panel/process.
Frame identity includes the RAW file revision; render identity separately includes preview sidecar or development settings. A sidecar change rerenders pixels while preserving a valid RAW-bound manual region. The UserDefaults region store is a preference store and is not included in the existing Settings cache byte total or explicit purge paths. See file storage.
Disposable human face evidence
FaceEvidenceCache writes JSON atomically under the app cache directory,
using a hash of the standardized file URL. The signature checks the source
revision, preview configuration, algorithm version, Vision revision and OS.
The saved preview dimensions must also match. Cache hits reuse human face
geometry and detail, while RawCull renders new crop pixels from the decoded
preview. Animal landmarks are evaluated separately and remain transient.
Manual region records are stored separately.
Unreadable similarity artifacts
An artifact read error preserves the stored record. Forced re-indexing can
replace it when a new artifact is generated successfully. See
PerFileAnalysisArtifactStore.swift
and the tests for PerFileAnalysisArtifactStore before changing read or refresh behavior.
Review masks after upgrading
The runtime attempts to remove the obsolete Objects mask directory during initialization. Current Review outlines resolve a retained input key from a displayed result and recheck the source fingerprint before loading the image. Clearing retained inputs can remove the outline while leaving its measurements and report readable. RawCullAIModelRuntime · ReviewSubjectMasks
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.