Skip to content

perf: push animated values from the shared slot block - #20

Merged
riteshshukla04 merged 5 commits into
margelo:mainfrom
SectionTN:perf/values-from-shared-block
Nov 4, 2026
Merged

riteshshukla04 merged 5 commits into
margelo:mainfrom
SectionTN:perf/values-from-shared-block

Conversation

@SectionTN

@SectionTN SectionTN commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

The Reanimated driver sent its animated values with setValues(number[]) on every frame, so each frame built a JS array that Nitro converted into a std::vector before the renderer swapped it in (the ROADMAP item about sending values through a shared ArrayBuffer). The driver already writes those values into the mirror block it keeps for the next commit, so the view now binds that block once and copies it natively on pushValues(). A Scene's mapper does the same for canvas targets. Offscreen surfaces and older native binaries keep the array.

Per push on a Pixel 9a emulator, median of 3 runs, in microseconds:

slots debug before debug after release before release after
16 6.0 1.8 3.3 1.0
256 45.8 7.0 28.5 6.1
1024 174.0 24.2 115.4 22.9
4096 697.2 93.3 438.0 88.5

A Scene attached to two canvases gains more, since its second target costs one native copy where main marshals the array again: 760.6 to 121.8 us per frame for 4096 slots in a release build.

Risk: the binding is native state that setValues never had. A recycled view drops it (on iOS that happens to a canvas hidden with display: none), so pushValues returns false and the mapper binds again. One harness case covers that, and another covers a value set that changes while the canvas animates.

Tested with bun test, tsc, eslint, prettier and clang-format, and with the imperative, imperativeScene, workletScene, rendering and viewport harness suites on a Pixel 9a emulator and an iOS 27.2 simulator.

setValues takes a number[], so every animated frame builds an array that
Nitro converts into a std::vector before the renderer swaps it in. A view
can now bind the Float64 block behind a TgfxSlots values buffer once
(bindValues) and copy it natively on each pushValues(), so only the call
crosses JSI per frame.

The renderer copies under the same lock as setValues, and clearScene
drops the binding. Each platform view keeps the bound ArrayBuffer until
the next bind, and the slots object now shares its value storage with the
buffers it hands out, so a bound canvas can outlive it.
@vercel

vercel Bot commented Nov 3, 2026

Copy link
Copy Markdown

@SectionTN is attempting to deploy a commit to the Margelo Team on Vercel.

A member of the Team first needs to authorize it.

The driver already writes every animated value into the mirror block for
the next commit's snapshot. It now binds that block to the view once per
mapper and calls pushValues() each frame instead of building a slot array
for setValues. A view without pushValues, or a canvas without a mirror,
still gets the array.

On a Pixel 9a emulator (debug build) a push of 256 values drops from
44 us to 7 us, and 4096 values from about 665 us to 87 us. A harness case
covers a value set that changes while the canvas animates.
A Scene attached to a canvas runs the same mapper as the driver but still
sent setValues(slots) to every target each frame. A target that has
pushValues now binds the mirror once and copies it natively, while an
offscreen surface or an older binary keeps getting the array, which the
mapper now fills only when some target reads it.

With 1024 slots on a Pixel 9a emulator (debug build) a frame for one
canvas drops from about 166 us to 33 us, and a second canvas adds about
1.5 us instead of another 124 us marshal.
On iOS a canvas hidden with display: none is unmounted and its view is
recycled, which clears the binding. When the canvas shows again it keeps
the same view, so the mapper still counts it as bound and every push is
dropped: the animation freezes until the value set changes. setValues had
no such state to lose.

pushValues now returns false while nothing is bound, and the driver and
the Scene mapper bind again and retry. bindValues also rejects a JS-owned
buffer, which would leave the renderer reading memory its runtime frees.
bindValues and pushValues serve the animation driver and the Scene mapper
and need a native-owned slot block that callers cannot create, so they are
@internal and drop out of the API reference. Comments that named
setValues as the per-frame push now say value push, and the roadmap entry
for sending values through a shared ArrayBuffer is marked done.
@SectionTN
SectionTN marked this pull request as ready for review November 3, 2026 20:17
@riteshshukla04
riteshshukla04 merged commit 8d38e3d into margelo:main Nov 4, 2026
4 of 6 checks passed
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.

2 participants