From f64517eb19e69df2b465886bd6183b63f2208ea3 Mon Sep 17 00:00:00 2001 From: wouterlucas Date: Wed, 25 Nov 2026 22:53:51 +0100 Subject: [PATCH] feat(core): translation-only invalidation for x/y updates Phase 2 of the L2-style fast paths: behavior-preserving update-cycle shortcut for pure translation. - New UpdateType.Translate (narrower than Local; All widened to 32767, added to RTT_NOTIFY_MASK). The x/y setters request it; a general Local flag set in the same frame still dominates in update(). - New updateLocalTranslation(): when rotation/scale/contain-resize are inert the local transform is always a pure translation, so only the translation terms are rewritten, including the stable mount offset (localOrigin = position - mount * dimensions). Nonzero mounts are now eligible for the translation fast path. - The global-update fast path keys on _localIsTranslate (mount-aware) instead of isSimple, so both parent global and local collapse to two additions for translation-only nodes under translation-only parents. - Child traversal, visibility, clipping, render-list and RTT notifications are untouched; mixed updates fall back to the general path. Tests: CoreNode.translate.test.ts (9). Full suite 558 passed, build clean, eslint 0 errors (warnings pre-existing / test-style only). --- src/core/CoreNode.translate.test.ts | 309 ++++++++++++++++++++++++++++ src/core/CoreNode.ts | 98 +++++++-- 2 files changed, 390 insertions(+), 17 deletions(-) create mode 100644 src/core/CoreNode.translate.test.ts diff --git a/src/core/CoreNode.translate.test.ts b/src/core/CoreNode.translate.test.ts new file mode 100644 index 00000000..dea0bf5b --- /dev/null +++ b/src/core/CoreNode.translate.test.ts @@ -0,0 +1,309 @@ +/* + * If not stated otherwise in this file or this component's LICENSE file the + * following copyright and licenses apply: + * + * Copyright 2026 Comcast Cable Communications, LLC. + * + * Licensed under the Apache License, Version 2.0 (the License); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +/** + * Tests for the translation-only update path (UpdateType.Translate): + * x/y-only changes must produce exactly the same transforms, render coords + * and bounds as the general local-update path — including stable nonzero + * mounts and transformed parents — while taking the narrower code path. + */ +import { describe, expect, it } from 'vitest'; +import { CoreNode, CoreNodeRenderState, UpdateType } from './CoreNode.js'; +import type { Matrix3d } from './lib/Matrix3d.js'; +import type { Stage } from './Stage.js'; +import { makeMockStage, makeNodeProps } from '../../test/mockStage.js'; + +const clippingRect = { + x: 0, + y: 0, + w: 2000, + h: 2000, + valid: false, + clipRadius: 0, +}; + +const makeStage = (): Stage => makeMockStage({}, { width: 2000, height: 2000 }); + +const matrixFields = (m: Matrix3d | undefined) => + m === undefined ? undefined : [m.ta, m.tb, m.tx, m.tc, m.td, m.ty]; + +const expectSameTransform = (fast: CoreNode, reference: CoreNode): void => { + expect(matrixFields(fast.localTransform)).toEqual( + matrixFields(reference.localTransform), + ); + expect(matrixFields(fast.globalTransform)).toEqual( + matrixFields(reference.globalTransform), + ); + expect({ ...fast.renderCoords }).toEqual({ ...reference.renderCoords }); + expect({ ...fast.renderBound }).toEqual({ ...reference.renderBound }); +}; + +describe('CoreNode translation-only updates', () => { + it('x/y setters request UpdateType.Translate, not Local', () => { + const stage = makeStage(); + const node = new CoreNode(stage, makeNodeProps({ w: 10, h: 10 })); + node.update(0, clippingRect); // flush initial update + expect(node.updateType).toBe(UpdateType.None); + + node.x = 5; + expect(node.updateType & UpdateType.Translate).toBe(UpdateType.Translate); + expect(node.updateType & UpdateType.Local).toBe(0); + + node.update(0, clippingRect); + node.y = 7; + expect(node.updateType & UpdateType.Translate).toBe(UpdateType.Translate); + expect(node.updateType & UpdateType.Local).toBe(0); + }); + + it('x/y-only motion matches the general path exactly', () => { + const stage = makeStage(); + const root = new CoreNode(stage, makeNodeProps({ w: 500, h: 500 })); + const fast = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 40, h: 30, x: 10, y: 20 }), + ); + const ref = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 40, h: 30, x: 10, y: 20 }), + ); + root.update(0, clippingRect); + + fast.x = 111; + fast.y = 222; + // Reference goes through the general local path. + ref.props.x = 111; + ref.props.y = 222; + ref.setUpdateType(UpdateType.Local); + + root.update(1, clippingRect); + expectSameTransform(fast, ref); + expect(fast.renderCoords!.x1).toBe(111); + expect(fast.renderCoords!.y1).toBe(222); + expect(fast.globalTransform!.tx).toBe(111); + expect(fast.globalTransform!.ty).toBe(222); + }); + + it('supports stable nonzero mounts (centered square stays translation-only)', () => { + const stage = makeStage(); + const root = new CoreNode(stage, makeNodeProps({ w: 500, h: 500 })); + const fast = new CoreNode( + stage, + makeNodeProps({ + parent: root, + w: 40, + h: 40, + x: 100, + y: 100, + mountX: 0.5, + mountY: 0.5, + }), + ); + const ref = new CoreNode( + stage, + makeNodeProps({ + parent: root, + w: 40, + h: 40, + x: 100, + y: 100, + mountX: 0.5, + mountY: 0.5, + }), + ); + // A child of the mount node, to verify inherited world updates still work. + const leaf = new CoreNode( + stage, + makeNodeProps({ parent: fast, w: 5, h: 5, x: 1, y: 2 }), + ); + const leafRef = new CoreNode( + stage, + makeNodeProps({ parent: ref, w: 5, h: 5, x: 1, y: 2 }), + ); + root.update(0, clippingRect); + // Mount-only local transform is a pure translation of (x - 0.5*w). + expect(fast._localIsTranslate).toBe(true); + expect(fast.localTransform!.tx).toBe(80); + expect(fast.localTransform!.ty).toBe(80); + + fast.x = 200; + ref.props.x = 200; + ref.setUpdateType(UpdateType.Local); + root.update(1, clippingRect); + + expectSameTransform(fast, ref); + expect(fast.localTransform!.tx).toBe(180); + // The mount node is translate-classified, so it takes the cheap + // translate() global path rather than a full matrix multiply. + expectSameTransform(leaf, leafRef); + expect(leaf.globalTransform!.tx).toBe(181); + expect(leaf.globalTransform!.ty).toBe(82); + }); + + it('maps child translation through an affine (rotated/scaled) parent', () => { + const stage = makeStage(); + const root = new CoreNode( + stage, + makeNodeProps({ + w: 500, + h: 500, + x: 50, + y: 60, + rotation: Math.PI / 6, + scaleX: 2, + scaleY: 1.5, + pivot: 0.5, + }), + ); + const fast = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 5, y: 6 }), + ); + const ref = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 5, y: 6 }), + ); + root.update(0, clippingRect); + + fast.x = 77; + fast.y = 88; + ref.props.x = 77; + ref.props.y = 88; + ref.setUpdateType(UpdateType.Local); + root.update(1, clippingRect); + + expectSameTransform(fast, ref); + // Parent is not a pure translation, so the child must not take the + // two-addition fast path — but the result must still be correct. + expect(fast._globalIsTranslate).toBe(false); + const p = root.globalTransform!; + const l = fast.localTransform!; + expect(fast.globalTransform!.tx).toBeCloseTo( + p.ta * l.tx + p.tb * l.ty + p.tx, + ); + expect(fast.globalTransform!.ty).toBeCloseTo( + p.tc * l.tx + p.td * l.ty + p.ty, + ); + }); + + it('applies simultaneous parent and child motion exactly once', () => { + const stage = makeStage(); + const root = new CoreNode( + stage, + makeNodeProps({ w: 500, h: 500, x: 0, y: 0 }), + ); + const child = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 10, y: 10 }), + ); + root.update(0, clippingRect); + expect(child.globalTransform!.tx).toBe(10); + + root.x = 100; + child.x = 30; + root.update(1, clippingRect); + + expect(child.globalTransform!.tx).toBe(130); + expect(child.globalTransform!.ty).toBe(10); + expect(child.renderCoords!.x1).toBe(130); + }); + + it('a general Local change dominates a mixed Translate+Local frame', () => { + const stage = makeStage(); + const root = new CoreNode(stage, makeNodeProps({ w: 500, h: 500 })); + const fast = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 0, y: 0 }), + ); + const ref = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 0, y: 0 }), + ); + root.update(0, clippingRect); + + // Size change (Local) and position change (Translate) in the same frame, + // in both orders. + fast.w = 25; + fast.x = 40; + ref.props.w = 25; + ref.props.x = 40; + ref.setUpdateType(UpdateType.Local); + root.update(1, clippingRect); + expectSameTransform(fast, ref); + + fast.x = 90; + fast.h = 33; + ref.props.x = 90; + ref.props.h = 33; + ref.setUpdateType(UpdateType.Local); + root.update(2, clippingRect); + expectSameTransform(fast, ref); + expect(fast.renderCoords!.x2).toBe(90 + 25); + expect(fast.renderCoords!.y3).toBe(33); + }); + + it('keeps visibility transitions when moving in and out of the viewport', () => { + const stage = makeMockStage({}, { width: 100, height: 100 }); + const root = new CoreNode(stage, makeNodeProps({ w: 100, h: 100 })); + const node = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 10, y: 10 }), + ); + root.update(0, clippingRect); + expect(node.renderState).toBe(CoreNodeRenderState.InViewport); + + node.x = 5000; + root.update(1, clippingRect); + expect(node.renderState).toBe(CoreNodeRenderState.OutOfBounds); + + node.x = 20; + root.update(2, clippingRect); + expect(node.renderState).toBe(CoreNodeRenderState.InViewport); + expect(node.renderCoords!.x1).toBe(20); + }); + + it('marks the quad dirty on translation-only updates', () => { + const stage = makeMockStage({ + options: { enableDirtyRepaints: true }, + } as Partial) as Stage; + const root = new CoreNode(stage, makeNodeProps({ w: 100, h: 100 })); + const node = new CoreNode( + stage, + makeNodeProps({ parent: root, w: 10, h: 10, x: 1, y: 1 }), + ); + root.update(0, clippingRect); + node.isQuadDirty = false; + + node.x = 2; + root.update(1, clippingRect); + expect(node.isQuadDirty).toBe(true); + }); + + it('repeated x writes before an update collapse into one translation', () => { + const stage = makeStage(); + const node = new CoreNode(stage, makeNodeProps({ w: 10, h: 10 })); + node.update(0, clippingRect); + + node.x = 10; + node.x = 20; + node.x = 30; + node.update(1, clippingRect); + expect(node.globalTransform!.tx).toBe(30); + expect(node.localTransform!.tx).toBe(30); + }); +}); diff --git a/src/core/CoreNode.ts b/src/core/CoreNode.ts index 4f113da0..f438cd66 100644 --- a/src/core/CoreNode.ts +++ b/src/core/CoreNode.ts @@ -194,6 +194,19 @@ export enum UpdateType { * Autosize update */ Autosize = 8192, + + /** + * Translation-only local update (x and/or y changed, nothing else) + * + * @remarks + * Narrower than {@link Local}: requested by the `x`/`y` setters when no + * other local property changed in the same frame. Lets the update cycle + * reuse the cached transform basis and only recompute translation terms. + * A general {@link Local} flag always dominates when both are set. + * CoreNode Properties Updated: + * - `localTransform` (translation terms only) + */ + Translate = 16384, /** * None */ @@ -202,7 +215,7 @@ export enum UpdateType { /** * All */ - All = 16383, + All = 32767, } /** @@ -216,6 +229,7 @@ export enum UpdateType { */ const RTT_NOTIFY_MASK = UpdateType.Local | + UpdateType.Translate | UpdateType.Global | UpdateType.Clipping | UpdateType.SortZIndexChildren | @@ -1185,8 +1199,7 @@ export class CoreNode extends EventEmitter { updateIsSimple(): void { const p = this.props; this._hasContainResize = - p.texture !== null && - p.textureOptions.resizeMode?.type === 'contain'; + p.texture !== null && p.textureOptions.resizeMode?.type === 'contain'; this.isSimple = p.rotation === 0 && p.scaleX === 1 && @@ -1294,7 +1307,52 @@ export class CoreNode extends EventEmitter { .scale(resizeModeScaleX, resizeModeScaleY); } - this._localIsTranslate = false; + // The resulting matrix is a pure translation whenever rotation and scale + // are identity and there is no contain-resize — even with a nonzero + // mount (mount only shifts the translation origin). Pivot terms cancel + // out when rotation === 0 and scale === 1. + this._localIsTranslate = + rotation === 0 && + scaleX === 1 && + scaleY === 1 && + this._hasContainResize === false; + } + + /** + * Translation-only local transform update, used when only `x`/`y` changed + * (UpdateType.Translate). + * + * @remarks + * Reuses the existing transform basis: with no rotation, no scale and no + * contain-resize the local transform is always a pure translation, so only + * the translation terms need to be rewritten — including the stable mount + * offset (`localOrigin = position - mount * dimensions`). Falls back to the + * general {@link updateLocalTransform} when any non-translation property is + * in play (defensive; those setters also set UpdateType.Local which + * dominates). + */ + updateLocalTranslation() { + const p = this.props; + if ( + p.rotation !== 0 || + p.scaleX !== 1 || + p.scaleY !== 1 || + this._hasContainResize === true + ) { + this.updateLocalTransform(); + return; + } + const lt = this.localTransform ?? (this.localTransform = new Matrix3d()); + if (this._localIsTranslate === false) { + // Matrix may not be identity-shape yet — normalize it before the + // translation-only write. + lt.ta = 1; + lt.tb = 0; + lt.tc = 0; + lt.td = 1; + } + lt.setTranslate(p.x - p.mountX * p.w, p.y - p.mountY * p.h); + this._localIsTranslate = true; } /** @@ -1325,6 +1383,13 @@ export class CoreNode extends EventEmitter { if (updateType & UpdateType.Local) { this.updateLocalTransform(); + updateType |= UpdateType.Global; + updateParent = hasParent; + } else if (updateType & UpdateType.Translate) { + // Narrower than Local: only x/y changed. A general Local flag (mixed + // in the same frame) dominates and takes the branch above. + this.updateLocalTranslation(); + updateType |= UpdateType.Global; updateParent = hasParent; } @@ -1370,13 +1435,15 @@ export class CoreNode extends EventEmitter { // Common non-RTT path const parentGT = parent?.globalTransform; if ( - this.isSimple === true && + this._localIsTranslate === true && parent !== null && parent._globalIsTranslate === true && parentGT !== undefined ) { // Translate-only fast path: both parent global and local are pure // translations. The global transform collapses to 2 additions. + // `_localIsTranslate` (rather than `isSimple`) also admits stable + // nonzero mounts, whose local transform is still a pure translation. if (this._globalIsTranslate === false) { gt.ta = 1; gt.tb = 0; @@ -1389,7 +1456,7 @@ export class CoreNode extends EventEmitter { } else { Matrix3d.copy(parentGT || lt, gt); this._globalIsTranslate = - this.isSimple === true && + this._localIsTranslate === true && parent !== null && parent._globalIsTranslate === true; } @@ -1397,7 +1464,7 @@ export class CoreNode extends EventEmitter { if (fastPathApplied === false) { if (parent !== null) { - if (this.isSimple === true) { + if (this._localIsTranslate === true) { gt.translate(lt.tx, lt.ty); } else { gt.translateOrMultiply(lt); @@ -2259,7 +2326,10 @@ export class CoreNode extends EventEmitter { set x(value: number) { if (this.props.x !== value) { this.props.x = value; - this.setUpdateType(UpdateType.Local); + // Narrower than Local: only the translation term changes. A general + // Local flag set in the same frame (size/rotation/...) still dominates + // in update(). + this.setUpdateType(UpdateType.Translate); } } @@ -2286,7 +2356,7 @@ export class CoreNode extends EventEmitter { set y(value: number) { if (this.props.y !== value) { this.props.y = value; - this.setUpdateType(UpdateType.Local); + this.setUpdateType(UpdateType.Translate); } } @@ -2300,10 +2370,7 @@ export class CoreNode extends EventEmitter { props.w = value; let updateType = UpdateType.Local | UpdateType.RecalcUniforms; - if ( - props.texture !== null && - this.stage.calculateTextureCoord === true - ) { + if (props.texture !== null && this.stage.calculateTextureCoord === true) { this.textureCoords = this.stage.renderer.getTextureCoords!(this); } @@ -2329,10 +2396,7 @@ export class CoreNode extends EventEmitter { props.h = value; let updateType = UpdateType.Local | UpdateType.RecalcUniforms; - if ( - props.texture !== null && - this.stage.calculateTextureCoord === true - ) { + if (props.texture !== null && this.stage.calculateTextureCoord === true) { this.textureCoords = this.stage.renderer.getTextureCoords!(this); }