Skip to content

Add memory limits to image NSCache#95

Merged
viktorstrate merged 1 commit intoviktorstrate:mainfrom
beezly:feature/image-cache-limits
Apr 8, 2026
Merged

Add memory limits to image NSCache#95
viktorstrate merged 1 commit intoviktorstrate:mainfrom
beezly:feature/image-cache-limits

Conversation

@beezly
Copy link
Copy Markdown
Contributor

@beezly beezly commented Apr 7, 2026

Summary

  • Sets a 256MB totalCostLimit on MatrixClient.imageCache
  • Adds a 64MB per-object limit — images larger than 64MB decoded are not cached (they still load and display, but won't evict smaller cached images)
  • Fixes cost calculation: costs are now computed from decoded RGBA pixel size (width × 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 previous totalCostLimit was not bounding memory meaningfully
  • Introduces a setCachedImage(_:forKey:) helper on MatrixClient so cost calculation and the per-object guard are enforced in one place; all three previous imageCache.setObject(...) call sites now route through it

Test plan

  • Navigate through channels with many images — memory usage should stabilise around 256MB of decoded image data rather than growing unboundedly
  • Very large images (>64MB decoded, e.g. a ~16MP photo) still load and display correctly, they just re-download on revisit
  • Normal images are served from cache on channel revisit

Closes #94

@beezly beezly force-pushed the feature/image-cache-limits branch from c612451 to a1ff7ba Compare April 7, 2026 14:39
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
@beezly beezly force-pushed the feature/image-cache-limits branch from a1ff7ba to 1c77773 Compare April 7, 2026 14:45
@viktorstrate viktorstrate merged commit 88c0410 into viktorstrate:main Apr 8, 2026
2 checks passed
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.

Set a memory limit on the image NSCache

2 participants