Skip to content

A fast 2D and 3D game engine — TypeScript SDK, C++/WebAssembly core, visual editor. Ship one project to web, desktop, WeChat MiniGames, playable ads, and native Android / iOS.

perf(sdk): stop StateVisualsApplySystem write amplification at rest

esenginea096bae230cb
diff --git a/sdk/src/ui/behavior/systems.ts b/sdk/src/ui/behavior/systems.ts
index 029f0c2d7..eba475575 100644
--- a/sdk/src/ui/behavior/systems.ts
+++ b/sdk/src/ui/behavior/systems.ts
@@ -112,6 +112,8 @@ interface VisualTransitionState {
readonly startColor: Color;
/** Snapshot of the target's uniform scale at the moment the transition started. */
readonly startScale: number;
+ /** Settled at `toSlot`: visuals are written, so skip all work until the slot changes. */
+ done: boolean;
}
function lerp(a: number, b: number, t: number): number {
@@ -174,8 +176,13 @@ export function createStateVisualsApplySystem(world: World): SystemDef {
const startScale = (flags & TransitionFlag.Scale) && world.has(target, Transform)
? (world.get(target, Transform) as TransformData).scale.x
: targetScale;
- tx = { toSlot: slot, elapsed: 0, startColor, startScale };
+ tx = { toSlot: slot, elapsed: 0, startColor, startScale, done: false };
transitions.set(e, tx);
+ } else if (tx.done) {
+ // Already settled at this slot: the visuals hold their target
+ // values, so skip the per-frame get/insert wasm round-trips until
+ // the state (and thus the slot) actually changes.
+ continue;
} else {
tx.elapsed += dt;
}
@@ -203,7 +210,7 @@ export function createStateVisualsApplySystem(world: World): SystemDef {
}
if (t >= 1) {
- transitions.delete(e);
+ tx.done = true;
}
}
diff --git a/sdk/tests/ui-state-visuals-fade.test.ts b/sdk/tests/ui-state-visuals-fade.test.ts
index d8d91a3e7..5124bccc4 100644
--- a/sdk/tests/ui-state-visuals-fade.test.ts
+++ b/sdk/tests/ui-state-visuals-fade.test.ts
@@ -114,6 +114,24 @@ describe('StateVisualsApplySystem — color fade', () => {
expect(r.color).toEqual({ r: 0, g: 0, b: 1, a: 1 });
});
+ it('stops writing once settled at a slot (no per-frame write amplification)', () => {
+ world.insert(entity, StateMachine, { current: 'pressed', previous: '' } as StateMachineData);
+ world.insert(entity, StateVisuals, makeVisuals({
+ transitionFlags: TransitionFlag.ColorTint,
+ fadeDuration: 0,
+ states: [visualState('pressed', { r: 0, g: 0, b: 1, a: 1 })],
+ }));
+
+ const sys = createStateVisualsApplySystem(world as never);
+ runSystem(sys, 1 / 60); // slot change → apply + settle
+ const insertSpy = vi.spyOn(world, 'insert');
+ runSystem(sys, 1 / 60); // idle
+ runSystem(sys, 1 / 60); // idle
+
+ const visualWrites = insertSpy.mock.calls.filter((c) => c[1] === UIVisual);
+ expect(visualWrites).toHaveLength(0);
+ });
+
it('interpolates color halfway at t = duration/2', () => {
world.insert(entity, StateMachine, { current: 'pressed', previous: '' } as StateMachineData);
world.insert(entity, StateVisuals, makeVisuals({
@@ -223,6 +241,3 @@ describe('StateVisualsApplySystem — sprite swap', () => {
expect(r.texture).toBe(42);
});
});
-
-// Silence any unused-import warnings
-void vi;