Skip to content

Invoke is a leading creative engine that uses image and video generation models to empower professionals, artists, and enthusiasts to generate and create visual media using the latest AI-driven technologies. The solution offers an industry-leading application interface, and serves as the foundation for multiple commercial products.

perf(canvas): two group-surface slots absorb frame/overview alternation

Josh Corbett7aaa3a8760ac
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupAdjustmentComposite.browser.test.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupAdjustmentComposite.browser.test.ts
index e4d230dd81..f7916e4587 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupAdjustmentComposite.browser.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupAdjustmentComposite.browser.test.ts
@@ -195,6 +195,64 @@ describe('group adjustment composite', () => {
expect(adjusted?.r).toBe(255 - 0x40);
});
+ it('holds two keys per group so alternating consumers (frame vs overview) stop rebuilding', () => {
+ // A transform session makes the frame composite session matrices while the
+ // overview composites the settled contract: two keys for one group, forever
+ // alternating. Two slots must absorb that; a third key still evicts.
+ const scene = sceneFor({ red: '#ff0000' });
+ let builds = 0;
+ const groupSurfaces = createGroupSurfaceCache({
+ createSurface: (w, h) => {
+ builds += 1;
+ return scene.backend.createSurface(w, h);
+ },
+ getAdjustedSurface: () => null,
+ getCacheEntry: (id) => scene.caches.get(id),
+ });
+ const docAt = (x: number) =>
+ docWith([
+ groupContract('g', [raster('red', { transform: { rotation: 0, scaleX: 1, scaleY: 1, x, y: 0 } })], {
+ adjustments: invertStack('ia'),
+ }),
+ ]);
+ const composite = (x: number) => {
+ const screen = scene.backend.createSurface(WIDTH, HEIGHT);
+ compositeDocument(screen, docAt(x), scene.caches, identity(), {
+ backend: scene.backend,
+ groupSurface: (scope, members, matrices, exclude) => groupSurfaces.get(scope, members, matrices, exclude),
+ });
+ return screen;
+ };
+
+ const first = centerPixel(composite(0));
+ composite(1);
+ expect(builds).toBe(2);
+ composite(0);
+ composite(1);
+ composite(0);
+ expect(builds).toBe(2);
+ expect(centerPixel(composite(0))).toEqual(first);
+
+ composite(2);
+ expect(builds).toBe(3);
+ // x:2 evicted the least-recently-used slot (x:1); x:0 is still warm.
+ composite(0);
+ expect(builds).toBe(3);
+ composite(1);
+ expect(builds).toBe(4);
+
+ // A null build (all members excluded, e.g. under a filter preview) must not
+ // evict the warm slots the other consumer still needs.
+ const screen = scene.backend.createSurface(WIDTH, HEIGHT);
+ compositeDocument(screen, docAt(1), scene.caches, identity(), {
+ backend: scene.backend,
+ groupSurface: (scope, members, matrices) => groupSurfaces.get(scope, members, matrices, new Set(['red'])),
+ });
+ const buildsAfterNull = builds;
+ composite(1);
+ expect(builds).toBe(buildsAfterNull);
+ });
+
it('keeps identity and pass-through groups on the flat path and keys the plan by group stacks', () => {
const flat = docWith([groupContract('g', [raster('white')])]);
expect(planBaseRasterComposite(flat, BBOX).groupScopes).toBeUndefined();
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupSurfaceCache.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupSurfaceCache.ts
index 55569f36ff..01e8147b88 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupSurfaceCache.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/render/groupSurfaceCache.ts
@@ -48,8 +48,13 @@ export interface GroupSurfaceCache {
const matKey = (m: Mat2d): string => `${m.a},${m.b},${m.c},${m.d},${m.e},${m.f}`;
+// The frame composites with live session matrices while the overview composites
+// the settled contract, so one group legitimately holds two keys at once; two
+// slots stop those consumers evicting each other every tick.
+const SLOTS_PER_GROUP = 2;
+
export const createGroupSurfaceCache = (deps: GroupSurfaceDeps): GroupSurfaceCache => {
- const cache = new Map<string, { key: string; result: GroupSurfaceResult }>();
+ const cache = new Map<string, { key: string; result: GroupSurfaceResult }[]>();
const scopeShapeKey = (scope: GroupAdjustmentScope): string =>
`${scope.id}@${scope.start}-${scope.end}:${adjustmentsKey(scope.adjustments)}(${scope.children
@@ -163,24 +168,35 @@ export const createGroupSurfaceCache = (deps: GroupSurfaceDeps): GroupSurfaceCac
return {
byteSize: () => {
let bytes = 0;
- for (const { result } of cache.values()) {
- bytes += result.rect.width * result.rect.height * 4;
+ for (const slots of cache.values()) {
+ for (const { result } of slots) {
+ bytes += result.rect.width * result.rect.height * 4;
+ }
}
return bytes;
},
clear: () => cache.clear(),
get: (scope, members, memberMatrices, excludeIds) => {
const key = buildKey(scope, members, memberMatrices, excludeIds);
- const cached = cache.get(scope.id);
- if (cached && cached.key === key) {
- return cached.result;
+ const slots = cache.get(scope.id) ?? [];
+ const hitIndex = slots.findIndex((slot) => slot.key === key);
+ if (hitIndex >= 0) {
+ const hit = slots[hitIndex]!;
+ if (hitIndex > 0) {
+ slots.splice(hitIndex, 1);
+ slots.unshift(hit);
+ }
+ return hit.result;
}
const result = build(scope, members, memberMatrices, excludeIds, scope.start);
if (result) {
- cache.set(scope.id, { key, result });
- } else {
- cache.delete(scope.id);
+ slots.unshift({ key, result });
+ slots.length = Math.min(slots.length, SLOTS_PER_GROUP);
+ cache.set(scope.id, slots);
}
+ // A null build (every member excluded or unrasterized) keeps existing
+ // slots: keys fully determine validity, so a warm slot held by the other
+ // consumer can only ever hit when genuinely valid.
return result;
},
prune: (liveGroupIds) => {