nightshift/perf: cache decoded images to eliminate repeated disk reads#3
Open
nightshift/perf: cache decoded images to eliminate repeated disk reads#3
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
SnippetStore.loadImage(for:)calledNSImage(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
NSCache<NSString, NSImage>toSnippetStoreloadImage(for:)now returns the cached image when available; falls back to disk and populates the cache on the first loadaddImage(_:)primes the cache with the already-decodedNSImageso the first render after a paste is also free of disk I/Odelete(_:)evicts the cache entry alongside removing the file, preventing stale dataNSCacheis used rather than a plainDictionaryso the OS can reclaim memory automatically under pressure without any manual eviction logic needed.Expected impact
NSCacheeviction policy