ikieEngineeringGet started
All posts

iOSSwiftUIPerformanceMeasurement

A few milliseconds more, and the stall grew fivefold

Flicking the handle quickly between ikie's small and big calendars made the screen stutter. We tested four guesses by replaying the exact same gesture on builds from before and after a rework, and found that what blew up the stall was a threshold: one frame. Bringing each drag update down to 3 ms took the stall after release from around 130 ms to around 10 ms.

한국어

The calendar at the top of ikie's home screen folds in three stages: a week strip showing one week, a month calendar, and a big calendar with event bars on every day. You drag a handle underneath to move between them. Flick that handle back and forth quickly, though, and the screen stuttered. The step between the month calendar and the big calendar was the worst.

We had just reworked week-view paging, so the first suspect was that rework. This post records how we measured to answer that question, and where our guesses were right and wrong.

First, decide how to measure

Stutter investigations tend to start and end with a feeling. So before fixing anything, we built a measurement that could repeat the same gesture identically, again and again.

ItemDetail
WhereiPhone 17 Pro simulator (iOS 26.5), real account data (read only), a busy month
BuildsRelease. main just before the paging rework, and main after the rework, measured the same way
GestureAn automated UI test drags the handle week strip → month → big → month → week strip, 0.25 s apart, five rounds
RepeatsThree alternating runs per build, median taken. The first round is dropped because it locates the handle
ProbeCPU ms per main run-loop turn, late frames over 25 ms, and which SwiftUI views re-evaluated and how often per turn. Markers at touch-down, release and animation end
Function costsample (1 ms interval)

One small detail changed the numbers a lot. Automated UI tests normally find buttons by reading the app's accessibility tree, and that reading itself uses the main thread. The measuring tool was disturbing the very thing it measured. So we dragged the handle by screen coordinates from outside the app (SpringBoard), and the app's tree is never read during measurement.

Four guesses, written down first

We read the code and wrote down four suspects. Before looking at any numbers, we also wrote down "if this guess is right, what should re-evaluate, and how many times?" How many times a view re-evaluates makes a good verdict. Time wobbles with the device and its load, but counts show the structure as it is.

  • A. Every frame of the drag recomputes the whole calendar.
  • B. Starting and ending a transition rebuilds the month view from scratch.
  • C. Toggling the big calendar re-runs the whole home screen, timeline included.
  • D. A fixed 0.5-second timer used by menu toggles overlaps the next transition.

The numbers

Main-thread CPU ms, median of three runs. "One update while dragging" is the cost each time the finger moves. At 60Hz one frame is 16.7 ms.

TransitionBuildTouch downOne update while draggingLongest turn while draggingLate frames
Week strip → monthBefore rework4810.2197
Week strip → monthAfter rework5610.2758
Month → bigBefore rework12014.65928
Month → bigAfter rework14418.029628
Big → monthBefore rework9611.43918
Big → monthAfter rework16517.127426

Two things stood out. Between the month and big calendars, both builds had 18–28 late frames per transition. Almost every frame was late. It stuttered before the rework too. Yet in the post-rework build the longest turn went from 59 ms to 296 ms, five times longer, while a single update grew only 3.4 ms, from 14.6 to 18.

Cross the threshold and stalls pile up

This gap was the biggest lesson of the analysis. If one update fits in a frame (16.7 ms), each finger movement is handled once and the screen just lags a little. Once it exceeds a frame, the touches that queued up in between get handled together in the next run-loop turn. A cost increase of a few milliseconds becomes a stall of hundreds.

Whether cost went up and whether that cost crossed the one-frame threshold are separate questions. A few milliseconds turn into hundreds at the moment the threshold is crossed.

So the goal wasn't "a bit faster". It was "bring each drag update clearly under the threshold".

A verdict for each guess

A. Recomputing every frame of the drag: right. This is the largest share, and it predates the rework. Each finger movement re-evaluated the calendar body once and the big calendar's week rows once per row of the month (4.8 times). There were two reasons. The drag value was state on the calendar view, so every drag re-ran its entire body. And each row of the big calendar was handed a fresh "how unfolded" value, so its equality check (Equatable) failed every time. So grouping seven days of items and splitting bars into lines was redone every frame, even though line splitting and bar placement don't depend on how unfolded the calendar is.

B. Rebuilding at start and end: right. This also predates the rework. At touch-down it built two transition months at once (62 day cells, 15 big-calendar rows, 131 ms). At rest it threw the transition months away and rebuilt the expanded calendar, including the months on either side (153 day cells, 231 ms). The views used during the transition and the view at rest were different, so one got thrown away at the start and the reverse happened at the end.

C. The whole home screen and timeline re-run: wrong. Home reads the big-calendar state, so we expected every transition to re-run it. Counting showed home ran only twice, when the app opened, and the timeline's views ran zero times during transitions.

D. The 0.5-second timer: unknown so far. Dragging the handle doesn't go through that timer, so this gesture couldn't measure it. We'll build a separate gesture for menu toggles.

What turned up outside the guesses

Some cost remained that the four guesses didn't explain. Between the week strip and the month calendar, the longest turn grew from 19 ms to 75 ms. This share did come after the paging rework. As the calendar grows, the visible height of the timeline changes every frame. That value was saved every frame, and after the rework all five pages of the UIKit pager read it.

A "turn one thing off" experiment settled it. We stopped writing that value only while folding and unfolding. The longest turn dropped from 75 ms to 17 ms, and late frames from 8 to 4. Switching off one suspicious line and measuring again was faster than staring at profiler graphs.

But when we measured again to merge that fix (#712), main before the fix was already at 17 ms. Two PRs merged in between had taken the same share first. One pinned incoming page views to the top of the board's height (#707). The other moved the rotating gradient border around blocks (the glow) to a Core Animation layer (#708). The work of re-placing page views and redrawing the border on every height change went away there. What #712 still bought was 0.8 ms per update and one late frame. The every-frame write was still there, so we removed it structurally anyway.

The measuring tool inflated the numbers

At first we couldn't pin down the extra 3–6 ms between the month and big calendars. sample showed the main thread's screen commits (Core Animation commits) rising from 165 samples before the rework to 1,884, most of them SwiftUI updates running inside the commit.

Before fixing A and B, we changed the measurement once more. Instead of driving the handle with an automated UI test (XCUITest), we built a loop inside the app that calls the handle's drag and release directly. It drags to the next stage at 250 pt/s and lifts a finger that has stopped. Measured this way, the 3–6 ms didn't appear. One update took 12.6 ms before the rework and 11.2 ms after.

When XCUITest drives an app, the app's accessibility tree is switched on. Dragging by coordinates from outside the app kept us from reading the tree, but the app still kept that tree up to date. The extra share most likely comes from accessibility updates for the pager's five pages that the rework added. That's still a guess. It's a cost paid only by people with the tree on, as with VoiceOver, so we left it to confirm on a real device.

The measuring tool is part of what you measure. When a number moves, first separate whether the app changed or the way you measured did.

Fixing A and B

We fixed both correct guesses in one PR (#726).

  • A, recomputing every frame of the drag. The drag value moved out of the calendar body's state into a small box that only the cell drawing the stage reads. Big-calendar rows compute line splitting and bar placement once, independent of how unfolded they are, and the unfold amount is used only for drawing, such as height and opacity.
  • B, rebuilding at start and end. The expanded calendar is always kept built in its own hosting (UIHostingController), separate from the home screen, and the growing month is built ahead only while the hand rests for 0.3 seconds. Throwing away and rebuilding panels at the start and end of a transition is gone.

We compared three builds with the same in-app loop. Real account, Release, median of three runs per build.

Before reworkAfter reworkFixed
See the numbers
Settle after releaseBefore reworkAfter reworkFixed
Month → big146 ms117 ms11 ms
Big → month131 ms132 ms8 ms
Week strip → month121 ms124 ms12 ms
Month → week strip13 ms47 ms57 ms
Main-thread CPU time of the turn that settles after release. Month → week strip still carries a share that appeared after the rework.
TransitionBuildTouch downOne update while draggingLongest turn while draggingLate framesMain thread busy
Month → bigBefore rework6812.651699%
Month → bigAfter rework6711.242696%
Month → bigFixed303.022438%
Big → monthBefore rework6912.252996%
Big → monthAfter rework7111.135793%
Big → monthFixed302.938336%
Week strip → monthAfter rework333.810443%
Week strip → monthFixed153.012334%

Between the month and big calendars, the main thread went from nearly fully busy while dragging (93–99%) to about a third. One update fell from 11–12 ms to 3 ms, under a fifth of a frame. That meets the goal of being clearly under the threshold. The stall after release went from 117–146 ms to 8–11 ms, and touch down more than halved.

Folding from the month calendar to the week strip still settles in 57 ms. That's the calendar body running five times in the release turn. It wasn't there before the rework (13 ms), and this fix didn't make it worse.

What the speed table missed

A and B cut the time a lot, but the new structure also created new work.

  • The hidden calendar re-ran while swiping folded (#744). With the expanded calendar always built, every day or week swipe with the calendar folded recomputed the whole hidden panel (71 and 82 times over 10 swipes). While hidden, it now passes a new value only when a key such as the visible month or stage changes, which brought it to zero.
  • Picking a day in another month rebuilt the hidden panel all at once. The hidden panel now catches up while the hand rests (that turn 87 → 26 ms, #784), builds the new month one row at a time (64 → 31 ms and 66 → 21 ms, #785), and removes the old month's rows one at a time too (#840).
  • The big calendar showed the wrong month. The separate panel centered all five months, but the accessibility positions XCUITest reads were correct, so the screen tests passed. A test that measures the real layout now guards it (1,278 pt off before the fix, 0 after).

So besides speed, we now measure "is everything in the right place every frame". Recording 40 person-like gestures (fast, repeated, reversed) frame by frame, row heights that jumped more than 40 pt in one frame went from 35 before the rework and 31 after it down to 15, and frames drawn with stale values after the end went from 15 and 10 down to 0.

  1. 1Don't save the timeline's visible height while folding; save it once after settling (#712)
  2. 2Only the stage cell reads the drag value; big-row layout is computed once, apart from the unfold amount (#726)
  3. 3Keep the expanded calendar built separately; build the growing month only while the hand rests (#726)
  4. 4Keep the hidden panel from following swipes, and catch up a row at a time (#744 #784 #785 #840)
  5. 5Confirm the accessibility share (3–6 ms) on a real device
  6. 6Measure menu toggles separately to check the 0.5-second timer

What's left

  • No real-device numbers yet. Every number in this post comes from the Mac simulator. We'll add numbers once we measure a real iPhone with the SwiftUI instrument.
  • The 0.5-second timer (D) is still unmeasured. A menu transition re-opening after release happened zero times in all three builds, but toggling from the menu needs its own measurement.
  • Month → week strip still settles in 57 ms.

What we learned

Most of fixing the stutter was deciding where to fix. Three things set that order: a measurement that repeats the same gesture, verdicts by counts instead of time, and experiments that switch off one suspicious line and measure again.

  • Watch the threshold. A few milliseconds under one frame and a few over it are completely different experiences for the user. So we brought each update well under the threshold (3 ms), not just under it.
  • Suspect the measuring tool. The 3–6 ms measured with the accessibility tree on vanished when we changed how we measured.
  • Speed alone isn't enough. In PRs where the speed table improved, the hidden panel re-ran and the big calendar showed the wrong month. Tests that check positions every frame filled that gap.

How we learned the same lesson on the paging side is in the week view paging post.

The ikie teamikie is a calendar for your events and to-dos in one place. Here we write about the problems we run into while building it, and how we solve them.