Add memory limits to image NSCache#95
Merged
viktorstrate merged 1 commit intoviktorstrate:mainfrom Apr 8, 2026
Merged
Conversation
c612451 to
a1ff7ba
Compare
Sets a 256MB total cost limit and a 64MB per-object limit on the image cache. Cache costs are now calculated from the decoded RGBA pixel size (width × height × 4 bytes) rather than the compressed download size, which could underestimate real memory usage by 20-50× for typical JPEGs. Images larger than 64MB decoded are not cached — they still load and display normally but won't evict smaller cached images. All insertions are routed through a new setCachedImage helper so cost calculation and the per-object guard are enforced in one place. Closes viktorstrate#94
a1ff7ba to
1c77773
Compare
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
totalCostLimitonMatrixClient.imageCachewidth × height × 4 bytes) rather than the compressed download size. Compressed sizes underestimate real memory usage by 20-50× for typical JPEGs — a 500KB JPEG can easily decode to 24MB — so the previoustotalCostLimitwas not bounding memory meaningfullysetCachedImage(_:forKey:)helper onMatrixClientso cost calculation and the per-object guard are enforced in one place; all three previousimageCache.setObject(...)call sites now route through itTest plan
Closes #94