krz/moonrise

Night-sky companion for iPhone, Apple Watch and Vision Pro: what's up tonight, the Moon, planets, launches and missions, and a Library of the solar system. SwiftUI app with a Cloudflare Workers backend. astronomy cloudflare-workers ios night-sky space swiftui visionos watchos

Commit 6a7dfc7bf6

6a7dfc7bf68817557dc1f68b285a6a849d1fa3a0

parent: bb6579fe06

Unsigned

cmc <hello@cleberg.net> · 2026-10-21 23:35 UTC

Sun tab: decode SDO images off the main thread (!99)

- `SunScreen.loadImage` passes each SDO image through `UIImage.byPreparingForDisplay()` before showing it, so the JPEG is not decoded on the main thread at first draw (about 17 ms in the !77 Time Profiler run).
- iOS build passes. Not measured and not checked visually; the simulator UI driver was unreliable.
- The rest of the Sun tab's first frame (SwiftUI/UIKit 91 ms, conformance lookups 67 ms, Charts 51 ms in the simulator) is unchanged; it needs a device measurement before more work.

Layout: unified · split

App/Sources/SunScreen.swift +9 −4
@@ -107,7 +107,7 @@ struct SunScreen: View {
107107 if let (data, response) = try? await URLSession.shared.data(from: w.url(size: 1024)),
108108 let http = response as? HTTPURLResponse, http.statusCode == 200, let image = UIImage(data: data) {
109109 if w.isUsable(data) {
110 picture = Picture(wavelength: w, image: image, loaded: .now)
110 picture = Picture(wavelength: w, image: await decoded(image), loaded: .now)
111111 let modified = http.value(forHTTPHeaderField: "Last-Modified").flatMap { try? Date($0, strategy: .http) }
112112 save(data, time: modified ?? .now, to: file)
113113 return
@@ -119,20 +119,25 @@ struct SunScreen: View {
119119 }
120120 guard !Task.isCancelled else { return }
121121 if let frame, let saved = UIImage(data: frame.data) {
122 picture = Picture(wavelength: w, image: saved, loaded: .now, frame: frame.time)
122 picture = Picture(wavelength: w, image: await decoded(saved), loaded: .now, frame: frame.time)
123123 } else {
124 picture = Picture(wavelength: w, image: image, loaded: .now)
124 picture = Picture(wavelength: w, image: await decoded(image), loaded: .now)
125125 }
126126 return
127127 }
128128 guard !Task.isCancelled else { return }
129129 if let frame = savedFrame(file, w), let image = UIImage(data: frame.data) {
130 picture = Picture(wavelength: w, image: image, loaded: .now, saved: frame.time)
130 picture = Picture(wavelength: w, image: await decoded(image), loaded: .now, saved: frame.time)
131131 } else {
132132 imageFailed = true
133133 }
134134 }
135135
136 /// The image decoded off the main thread, so its first draw does not decode the JPEG there.
137 private func decoded(_ image: UIImage) async -> UIImage {
138 await image.byPreparingForDisplay() ?? image
139 }
140
136141 /// The saved image and its frame time, if it is usable: images saved before the check may be black.
137142 private func savedFrame(_ file: URL, _ w: SolarImage) -> (data: Data, time: Date)? {
138143 guard let data = try? Data(contentsOf: file), w.isUsable(data) else { return nil }