Skip to content

Decode local images on iOS at the size they're drawn - #151

Merged
simonhamp merged 1 commit into
perf/ios-image-cache-off-mainfrom
perf/ios-image-downsample
Oct 22, 2026
Merged

simonhamp merged 1 commit into
perf/ios-image-cache-off-mainfrom
perf/ios-image-downsample

Conversation

@simonhamp

Copy link
Copy Markdown
Member

Stacked on #149, so the diff here is only the downsampling. Retarget to main once #149 merges.

After #149, local images load off the main thread but still decode at full size. A 2400x1600 photo in a 318x212 pt frame takes 15 MB, and the 64 MB cache holds about four of them. In fast flings on the simulator, photos showed their placeholder for up to about 0.3 s. Coil on Android already decodes at the view's size. This does the same on iOS.

The change

  • NativeUILocalImage measures its laid-out size with onGeometryChange and asks the cache for an image that fills that many pixels. Measuring doesn't change layout. Before the image arrives it measures the placeholder, which fills the element's frame, so rows still don't move.
  • The cache still reads the file with UIImage(contentsOfFile:), so UIKit keeps choosing the file, scale and orientation as before. It turns out it picks a [email protected] sitting beside name.jpg on a 3x phone. When the frame is smaller than the file, it decodes with preparingThumbnail(of:), which is ImageIO's thumbnail decode with the orientation handled. Otherwise it does the same full decode as before.
  • The thumbnail keeps the full file's size in points. An image laid out at its natural size or aspect ratio comes out the same size as before.
  • There's still one cache entry per path. A file drawn at two sizes keeps the bigger decode, which serves the smaller frame too. When a frame grows, the image on screen keeps drawing until a bigger decode replaces it. It doesn't go back to the placeholder.
  • The size it decodes to fills the frame, for fit images as well as fill ones. A contained image whose frame is a different shape from the image can decode bigger than it's drawn, but never smaller. I went that way because a flexible fit image's placeholder doesn't know its final height, and decoding to fit that guess would give a blurry first frame and a second decode.

Numbers

Same setup as #149: iPhone 17 Pro simulator (iOS 26.2), a 300-row native:list of local JPEGs, cold launch, then 30 scripted flings. Before is #149 on its own.

Before After
Decode samples, all threads (Time Profiler, 2 runs) 2,320 / 2,387 1,269 / 1,233
Photo decodes during the flings 113 at 3.84 MP 77 at 0.61 MP
Avatar decodes during the flings 326 81
Footprint after launch / 20 flings down / 10 up (2 runs) 307-312 / 437-467 / 507-514 MB 259-263 / 290-295 / 292-294 MB
CG Image memory at the end 217-224 MB no CG Image regions listed
Placeholder on screen, 8 fast flings down (3 runs) 1.8-2.1 s 1.1-1.4 s
Placeholder on screen, 8 fast flings back up (3 runs) 2.2-2.3 s 1.5-1.8 s

The main thread is unchanged from #149: one or two decode samples per run.

Each photo decode is slower on the simulator, though. The median went from 15.4 ms (full size) to 23.8 ms (954x636). In a standalone test the thumbnail decode costs about 20 ms against 12 ms for a full decode of the same file. Total decode time still drops by about half because there are far fewer decodes: the cache now holds about 26 of these photos instead of 4, so avatars and recently seen photos stop being thrown out. A phone has a hardware JPEG decoder, so the per-decode numbers could look different there.

Checked on the simulator

  • Layout doesn't change. The accessibility frames of every element on the first screen are identical before and after. Left is before, right is after:

  • EXIF orientation still works: the photo stored as portrait pixels with orientation 6 draws upright (the "ROT" row above).
  • Downsampled photos are as sharp as before. They are drawn at one pixel per screen pixel instead of being scaled down by the GPU. Before on the left, after on the right, 2x zoom:

  • A growing frame gets a bigger decode. On a test row whose image goes from 60x40 to 300x200 pt every few seconds, the new size starts a 900x600 decode within a millisecond of the layout change, and it lands about 24 ms later. Shrinking back reuses the bigger decode without decoding again. The grown image is as sharp as the full decode. Left is before, middle is after, right is what the 60x40 decode would look like stretched:

  • A big photo is never in memory at full size. Decoding an 8000x6000 JPEG for a small frame raised peak memory by 9 MB, against 432 MB for the full decode.

For review

  • Before this change, CG Image memory went well past the cache's 64 MB limit (217-224 MB). So decoded images are also being kept alive outside the cache, probably by each row's held image or its layer. Smaller decodes shrink that a lot, but they don't put a limit on it.
  • A file that's drawn small first and big later is decoded twice. The second decode replaces the first in the cache.
  • I tested on top of combined/dev-null-app (both files match main apart from one unrelated icon line), with a local test screen in a clone of dev-null-app. Nothing from the test app is in this PR.
  • swiftc -typecheck -wmo against mobile-air's NativeRender gives the same baseline errors as Load local images off the main thread on iOS #149 (core types outside that folder) and nothing new, with planted errors to prove the new code was checked. With -strict-concurrency=complete the only warning in the file is still the existing cache static.

🤖 Generated with Claude Code

NativeUILocalImage decoded every file at full size, so a 2400x1600 photo
in a 318x212 pt frame took 15 MB, and the 64 MB cache held about four of
them. Coil on Android decodes at the view's size.

The view now measures its laid-out size with onGeometryChange, which
changes nothing about layout, and asks the cache for an image big enough
to fill that many pixels. The cache reads the file with
UIImage(contentsOfFile:), so UIKit still picks the @3x sibling, the scale
and the orientation, then decodes with preparingThumbnail(of:) when the
frame is smaller than the file. The thumbnail keeps the full file's size
in points, so an image laid out at its natural size or ratio doesn't move.

There's still one cache entry per path. A file drawn at two sizes keeps
the bigger decode and serves both. When a frame grows, the image already
on screen keeps drawing until a bigger decode replaces it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@simonhamp
simonhamp marked this pull request as ready for review October 22, 2026 18:16
@simonhamp
simonhamp merged commit ca63ed9 into perf/ios-image-cache-off-main Oct 22, 2026
4 checks passed
@simonhamp
simonhamp deleted the perf/ios-image-downsample branch October 22, 2026 18:17
simonhamp added a commit that referenced this pull request Oct 22, 2026
* Load local images off the main thread on iOS

NativeUIImageCache stat-ed the file on every body evaluation to build its
key, and UIImage(contentsOfFile:) read and decoded the file on the main
thread. A trace of a list of local images scrolling on a phone spent 0.78 s
of 75 s there.

The cache is now keyed by path, and the main thread only looks in memory.
NativeUILocalImage shows a cached image straight away, otherwise a
placeholder in the image's frame while the file is checked, read and decoded
in the background. Rows showing the same file share one load, and a load is
cancelled when every image waiting on it has gone off screen. Each appearance
re-checks the file's modification date and size in the background, so a file
overwritten in place still shows its new bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* Decode local images on iOS at the size they're drawn (#151)

NativeUILocalImage decoded every file at full size, so a 2400x1600 photo
in a 318x212 pt frame took 15 MB, and the 64 MB cache held about four of
them. Coil on Android decodes at the view's size.

The view now measures its laid-out size with onGeometryChange, which
changes nothing about layout, and asks the cache for an image big enough
to fill that many pixels. The cache reads the file with
UIImage(contentsOfFile:), so UIKit still picks the @3x sibling, the scale
and the orientation, then decodes with preparingThumbnail(of:) when the
frame is smaller than the file. The thumbnail keeps the full file's size
in points, so an image laid out at its natural size or ratio doesn't move.

There's still one cache entry per path. A file drawn at two sizes keeps
the bigger decode and serves both. When a frame grows, the image already
on screen keeps drawing until a bigger decode replaces it.

Co-authored-by: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
simonhamp added a commit that referenced this pull request Oct 22, 2026
Brings in #149 (with #151), #152 and the final #145, plus everything else
merged to main since #144. The four conflicts were all in files where
combined's own changes (#145, #149, custom icons, the pager header and the
sticky header) are now on main in their final form, so main's version wins.
Combined still carries text-outline (#142) and the Android sheet handle on
top of main.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
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