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 10eb70e1c4

10eb70e1c45bd155729aa39ed4413bb54e6cd06b

parent: 4c72d5eff7

Unsigned

cmc <hello@cleberg.net> · 2026-10-21 09:16 UTC

Satellite passes: 60/180 s steps well below the horizon, engine lock only around the Sun (!80)

`SkyEngine.passes(of:)` took 751 ms for the 158-object visual group and 136 ms for a 30-object launch. It propagated every 20 s for 72 h (12,961 SGP4 calls per object) whatever the altitude, and held `SkyEngine.lock` for the whole object. `NSRecursiveLock` is not fair, so the passes thread re-took it between objects and engine calls on other threads waited far longer than one object's ~4.7 ms (see the lock table). Engine report (perf/engine.md) finding 5.

## Changes

- `passes(of:)` steps 20 s from -8° up, 60 s below -8° and 180 s below -30°. Samples stay on the 20 s grid from `start` (`start + i × 20`), so as soon as the satellite is within 8° of the horizon the samples are the same as before, and rise, peak, set, azimuths and `visible` are unchanged.
- Step sizes: below the horizon (spherical Earth), altitude changes no faster than the satellite's angle around Earth's centre relative to the observer. That is about 4°/min in low orbit and under 6.5°/min for any orbit with perigee above the surface (escape speed at Earth's radius is 6.0°/min, plus 0.25°/min for Earth's rotation). A 60 s step from below -8° skips 40 s (at most 4.3°); a 180 s step from below -30° skips 160 s (at most 17.3°). Neither can step over a 20 s sample at or above the horizon. Measured over the fixtures (160 sets plus Vanguard 1, e 0.19) at 0°, 42° and 70° N, 20 s sampling: fastest change below the horizon 3.92°/min; highest altitude within 40 s of a sample below -8°: -5.95°; within 160 s of a sample below -30°: -23.5°.
- `lock` is held only around the Sun calls in the visibility test (`time`, `Astronomy_Equator`, `Astronomy_Horizon`, `sunDirection`). SGP4 and `lookAngles` are plain Swift with no shared state.
- `PassSearchTests`: the previous 20 s search as a reference, against `passes(of:)` for ISS, Tiangong, Hubble, a Starlink train and its stage, 155 synthetic low-orbit sets (the benchmark's visual-group generator) and Vanguard 1, at Quito, Chicago and Tromsø, 72 h. Requires the same number of passes per object, rise/peak/set within 1 s, same `visible` and peak altitude. With steps changed to 20/120/600 s it fails at Quito and Chicago.

Launch first pass: not limited to the train and stage. The launch sheet and the notification both use the earliest visible pass of any of the launch's objects, and the server sends at most 8 (a train, then rocket bodies and other objects), so dropping objects would change what is shown. At 8 objects it is now 9.9 ms.

## Measurements

Release benchmark package (perf/), M2 Pro, Chicago, 2026-10-20 18:00 UTC, medians. Load (`uptime`, 12 cores): main 2.27 → 2.80, branch 3.18 → 2.99 and 3.35 → 3.43.

| | main | branch |
|---|---:|---:|
| Visual group, 158 objects, 72 h (2,184 passes) | 753 ms | 255–256 ms |
| ISS only, 72 h | 4.65 ms | 1.43–1.47 ms |
| Launch, train only | 4.52 ms | 1.23–1.25 ms |
| Launch, 8 objects (server cap) | 36.2 ms | 9.9 ms |
| Launch, 30 objects | 136 ms | 37.2 ms |
| `PassAnswer` incl. ISS propagation | 4.68 ms | 1.45 ms |

Of the 255 ms, about 62 ms is the Sun check, run at every sample above 10° of a pass that is not yet visible (194 ms with it disabled, for measurement only). The geometry-only prototype was 180 ms.

`SkyEngine.horizontal(.moon)` called every 2 ms on one thread while the visual group's passes run on another, 3 runs:

| | main | branch |
|---|---:|---:|
| Median wait | 7.1–31.3 ms | 0.002–0.003 ms |
| Max wait | 79–154 ms | 0.07–0.70 ms |
| Calls completed during the run | 18–45 | 102–103 |

## Tests

- `swift test` (Packages/MoonriseKit): 196 tests passed (195 + `adaptiveStepsFindTheSamePasses`).
- `npm test` (server): 55 passed.
- `xcodebuild test` (Moonrise, iPhone 18 Pro simulator): passed. Accessibility audit 64 issues, the same per screen as on main in !79: Sky 4, Settings 2, System 7, Sun 3, News & launches 21, Library 14, Library page: Saturn 13.

## Not verified

- A device. Timings are from the Mac benchmark; the lock-wait figures are from a synthetic probe, not from the app's main thread.
- Real CelesTrak element sets: the test and benchmark use the fixture sets (three real, the rest synthetic). Objects in deep space or below ~150 km are not in the fixtures; the step bound covers any orbit with perigee above the surface.

Layout: unified · split

Packages/MoonriseKit/Sources/SkyEngine/Passes.swift +10 −5
@@ -105,15 +105,13 @@ extension SkyEngine {
105 return (perp * perp).sum().squareRoot() > 6378.135 105 return (perp * perp).sum().squareRoot() > 6378.135
106 } 106 }
107 107
108 /// Passes above `minAltitude` in the next `hours`, sampled every 20 s. 108 /// Passes above `minAltitude` in the next `hours`, sampled every 20 s from 8° below the horizon up.
109 public static func passes(of elements: OrbitalElements, from start: Date, hours: Double = 72, observer: Observer, minAltitude: Double = 10) -> [SatellitePass] { 109 public static func passes(of elements: OrbitalElements, from start: Date, hours: Double = 72, observer: Observer, minAltitude: Double = 10) -> [SatellitePass] {
110 guard let sgp = try? SGP4(elements) else { return [] } 110 guard let sgp = try? SGP4(elements) else { return [] }
111 lock.lock()
112 defer { lock.unlock() }
113 var out: [SatellitePass] = [] 111 var out: [SatellitePass] = []
114 var current: (rise: Date, riseAz: Double, peak: Date, maxAlt: Double, visible: Bool)? 112 var current: (rise: Date, riseAz: Double, peak: Date, maxAlt: Double, visible: Bool)?
115 var lastAz = 0.0 113 var lastAz = 0.0
116 let step = 20.0 114 var i = 0
117 var t = start 115 var t = start
118 let end = start.addingTimeInterval(hours * 3600) 116 let end = start.addingTimeInterval(hours * 3600)
119 while t <= end { 117 while t <= end {
@@ -123,10 +121,12 @@ extension SkyEngine {
123 if current == nil { current = (t, h.azimuth, t, h.altitude, false) } 121 if current == nil { current = (t, h.azimuth, t, h.altitude, false) }
124 if h.altitude > current!.maxAlt { current!.maxAlt = h.altitude; current!.peak = t } 122 if h.altitude > current!.maxAlt { current!.maxAlt = h.altitude; current!.peak = t }
125 if !current!.visible, h.altitude >= minAltitude { 123 if !current!.visible, h.altitude >= minAltitude {
124 lock.lock()
126 var tt = time(t) 125 var tt = time(t)
127 let sun = Astronomy_Equator(BODY_SUN, &tt, observer.astro, EQUATOR_OF_DATE, ABERRATION) 126 let sun = Astronomy_Equator(BODY_SUN, &tt, observer.astro, EQUATOR_OF_DATE, ABERRATION)
128 let sunAlt = Astronomy_Horizon(&tt, observer.astro, sun.ra, sun.dec, REFRACTION_NORMAL).altitude 127 let sunAlt = Astronomy_Horizon(&tt, observer.astro, sun.ra, sun.dec, REFRACTION_NORMAL).altitude
129 if sunAlt < -6, sunlit(r, sun: sunDirection(t)) { current!.visible = true } 128 if sunAlt < -6, sunlit(r, sun: sunDirection(t)) { current!.visible = true }
129 lock.unlock()
130 } 130 }
131 lastAz = h.azimuth 131 lastAz = h.azimuth
132 } else if let c = current { 132 } else if let c = current {
@@ -135,7 +135,12 @@ extension SkyEngine {
135 } 135 }
136 current = nil 136 current = nil
137 } 137 }
138 t = t.addingTimeInterval(step) 138 // Below the horizon altitude changes no faster than the satellite moves around Earth's centre relative to the
139 // observer: about 4°/min in low orbit, under 6.5°/min for any orbit above the surface. A 60 s step from below
140 // -8° skips 40 s (at most 4.3°) and a 180 s step from below -30° skips 160 s (at most 17.3°), so no 20 s sample
141 // at or above the horizon is skipped and the passes are those of plain 20 s steps.
142 i += h.altitude > -8 ? 1 : h.altitude > -30 ? 3 : 9
143 t = start.addingTimeInterval(Double(i) * 20)
139 } 144 }
140 return out 145 return out
141 } 146 }
Packages/MoonriseKit/Tests/SkyEngineTests/PassSearchTests.swift added +94
@@ -0,0 +1,94 @@
1import CAstronomy
2import Foundation
3import Testing
4@testable import SkyEngine
5
6/// The search `passes(of:)` replaced: a sample every 20 s whatever the altitude.
7private func everyTwentySeconds(_ elements: OrbitalElements, from start: Date, observer: Observer) -> [SatellitePass] {
8 guard let sgp = try? SGP4(elements) else { return [] }
9 var out: [SatellitePass] = []
10 var current: (rise: Date, riseAz: Double, peak: Date, maxAlt: Double, visible: Bool)?
11 var lastAz = 0.0
12 var t = start
13 while t <= start.addingTimeInterval(72 * 3600) {
14 guard let r = sgp.position(at: t) else { break }
15 let h = SkyEngine.lookAngles(teme: r, at: t, from: observer)
16 if h.altitude >= 0 {
17 if current == nil { current = (t, h.azimuth, t, h.altitude, false) }
18 if h.altitude > current!.maxAlt { current!.maxAlt = h.altitude; current!.peak = t }
19 if !current!.visible, h.altitude >= 10 {
20 var tt = SkyEngine.time(t)
21 let sun = Astronomy_Equator(BODY_SUN, &tt, observer.astro, EQUATOR_OF_DATE, ABERRATION)
22 let sunAlt = Astronomy_Horizon(&tt, observer.astro, sun.ra, sun.dec, REFRACTION_NORMAL).altitude
23 if sunAlt < -6, SkyEngine.sunlit(r, sun: SkyEngine.sunDirection(t)) { current!.visible = true }
24 }
25 lastAz = h.azimuth
26 } else if let c = current {
27 if c.maxAlt >= 10 {
28 out.append(SatellitePass(name: elements.OBJECT_NAME, rise: c.rise, set: t, peak: c.peak, maxAltitude: c.maxAlt, riseAzimuth: c.riseAz, setAzimuth: lastAz, visible: c.visible))
29 }
30 current = nil
31 }
32 t = t.addingTimeInterval(20)
33 }
34 return out
35}
36
37private struct LCG {
38 var state: UInt64
39 mutating func next() -> Double {
40 state = state &* 6364136223846793005 &+ 1442695040888963407
41 return Double(state >> 11) / Double(1 << 53)
42 }
43 mutating func range(_ a: Double, _ b: Double) -> Double { a + (b - a) * next() }
44}
45
46/// ISS, Tiangong and Hubble, 155 low-orbit objects at the visual group's usual inclinations, and a Starlink train with
47/// its second stage, all searched from 2026-10-20 18:00 UTC.
48private let fixtures: [OrbitalElements] = {
49 var out = [
50 OrbitalElements(name: "ISS (ZARYA)", norad: 25544, epoch: "2026-10-18T03:28:15.076416", meanMotion: 15.48918166, eccentricity: 0.000678,
51 inclination: 51.6328, raan: 51.6259, argPerigee: 277.7856, meanAnomaly: 82.2363, bstar: 0.00014456),
52 OrbitalElements(name: "CSS (TIANHE)", norad: 48274, epoch: "2026-10-18T05:11:02.000000", meanMotion: 15.60, eccentricity: 0.0005,
53 inclination: 41.47, raan: 120.0, argPerigee: 30.0, meanAnomaly: 200.0, bstar: 0.0002),
54 OrbitalElements(name: "HST", norad: 20580, epoch: "2026-10-18T09:00:00.000000", meanMotion: 15.28, eccentricity: 0.0002,
55 inclination: 28.47, raan: 300.0, argPerigee: 90.0, meanAnomaly: 10.0, bstar: 0.00002),
56 OrbitalElements(name: "Starlink train (Group 15-25)", norad: 66001, epoch: "2026-10-20T10:10:01.123456", meanMotion: 15.91, eccentricity: 0.0012,
57 inclination: 53.05, raan: 200.0, argPerigee: 270.0, meanAnomaly: 90.0, bstar: 0.0006),
58 OrbitalElements(name: "FALCON 9 R/B", norad: 66030, epoch: "2026-10-20T10:10:01.123456", meanMotion: 15.905, eccentricity: 0.0012,
59 inclination: 53.05, raan: 200.0, argPerigee: 270.0, meanAnomaly: 88.0, bstar: 0.0006),
60 ]
61 var r = LCG(state: 42)
62 let incs = [71.0, 74.0, 81.2, 82.5, 83.0, 98.2, 65.0, 56.0, 50.0]
63 for i in 0..<155 {
64 let k = Int(r.next() * Double(incs.count))
65 out.append(OrbitalElements(name: "OBJECT \(i)", norad: 10_000 + i, epoch: "2026-10-19T12:00:00.000000",
66 meanMotion: r.range(13.9, 15.3), eccentricity: r.range(0.0005, 0.02), inclination: incs[k] + r.range(-0.5, 0.5),
67 raan: r.range(0, 360), argPerigee: r.range(0, 360), meanAnomaly: r.range(0, 360), bstar: r.range(1e-5, 2e-4)))
68 }
69 return out
70}()
71
72@Test(arguments: [
73 ("equator", Observer(latitude: 0.18, longitude: -78.47)),
74 ("mid-latitude", chicago),
75 ("high latitude", Observer(latitude: 69.65, longitude: 18.96)),
76])
77func adaptiveStepsFindTheSamePasses(_ place: String, _ observer: Observer) throws {
78 let start = Date(timeIntervalSince1970: 1_792_519_200)
79 // Vanguard 1 (e 0.19) from its epoch, for an orbit whose angular rate varies.
80 let cases = fixtures.map { ($0, start) } + [(vanguard, try #require(vanguard.epochDate))]
81 var count = 0
82 for (elements, from) in cases {
83 let old = everyTwentySeconds(elements, from: from, observer: observer)
84 let new = SkyEngine.passes(of: elements, from: from, hours: 72, observer: observer)
85 try #require(new.count == old.count, "\(place), \(elements.OBJECT_NAME): \(new.count) passes, \(old.count) with 20 s steps")
86 for (a, b) in zip(new, old) {
87 #expect(abs(a.rise.timeIntervalSince(b.rise)) <= 1 && abs(a.peak.timeIntervalSince(b.peak)) <= 1 && abs(a.set.timeIntervalSince(b.set)) <= 1,
88 "\(place), \(elements.OBJECT_NAME): \(a.rise)–\(a.set), \(b.rise)–\(b.set) with 20 s steps")
89 #expect(a.visible == b.visible && abs(a.maxAltitude - b.maxAltitude) < 1e-6)
90 }
91 count += old.count
92 }
93 #expect(count > 1000, "\(count) passes")
94}