Skip to content

[Performance] Remove redundant image scheduler hop#80

Merged
NachoSoto merged 1 commit into
masterfrom
agent/remove-redundant-image-scheduler-hop
Jul 12, 2026
Merged

[Performance] Remove redundant image scheduler hop#80
NachoSoto merged 1 commit into
masterfrom
agent/remove-redundant-image-scheduler-hop

Conversation

@NachoSoto

Copy link
Copy Markdown
Owner

Summary

  • remove the second image-creation scheduler hop after UIKit and SwiftUI have already formed render data on that scheduler
  • keep renderer work on the injected image scheduler and final delivery on the UI scheduler
  • add focused coverage requiring one scheduling operation per request

Performance

Isolated AsyncImageView benchmark

20,000 unique size requests across 10 runs:

Baseline This PR Change
Scheduler operations 40,000 20,000 -50%
Mean 1,842.490 ms 1,690.561 ms -8.2%
Median 1,792.986 ms 1,681.549 ms -6.2%
Render-data / renderer calls 20,000 20,000 unchanged

WatchChess integration benchmark

Release-hosted WatchChess benchmark with 96 real PieceView requests and 96 repeated hits per sample, 10 strictly alternating samples per variant:

Baseline This PR Change
Scheduler operations 192 96 -50%
Cold median 122.068 ms 123.213 ms +0.9% (noise)
Warm median 5.308 ms 5.262 ms -0.9% (noise)

The paired cold delta was +0.831 ms (95% CI -1.515 to +3.177 ms); the paired warm delta was -0.097 ms (95% CI -0.631 to +0.436 ms). The app workload confirms the operation reduction, but not a measurable wall-time change.

Rendering and cache parity

Baseline and candidate matched UIKit and SwiftUI image/snapshot hashes, point and pixel sizes, data/size invalidation sequences, cold/repeated cache semantics, and persistent miss → hit → hit after renderer recreation behavior. Every renderer started on the requested image scheduler and every UI callback ran on the main thread.

The benchmark also reproduced a pre-existing disk-reload scale-metadata loss in both variants: raw pixels are preserved while 36 pt @2x reloads as 72 pt @1x. This PR does not change it.

Validation

  • swiftlint --config .swiftlint.yml --strict
  • full iOS simulator suite: 63 tests passed
  • WatchChess Release simulator build and 20/20 measured samples passed

@NachoSoto NachoSoto marked this pull request as ready for review July 11, 2026 18:41
NachoSoto added a commit that referenced this pull request Jul 12, 2026
## Summary

This branch combines the independently reviewed changes from:

- #77 — skip unchanged UIKit image sizes
- #78 — streamline SwiftUI geometry observation
- #79 — preserve `UIImage.scale` in the disk cache
- #80 — remove the redundant image-scheduler hop
- #81 — lazily initialize SwiftUI image state
- #82 — reduce multicast contention while preserving subscription
semantics

## Component benchmarks

| Change | WatchChess workload | Result |
|---|---|---:|
| UIKit unchanged-size guard (#77) | 128,000 origin-only mutations |
**-32.24% mean**, scheduler calls 128,000 → 0 |
| SwiftUI geometry (#78) | 100 real cells × 80 relayouts | **-2.86%
mean**, 95% CI for saving 9.053–45.532 ms |
| Scheduler hop (#80) | 96 cold + 96 repeated requests | scheduler
operations **-50%**; wall time neutral |
| Lazy SwiftUI state (#81) | 20,000 body reconstructions | **-4.44%
mean**, 12/12 paired wins |
| Multicast completed hits (#82) | 50,000 real `PieceView` hits |
**-89.3% / 9.33× faster** |
| Multicast distinct keys (#82) | 24 concurrent keys | neutral: -1.4%
mean, 95% CI spans zero |

## Combined WatchChess benchmark

Release WatchChess, iPhone 17 Pro Max / iOS 26.5 simulators, 10 samples
per variant and flow. Negative CPU/hitch changes are improvements.

### CPU

| Flow | CPU time, baseline → combined | Change (paired-bootstrap 95%
CI) | Mean one-core-equivalent CPU |
|---|---:|---:|---:|
| Tournament-list scrolling | 2.230 → 2.264 s | +1.54% (-2.79% to
+6.09%): neutral | 12.01% → 12.07% |
| Rounds scrolling | 1.059 → 1.024 s | **-3.25% (-5.55% to -1.27%)** |
13.03% → 12.79% |
| Open game | 0.452 → 0.439 s | **-2.89% (-5.36% to -0.12%)** | **13.47%
→ 13.11%** (-2.63%; CI -4.37% to -0.87%) |

### FPS and hitches

`FPS` below is a main-run-loop `CADisplayLink` callback proxy over the
exact UI-action window, not compositor presentation FPS.

| Flow | FPS, baseline → combined | FPS change (95% CI) | Hitches/min,
baseline → combined | Hitch change (95% CI) |
|---|---:|---:|---:|---:|
| Tournament-list scrolling | 58.93 → 59.00 | +0.12% (-0.20% to +0.48%):
neutral | 40.67 → 37.39 | -8.06% (-29.75% to +14.24%): neutral |
| Rounds scrolling | 58.53 → 58.98 | **+0.77% (+0.13% to +1.39%)** |
63.24 → 51.68 | -18.28% (-35.36% to +5.33%): trend, CI spans zero |
| Open game | 54.14 → 54.07 | -0.14% (-1.20% to +0.91%): neutral |
131.04 → 145.44 | +10.99% (-5.26% to +29.68%): neutral |

Missed-refresh rates were also neutral for tournaments (-3.62%, CI
-21.79% to +14.17%) and game (+2.99%, CI -5.93% to +13.06%). Rounds
trended lower (-26.69%, CI -47.25% to +4.29%).

## Methodology and controls

- two-pass, pass-outer crossover: pass 1 baseline on simulator A then
candidate on B; pass 2 candidate on A then baseline on B
- five measurements per pass, variant, and flow; 10 total per
variant/flow
- engine analysis forced off (`currentEngineStrength = 1`) and validated
inside every app launch/report
- exact 24-entry real API fixture in cache-only mode; any cache miss
failed the run
- identical 147-path warm AsyncImageView cache coverage per variant
- exact AsyncImageView path/hash/mtime validation before sample 1 and
after every five-sample block
- API path/size/hash validation before and after every block
- arithmetic-mean CPU headline; duration-weighted ratio-of-sums
FPS/hitch rates
- deterministic 100,000-resample, pass-stratified paired bootstrap
confidence intervals
- symmetric full cache hashing before each measured block; these are
deliberately warm-cache measurements
- hitches: clipped display-link interval at least 1.5× its target
refresh period

All earlier measurements with uncontrolled engine state or unequal cache
warmness were quarantined and excluded.

## Rendering and cache regression checks

- cold game and rounds screenshots were pixel-identical; cold tournament
app content was pixel-identical outside dynamic system chrome
- all four pass-1 seed/verify tournament screenshots were
pixel-identical across the 1320×2590 app region; the only difference was
5,708 pixels in the translucent system search-field blur
- six gated 8 FPS recordings covered tournament scrolling, rounds
scrolling, and opening a game for both variants
- manual contact-sheet review found no blank flashes, stale
substitutions, missing portraits, or persistent placeholders; both game
recordings showed the same normal transition placeholders and matching
fully rendered end states
- all six recordings passed pre/post AsyncImageView and API cache audits
- baseline and candidate kept identical 147 relative cache paths; each
variant's hashes and mtimes remained stable

The different cache bytes between variants are intentional: #79 stores
image scale in a versioned envelope. Legacy raw-PNG entries become one
normal miss and regenerate in the corrected format. Existing remote
images may therefore re-download once; offline users can temporarily
miss legacy cached images until connectivity returns.

## #79 and #82 semantics

#79 and the [#82 synchronous-subscriber review
finding](#82 (comment))
are different fixes, and both are included:

- #79 fixes PNG disk-cache scale metadata.
- #82 attaches the first subscriber before starting synchronous upstream
work, preserving first-request miss / repeat-request hit semantics.

#82's `lifetime.observeEnded` intentionally holds the wrapper and
in-flight entry only until producer completion or cancellation.
Weak-reference tests verify release after both paths, while cancellation
still disposes upstream work; no retain cycle or leak was observed.

## Validation

- `swiftlint --config .swiftlint.yml --strict`: 0 violations
- full iOS simulator suite: 69 XCTest/Quick + 13 Swift Testing tests =
**82 passed**
- focused multicast concurrency, synchronous, retry, memory-warning,
cancellation, and weak-lifetime coverage
- WatchChess Release cache preflights, 60 CPU samples, 60 frame reports,
and six visual recordings passed their evidence checks
- CI updated to macOS 26 / Xcode 26.5
@NachoSoto NachoSoto merged commit 78b53bc into master Jul 12, 2026
1 check passed
@NachoSoto NachoSoto deleted the agent/remove-redundant-image-scheduler-hop branch July 12, 2026 03:01
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