diff --git a/ROADMAP.md b/ROADMAP.md index 09738a7..4be9f8a 100644 --- a/ROADMAP.md +++ b/ROADMAP.md @@ -184,10 +184,11 @@ Reducing per-op cost in tgfx's GL backend is where the remaining gap to - 🟠 Re-subscribe the `useAnimatedReaction` mapper only when the value *set* changes (currently re-subscribes on every commit due to new array identity). -- ✅ Values travel through a shared ArrayBuffer (#20): the `` driver and a - `Scene`'s mapper bind their mirror block once and copy it natively each frame, - so no `number[]` crosses JSI per frame. Still open: JS-thread `handle.set()` - writes and offscreen surfaces send `setValues(number[])`. +- ✅ Values travel through a shared ArrayBuffer (#20, #24): the `` driver + and a `Scene`'s mapper bind their mirror block once and copy it natively each + frame, and a JS-thread `handle.set()` writes the same block before it pushes, + so no `number[]` crosses JSI. Still open: offscreen surfaces send + `setValues(number[])`. - 🟡 Examples + tests for gesture-driven (gesture-handler is installed but unused), `withDecay`, `useDerivedValue`, `useClock`. - 🟡 Document that animated `rotate`/`skew` expect radians (`'45deg'` strings are diff --git a/packages/react-native-tgfx/ios/HybridTgfxView.swift b/packages/react-native-tgfx/ios/HybridTgfxView.swift index 53e43ce..525b939 100644 --- a/packages/react-native-tgfx/ios/HybridTgfxView.swift +++ b/packages/react-native-tgfx/ios/HybridTgfxView.swift @@ -6,6 +6,8 @@ final class HybridTgfxView: HybridTgfxViewSpec, RecyclableView { private let metalView = TgfxMetalView() // The renderer reads these bytes on every push, so they are released only by the next bind. private var boundValues: ArrayBuffer? + // A JS-thread push and the UI-thread driver both bind, so the pointer and its buffer change together. + private let bindLock = NSLock() var view: UIView { metalView } @@ -54,6 +56,8 @@ final class HybridTgfxView: HybridTgfxViewSpec, RecyclableView { throw RuntimeError.error(withMessage: "[tgfx] bindValues needs a native-owned buffer (a TgfxSlots values block)") } let slots = UnsafeRawPointer(values.data).assumingMemoryBound(to: Double.self) + bindLock.lock() + defer { bindLock.unlock() } metalView.bindValues(slots, count: Int32(values.size / MemoryLayout.size)) boundValues = values } diff --git a/packages/react-native-tgfx/src/__tests__/mirror.test.ts b/packages/react-native-tgfx/src/__tests__/mirror.test.ts index 9d3f500..3047e51 100644 --- a/packages/react-native-tgfx/src/__tests__/mirror.test.ts +++ b/packages/react-native-tgfx/src/__tests__/mirror.test.ts @@ -122,3 +122,139 @@ test('with a driver to follow, an unseen mutable is deferred and the program fla expect(pushed).toEqual([[7]]) expect(x.reads()).toBe(0) }) + +/** What a handle holds without Reanimated: a plain slot, read fresh on every push. */ +const plainSlot = (value: number) => ({ + value, + _isReanimatedSharedValue: true as const, +}) + +/** A mirror that logs its stores by index, to tell a write-back from an entry left alone. */ +function loggedBlock(values: number[], written: boolean) { + const b = block(values, written) + const stores: number[] = [] + b.values = new Proxy(b.values, { + set(target, key, value: number) { + stores.push(Number(key)) + target[Number(key)] = value + return true + }, + }) + return { b, stores } +} + +const rectAt = (x: unknown, y: unknown = 0): Node[] => + [ + { type: NodeType.Rect, props: { x, y, width: 1, height: 1 }, children: [] }, + ] as Node[] + +test('a JS-thread push writes its slots into the mirror and hands over the block', () => { + const slot = plainSlot(3) + const arrays: number[][] = [] + const container = new TgfxContainer(() => {}) + container.onValues = (values) => arrays.push(values.slice()) + container.setRootDirect(rectAt(slot)) + const mirror = block([0], false) + container.mirror = { values: [slot], block: mirror } + const offered: FlagBlock[] = [] + container.onBlock = (b) => { + offered.push(b) + return true + } + slot.value = 8 + container.pushValues() + expect(offered).toHaveLength(1) + expect(offered[0]).toBe(mirror) + expect(mirror.values[0]).toBe(8) + expect(arrays).toEqual([]) +}) + +test('a JS-thread push leaves a mutable the driver wrote alone', () => { + const x = shared(5) + const slot = plainSlot(3) + const container = new TgfxContainer(() => {}) + container.setRootDirect(rectAt(x.sv, slot)) + const { b, stores } = loggedBlock([7, 0], true) + container.mirror = { values: [x.sv, slot], block: b } + container.onBlock = () => true + const before = x.reads() + container.pushValues() + expect(stores).toEqual([1]) + expect([b.values[0], b.values[1]]).toEqual([7, 3]) + expect(x.reads()).toBe(before) +}) + +test('before the driver writes the mirror, a JS-thread push stores the literal the program carries', () => { + const x = shared(5) + const container = new TgfxContainer(() => {}) + container.setRootDirect(rectAt(x.sv)) + const mirror = block([0], false) + container.mirror = { values: [x.sv], block: mirror } + container.onBlock = () => true + container.pushValues() + expect(mirror.values[0]).toBe(5) + expect(x.reads()).toBe(1) +}) + +test('a target that cannot take the block gets the array instead', () => { + const slot = plainSlot(3) + const arrays: number[][] = [] + const container = new TgfxContainer(() => {}) + container.onValues = (values) => arrays.push(values.slice()) + container.setRootDirect(rectAt(slot)) + container.mirror = { values: [slot], block: block([0], false) } + let offered = 0 + container.onBlock = () => { + offered++ + return false + } + container.pushValues() + expect(offered).toBe(1) + expect(arrays).toEqual([[3]]) +}) + +test('without a mirror block a JS-thread push is an array', () => { + const slot = plainSlot(3) + const arrays: number[][] = [] + const container = new TgfxContainer(() => {}) + container.onValues = (values) => arrays.push(values.slice()) + container.onBlock = () => { + throw new Error('no block to hand over') + } + container.setRootDirect(rectAt(slot)) + expect(container.mirror).toBeNull() + container.pushValues() + expect(arrays).toEqual([[3]]) +}) + +/** A handle's block slot: an accessor over shared bytes, plus the boxed block a reader can view itself. */ +function blockSlot(value: number) { + const values = new ArrayBuffer(16) + const bytes = new Float64Array(values) + bytes[1] = value + let reads = 0 + const slot = { + _isReanimatedSharedValue: true as const, + __tgfxBoxed: { unbox: () => ({ values }) }, + __tgfxAt: 1, + get value(): number { + reads++ + return bytes[1]! + }, + } + return { slot, bytes, reads: () => reads } +} + +test('a JS-thread push reads a block slot from its block, not through its accessor', () => { + const b = blockSlot(4) + const container = new TgfxContainer(() => {}) + container.setRootDirect(rectAt(b.slot)) + const mirror = block([0], false) + container.mirror = { values: [b.slot], block: mirror } + container.onBlock = () => true + b.bytes[1] = 9 + const before = b.reads() + container.pushValues() + expect(mirror.values[0]).toBe(9) + expect(b.reads()).toBe(before) +}) diff --git a/packages/react-native-tgfx/src/__tests__/scene.test.ts b/packages/react-native-tgfx/src/__tests__/scene.test.ts index 3145c0f..1989dd2 100644 --- a/packages/react-native-tgfx/src/__tests__/scene.test.ts +++ b/packages/react-native-tgfx/src/__tests__/scene.test.ts @@ -201,11 +201,13 @@ function mirrorScene(scene: Scene, count: number): ArrayBuffer { function canvasTarget() { let bound: ArrayBuffer | null = null const received: number[][] = [] + const binds: ArrayBuffer[] = [] const t: SceneTarget = { setScene: () => {}, setValues: () => {}, bindValues: (buffer) => { bound = buffer + binds.push(buffer) }, pushValues: () => { if (bound == null) { @@ -215,7 +217,7 @@ function canvasTarget() { return true }, } - return { t, received, recycle: () => (bound = null) } + return { t, received, binds, recycle: () => (bound = null) } } /** A handle scene on a surface (no push) and a canvas, its mapper running on the mirror. */ @@ -231,7 +233,14 @@ function mirroredHandleScene() { // Attaching recompiles with the same slots, so the mirror stays and the mapper restarts. scene.attach(canvas.t) expect(mappers.size).toBe(1) - return { box, surface, canvas, values, run: [...mappers.values()][0]! } + return { + scene, + box, + surface, + canvas, + values, + run: [...mappers.values()][0]!, + } } test('a mirrored mapper has a pushing target copy the block; the rest get the array', () => { @@ -251,14 +260,52 @@ test('a mirrored mapper has a pushing target copy the block; the rest get the ar }) test('a target that lost its binding to a recycle is bound again on the next frame', () => { - const { box, canvas, run } = mirroredHandleScene() + const { canvas, run } = mirroredHandleScene() run() canvas.recycle() - box.set({ x: 9 }) run() expect(canvas.received).toEqual([ [1, 2, 3, 4], + [1, 2, 3, 4], + ]) +}) + +test('a JS-thread write has a pushing target copy the block; the rest get the array', () => { + const { box, surface, canvas, values } = mirroredHandleScene() + const before = surface.calls.values.length + box.set({ x: 9 }) + expect(canvas.received).toEqual([[9, 2, 3, 4]]) + expect(surface.calls.values.slice(before)).toEqual([[9, 2, 3, 4]]) + expect(Array.from(new Float64Array(values))).toEqual([9, 2, 3, 4]) +}) + +test('a JS-thread write binds a target once, and again after a recycle', () => { + const { box, canvas, values } = mirroredHandleScene() + box.set({ x: 9 }) + box.set({ x: 10 }) + canvas.recycle() + box.set({ x: 11 }) + expect(canvas.binds).toHaveLength(2) + expect(canvas.binds[0]).toBe(values) + expect(canvas.binds[1]).toBe(values) + expect(canvas.received).toEqual([ + [9, 2, 3, 4], + [10, 2, 3, 4], + [11, 2, 3, 4], + ]) +}) + +test('a JS-thread write after the mirror is replaced binds the new block', () => { + const { scene, box, canvas, values } = mirroredHandleScene() + box.set({ x: 9 }) + const next = mirrorScene(scene, 4) + box.set({ x: 10 }) + expect(canvas.binds).toHaveLength(2) + expect(canvas.binds[0]).toBe(values) + expect(canvas.binds[1]).toBe(next) + expect(canvas.received).toEqual([ [9, 2, 3, 4], + [10, 2, 3, 4], ]) }) diff --git a/packages/react-native-tgfx/src/components/Canvas.tsx b/packages/react-native-tgfx/src/components/Canvas.tsx index 2d334ef..e043373 100644 --- a/packages/react-native-tgfx/src/components/Canvas.tsx +++ b/packages/react-native-tgfx/src/components/Canvas.tsx @@ -17,7 +17,11 @@ import { Fill } from './drawings' import { AnimationDriver } from '../reanimated/AnimationDriver' import { ViewportDriver } from '../reanimated/ViewportDriver' import { isLive } from '../reanimated/liveValue' -import type { BoxedSlots } from '../reanimated/slotFlags' +import { + pushBlock, + type BoxedSlots, + type FlagBlock, +} from '../reanimated/slotFlags' import { TgfxRoot } from '../reconciler/Reconciler' import { directNodes } from '../reconciler/directNodes' import { createNode, NodeType } from '../reconciler/types' @@ -223,6 +227,8 @@ export function Canvas({ const [view, setView] = useState(null) const rootRef = useRef(null) const viewRef = useRef(null) + // The mirror block a JS-thread push last bound the view to. + const boundBlock = useRef(null) const scene = useRef<{ buffer: ArrayBuffer strings: string[] @@ -239,6 +245,7 @@ export function Canvas({ if (hybridRef.current == null) { hybridRef.current = callback((ref: TgfxViewRef) => { viewRef.current = ref + boundBlock.current = null // Optional: a JS bundle newer than the native binary would otherwise throw here. ref.setColorSpace?.(colorSpaceRef.current) // Unconditional: a recycled view resets its viewport in clearScene(), but @@ -345,6 +352,14 @@ export function Canvas({ rootRef.current.container.deferReads = AnimationDriver != null rootRef.current.container.onValues = (vals) => viewRef.current?.setValues(vals) + rootRef.current.container.onBlock = (block) => { + const view = viewRef.current + if (view == null) { + return true + } + boundBlock.current = pushBlock(view, block, boundBlock.current) + return boundBlock.current != null + } rootRef.current.container.onStrings = (indices, vals) => viewRef.current?.setStringValues?.(indices, vals) } diff --git a/packages/react-native-tgfx/src/reanimated/liveValue.ts b/packages/react-native-tgfx/src/reanimated/liveValue.ts index 13b05d6..7e66eda 100644 --- a/packages/react-native-tgfx/src/reanimated/liveValue.ts +++ b/packages/react-native-tgfx/src/reanimated/liveValue.ts @@ -109,6 +109,19 @@ export function readLiveNumber(v: MaybeSharedValue): number { return Number(v.value) } +/** A block slot's shared bytes and its index in them, for a reader that walks many slots; null for anything else. */ +export function blockSource( + v: MaybeSharedValue +): { view: Float64Array; at: number } | null { + 'worklet' + const at = (v as Partial).__tgfxAt + const boxed = (v as Partial).__tgfxBoxed + if (at === undefined || boxed === undefined) { + return null + } + return { view: valuesOf(boxed), at } +} + /** Write a live number wherever it is; worklet-side block writes land in the shared bytes. */ export function writeLiveNumber(v: MaybeSharedValue, x: number): void { 'worklet' diff --git a/packages/react-native-tgfx/src/reanimated/slotFlags.ts b/packages/react-native-tgfx/src/reanimated/slotFlags.ts index 91fd793..ac526c2 100644 --- a/packages/react-native-tgfx/src/reanimated/slotFlags.ts +++ b/packages/react-native-tgfx/src/reanimated/slotFlags.ts @@ -64,6 +64,32 @@ export function makeFlagBlock( } } +/** A view or surface that can copy a value block natively instead of taking an array. */ +export interface BlockTarget { + bindValues?(values: ArrayBuffer): void + pushValues?(): boolean +} + +/** Has `target` copy `block`, binding it when it holds another or a recycle dropped it; null if it cannot. */ +export function pushBlock( + target: BlockTarget, + block: FlagBlock, + bound: FlagBlock | null +): FlagBlock | null { + if (target.pushValues == null || target.bindValues == null) { + return null + } + const buffer = block.values.buffer as ArrayBuffer + if (bound !== block) { + target.bindValues(buffer) + } + if (target.pushValues() === false) { + target.bindValues(buffer) + target.pushValues() + } + return block +} + /** This runtime's view of the block, cached on its global: unboxing costs a JSI call. */ export function flagsOf(boxed: BoxedSlots): Uint8Array { 'worklet' diff --git a/packages/react-native-tgfx/src/reconciler/Container.ts b/packages/react-native-tgfx/src/reconciler/Container.ts index 5400746..f657982 100644 --- a/packages/react-native-tgfx/src/reconciler/Container.ts +++ b/packages/react-native-tgfx/src/reconciler/Container.ts @@ -5,6 +5,7 @@ import { compileScene } from '../compiler/compile' import type { MaybeSharedValue } from '../compiler/values' import { SceneWriter } from '../compiler/writer' import { nodeInternals, type NodeOwner } from '../imperative' +import { blockSource } from '../reanimated/liveValue' import { makeFlagBlock, type FlagBlock } from '../reanimated/slotFlags' import type { Node } from './types' @@ -54,16 +55,18 @@ export class TgfxContainer implements NodeOwner { claimRefs = true onValues: ((values: number[]) => void) | null = null onStrings: ((indices: number[], values: string[]) => void) | null = null + /** A JS-thread push already in the mirror block, for targets that copy it natively; false sends the array. */ + onBlock: ((block: FlagBlock) => boolean) | null = null /** - * The slots as the UI-thread driver last pushed them, in shared memory. + * The slots as last pushed, from either thread, in shared memory. * * A commit snapshots every SharedValue prop into the program, and on * Reanimated 4 a JS-thread read of a value the UI thread has written since * is a synchronous hop to that runtime, queued behind whatever frame it is * in - N hops per commit, minutes at a few thousand animating slots. The * driver mirrors what it pushes here, so the snapshot is a plain load. Flag - * byte 0 marks the mirror written. Block slots are never taken from it: a - * JS-thread write to one fires no driver, so the mirror can trail it. + * byte 0 marks the driver's first write. Block slots are never taken from + * it: their own block is always current, and the mirror can trail it. */ mirror: { values: MaybeSharedValue[]; block: FlagBlock } | null = null @@ -93,6 +96,12 @@ export class TgfxContainer implements NodeOwner { private stringValues: MaybeSharedValue[] = [] private readonly sentStrings: (string | null)[] = [] private readonly scratch: number[] = [] + /// Per slot, the handle block a push loads it from, or null to read `.value`. + private views: (Float64Array | null)[] = [] + /// Per slot, its index in `views[i]`. + private viewAt = new Int32Array(0) + /// Per slot, 1 for a Reanimated mutable. + private mutable = new Uint8Array(0) constructor(onCommit: CommitListener) { this.onCommit = onCommit @@ -127,6 +136,7 @@ export class TgfxContainer implements NodeOwner { } } this.values = values + this.indexSlots() this.stringValues = stringValues // The program carries current literals, so nothing is pending for the new table. this.sentStrings.length = stringValues.length @@ -168,21 +178,82 @@ export class TgfxContainer implements NodeOwner { // Nothing to patch, and an empty push still costs the render thread a redraw on Android. return } - if (this.unresolved && this.mirror?.block.bytes[0] !== 1) { + const block = this.mirror?.block ?? null + const written = block?.bytes[0] === 1 + if (this.unresolved && !written) { // The driver's first push carries every slot, block slots included. return } + if (block != null && this.onBlock != null) { + const into = block.values + const views = this.views + const at = this.viewAt + for (let i = 0; i < this.values.length; i++) { + const view = views[i] + if (view != null) { + into[i] = view[at[i]!]! + continue + } + // The driver's own entry: a store here could land over a newer write. + if (written && this.mutable[i] === 1 && Number.isFinite(into[i]!)) { + continue + } + into[i] = this.slotValue(i, block, written) + } + if (this.onBlock(block)) { + return + } + } const out = this.scratch - const known = this.knownValues() out.length = this.values.length for (let i = 0; i < this.values.length; i++) { - const sv = this.values[i]! - const k = known?.get(sv as object) - out[i] = k !== undefined ? k : Number(sv.value) + out[i] = this.slotValue(i, block, written) } this.onValues?.(out) } + /// Slot `i` as a push sends it: a mutable as the driver mirrored it or the program carries it, else read now. + private slotValue( + i: number, + block: FlagBlock | null, + written: boolean + ): number { + const view = this.views[i] + if (view != null) { + return view[this.viewAt[i]!]! + } + const sv = this.values[i]! + if (this.mutable[i] === 1) { + if (written) { + const mirrored = block!.values[i]! + if (Number.isFinite(mirrored)) { + return mirrored + } + } else { + const literal = this.lastLiterals?.get(sv as object) + if (literal !== undefined) { + return literal + } + } + } + return Number(sv.value) + } + + /// Per commit, where each slot is read on a push: a typed load from its handle block beats a getter call per slot. + private indexSlots(): void { + const n = this.values.length + this.views = new Array(n) + this.viewAt = new Int32Array(n) + this.mutable = new Uint8Array(n) + for (let i = 0; i < n; i++) { + const sv = this.values[i]! + const source = blockSource(sv) + this.views[i] = source?.view ?? null + this.viewAt[i] = source?.at ?? 0 + this.mutable[i] = isMutable(sv) ? 1 : 0 + } + } + private knownValues(): Map | null { const mirror = this.mirror if (mirror == null || mirror.block.bytes[0] !== 1) { diff --git a/packages/react-native-tgfx/src/scene.ts b/packages/react-native-tgfx/src/scene.ts index ee3998c..1fff4c9 100644 --- a/packages/react-native-tgfx/src/scene.ts +++ b/packages/react-native-tgfx/src/scene.ts @@ -12,7 +12,12 @@ import { readLiveString, } from './reanimated/liveValue' import { reanimated } from './reanimated/peer' -import { flagsOf, valuesOf } from './reanimated/slotFlags' +import { + flagsOf, + pushBlock, + valuesOf, + type FlagBlock, +} from './reanimated/slotFlags' import { TgfxContainer } from './reconciler/Container' import type { SceneNode } from './elements' import type { TgfxImage as TgfxImageHandle } from './specs/TgfxImage.nitro' @@ -60,6 +65,8 @@ interface Attachment { /// The string table this target holds, for delta encoding. Per target: a /// mispacked delta misindexes every string after entry 0. sent: string[] + /// The mirror block a JS-thread push last bound this target to. + bound: FlagBlock | null } /** @@ -87,6 +94,8 @@ export class Scene { private quiet = false private lastValues: MaybeSharedValue[] = [] private lastStringValues: MaybeSharedValue[] = [] + /// Reused per JS-thread push, for a target that cannot copy the block. + private readonly slots: number[] = [] private readonly container = new TgfxContainer( (buffer, strings, values, stringValues) => { @@ -111,6 +120,17 @@ export class Scene { t.target.setValues(values) } } + this.container.onBlock = (block) => { + let slots: number[] | null = null + for (const t of this.targets) { + t.bound = pushBlock(t.target, block, t.bound) + if (t.bound == null) { + slots ??= this.slotsFrom(block) + t.target.setValues(slots) + } + } + return true + } this.container.onStrings = (indices, values) => { for (const t of this.targets) { t.target.setStringValues?.(indices, values) @@ -189,7 +209,7 @@ export class Scene { if (this.disposed) { throw new Error('[tgfx] Scene.attach: this scene is disposed') } - const entry: Attachment = { target, sent: [] } + const entry: Attachment = { target, sent: [], bound: null } this.targets.push(entry) if (this.dirty) { this.flush() @@ -464,6 +484,15 @@ export class Scene { }, listenable) } + private slotsFrom(block: FlagBlock): number[] { + const out = this.slots + out.length = block.values.length + for (let i = 0; i < out.length; i++) { + out[i] = block.values[i]! + } + return out + } + private markDirty(): void { if (this.disposed || this.dirty) { return