Skip to content

nightshift/perf: cache decoded images to eliminate repeated disk reads#3

Open
perandre wants to merge 1 commit intomainfrom
nightshift/perf-2026-04-09
Open

nightshift/perf: cache decoded images to eliminate repeated disk reads#3
perandre wants to merge 1 commit intomainfrom
nightshift/perf-2026-04-09

Conversation

@perandre
Copy link
Copy Markdown
Owner

@perandre perandre commented Apr 9, 2026

Summary

SnippetStore.loadImage(for:) called NSImage(contentsOf:) on every SwiftUI render pass — synchronous disk I/O on the main thread for every visible image snippet, on every frame. With several image snippets in the list this causes measurable jank while scrolling or resizing the panel.

Changes

  • Added an NSCache<NSString, NSImage> to SnippetStore
  • loadImage(for:) now returns the cached image when available; falls back to disk and populates the cache on the first load
  • addImage(_:) primes the cache with the already-decoded NSImage so the first render after a paste is also free of disk I/O
  • delete(_:) evicts the cache entry alongside removing the file, preventing stale data

NSCache is used rather than a plain Dictionary so the OS can reclaim memory automatically under pressure without any manual eviction logic needed.

Expected impact

  • Zero disk reads for image snippets on any render after the first
  • Panel scrolling and open animation stay on the main-thread fast path
  • Memory footprint self-regulates via NSCache eviction policy

loadImage(for:) called NSImage(contentsOf:) on every SwiftUI render
pass, doing synchronous disk I/O on the main thread for each visible
image snippet. Add a simple NSCache so the file is read at most once
per session; cache entries are evicted automatically on memory pressure.
Invalidate the relevant entry on delete and on addImage so callers
always see the current image.
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.

1 participant