The Native Frame: Zero Re-renders, Fewer Allocations
On iOS and Android the stage is drawn by Skia from React Native. Day 2 finds the two native costs that every frame paid twice over: React re-rendering the whole stage tree 60 times a second, and the renderer allocating and replaying work that never changes.
Where native time goes
A React Native app has one JavaScript thread. It runs the game engine, the React tree, and it is also the thread that delivers touch events. Anything heavy on it does double damage: the frame is late, and the next touch waits behind it, so the dragged object trails the finger.
The native stage (ShapeActorStage.native.tsx) worked like this: each frame the engine ticked, the Skia renderer recorded the frame into a picture, and the component stored that picture with setPicture(picture). That one line looks harmless.
Finding it: a guard test, not a hunch
The suite's native rail is a Jest test that mounts the real stage, starts a drag, advances 60 frames and counts React renders of the stage tree.
before: 60 frames mid-drag -> 60 stage re-renders
after: 60 frames mid-drag -> 0 stage re-renders
Sixty re-renders a second means the scroll view, the toy buttons, the overlays and the navigation arrows were all re-rendered on the touch thread, every frame, to change one picture. The test was written first and run against the old file, where it failed with exactly 60. That makes it a real guard: it was red before the fix and green after, and it will go red again if anyone reintroduces per-frame state.
The fix: keep the frame out of React state
React state is for things that change what the tree looks like. A per-frame picture changes only what one canvas draws. The fix moves the picture into a small external store and lets only the canvas subscribe to it, through React's useSyncExternalStore:
// frameStore.tsx (simplified)
export function createStore<T>(initial: T) {
let value = initial;
const listeners = new Set<() => void>();
return {
get: () => value,
set(next: T) { value = next; for (const l of listeners) l(); },
subscribe(l: () => void) { listeners.add(l); return () => listeners.delete(l); },
};
}
function StageCanvas({ store }: { store: Store<SkPicture> }) {
const picture = useSyncExternalStore(store.subscribe, store.get);
return <Canvas><Picture picture={picture} /></Canvas>;
}
The engine publishes a new picture; the store notifies its one subscriber; the canvas re-renders; nothing else does. Overlays (a word card, a quest) got their own stores and re-render only while one is showing.
A second bug hid next to it. The stage's clock was armed after the onReady callback, so when the world-picker's preview tiles called stop() in onReady, the clock started anyway. Every preview tile was animating at 60 fps behind the picker. The fix arms the clock before onReady and adds controller.freeze(), which start() respects.
The renderer: stop rebuilding what never changes
The Skia renderer (SkiaStageRenderer) draws the scene in world units onto a Skia canvas. Profiling one recorded frame showed work that did not need to happen per frame:
| cost per frame | fix |
|---|---|
| a new parent→children part map for every actor | built once per blueprint, cached in a WeakMap |
a native call (getAlphaf) or a regex per paint, to read a colour's alpha | alpha cached per colour string |
| the spotlight "wash" rectangle drawn across the whole 10 000-unit world | clipped to the visible span |
| the backdrop (market: ~9 000 drawing ops) replayed whole | recorded as consecutive chunks, only chunks near the view replayed: 11 of 182 |
The last one needed care. A first design cut the backdrop into clipped 1 024-unit tiles, and its pixels differed from the original at the seams: anti-aliased edges were cut in half. It was rejected. The kept design records consecutive runs of ops in paint order, unclipped, each with the x-range it touches. Replaying the chunks that reach the view is then pixel-identical to replaying everything.

The parity check (npm run perf:parity) paints every op directly and paints the chunks, for four worlds and six camera positions each, and fails on a single differing channel. It reported 0.
Before and after side by side
The recording below is the web build of the same session (the native fixes are proven by the guard test and the Skia parity check; the cloud container has no phone simulator to record). Low-end class, 6× CPU, the playground world, the same touch script on both builds.

| measurement | before | after |
|---|---|---|
| native stage re-renders per 60 drag frames | 60 | 0 |
| picker preview tiles | ticking at 60 fps each | frozen |
| market backdrop chunks replayed per frame | 182 of 182 | 11 of 182 |
| low-end web, mid-drag | 18–21 fps | 25–43 fps |
Design principles at work
- Separation of concerns between model and view. The engine (the model) produces frames; React (the view's structure) should change only when the structure changes. Mixing a per-frame value into tree state coupled two lifecycles that run at different speeds.
- Publish/subscribe. The frame store is a tiny pub/sub channel: one publisher (the engine loop), one subscriber (the canvas). Adding a second consumer, an overlay, does not touch the publisher.
- Dependency injection. The renderer takes a structural
SkiaLikeinterface, not the Skia library itself: the app injects React Native Skia, the benchmark injects CanvasKit. The same drawing code is measured headless and shipped on devices. (Day 9 injects a third implementation.) - Caching immutable things only. Every cache added today holds something that cannot change for its key: a blueprint's part tree, a colour's alpha, a recorded chunk of a backdrop that never moves.
Key takeaways
- On React Native, a per-frame
setStateat the root re-renders everything on the thread that also delivers touches. Keep per-frame values in an external store. - Write the guard test first and prove it red on the old code: "60 → 0" is a claim anyone can rerun.
- Cache only what is immutable for its key, and check pixel parity whenever a renderer optimisation changes how something is drawn.
- A rejected design (clipped tiles) is a result too. Record it (Day 10).