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.

fix(performance): keep canvas sync within budgets

Josh Corbett896c092313c1
diff --git a/invokeai/frontend/webv2/performance/architecture-baseline.json b/invokeai/frontend/webv2/performance/architecture-baseline.json
index bd4833b36e..0494f010de 100644
--- a/invokeai/frontend/webv2/performance/architecture-baseline.json
+++ b/invokeai/frontend/webv2/performance/architecture-baseline.json
@@ -464,7 +464,6 @@
"source:src/features/generation/core/batch.ts",
"source:src/features/generation/core/canvas/addControlLayers.ts",
"source:src/features/generation/core/canvas/addRegionalGuidance.ts",
- "source:src/features/generation/core/canvas/compileCanvasGraph.ts",
"source:src/features/generation/core/canvas/controlValidation.ts",
"source:src/features/generation/core/catalogSearch.ts",
"source:src/features/generation/core/componentCompatibility.ts",
diff --git a/invokeai/frontend/webv2/performance/browser-baseline.json b/invokeai/frontend/webv2/performance/browser-baseline.json
index 18d4df754e..b79131a123 100644
--- a/invokeai/frontend/webv2/performance/browser-baseline.json
+++ b/invokeai/frontend/webv2/performance/browser-baseline.json
@@ -1115,9 +1115,7 @@
"source:src/features/generation/core/batch.ts",
"source:src/features/generation/core/canvas/addControlLayers.ts",
"source:src/features/generation/core/canvas/addRegionalGuidance.ts",
- "source:src/features/generation/core/canvas/compileCanvasGraph.ts",
"source:src/features/generation/core/canvas/controlValidation.ts",
- "source:src/features/generation/core/canvas/types.ts",
"source:src/features/generation/core/catalogSearch.ts",
"source:src/features/generation/core/componentCompatibility.ts",
"source:src/features/generation/core/conditioningRebalance.ts",
@@ -2043,9 +2041,7 @@
"source:src/features/generation/core/batch.ts",
"source:src/features/generation/core/canvas/addControlLayers.ts",
"source:src/features/generation/core/canvas/addRegionalGuidance.ts",
- "source:src/features/generation/core/canvas/compileCanvasGraph.ts",
"source:src/features/generation/core/canvas/controlValidation.ts",
- "source:src/features/generation/core/canvas/types.ts",
"source:src/features/generation/core/catalogSearch.ts",
"source:src/features/generation/core/componentCompatibility.ts",
"source:src/features/generation/core/conditioningRebalance.ts",
@@ -3081,9 +3077,7 @@
"source:src/features/generation/core/batch.ts",
"source:src/features/generation/core/canvas/addControlLayers.ts",
"source:src/features/generation/core/canvas/addRegionalGuidance.ts",
- "source:src/features/generation/core/canvas/compileCanvasGraph.ts",
"source:src/features/generation/core/canvas/controlValidation.ts",
- "source:src/features/generation/core/canvas/types.ts",
"source:src/features/generation/core/catalogSearch.ts",
"source:src/features/generation/core/componentCompatibility.ts",
"source:src/features/generation/core/conditioningRebalance.ts",
diff --git a/invokeai/frontend/webv2/scripts/write-contract-caller-matrix.mjs b/invokeai/frontend/webv2/scripts/write-contract-caller-matrix.mjs
index 81b93af7d4..4a4688cb5a 100644
--- a/invokeai/frontend/webv2/scripts/write-contract-caller-matrix.mjs
+++ b/invokeai/frontend/webv2/scripts/write-contract-caller-matrix.mjs
@@ -37,6 +37,7 @@ const hubs = [
'features/models/react.ts',
'features/nodes/index.ts',
'features/generation/components.ts',
+ 'features/generation/canvasGraph.ts',
'features/generation/contracts.ts',
'features/generation/graph.ts',
'features/generation/index.ts',
diff --git a/invokeai/frontend/webv2/src/architecture/dependencyPolicy.test.ts b/invokeai/frontend/webv2/src/architecture/dependencyPolicy.test.ts
index f526cff46e..c1d46b4854 100644
--- a/invokeai/frontend/webv2/src/architecture/dependencyPolicy.test.ts
+++ b/invokeai/frontend/webv2/src/architecture/dependencyPolicy.test.ts
@@ -181,6 +181,7 @@ describe('feature public-interface registry', () => {
// Generation's prompt cache through the public entry module.
expect(checkDependency('workbench/invocationSubmit.ts', '@features/generation/prompts')).toEqual([]);
expect(checkDependency('app/GenerateWidgetSyncRuntime.tsx', '@features/generation/runtime')).toEqual([]);
+ expect(checkDependency('workbench/widgets/canvas/invoke.ts', '@features/generation/canvasGraph')).toEqual([]);
expect(
checkDependency('workbench/invocationSubmit.ts', '@features/generation/data/dynamicPromptsQueries')
).toMatchObject([{ rule: 'feature-private-interface' }]);
diff --git a/invokeai/frontend/webv2/src/architecture/featureInterfaces.ts b/invokeai/frontend/webv2/src/architecture/featureInterfaces.ts
index fd13bdc5e1..c331773b61 100644
--- a/invokeai/frontend/webv2/src/architecture/featureInterfaces.ts
+++ b/invokeai/frontend/webv2/src/architecture/featureInterfaces.ts
@@ -7,6 +7,7 @@
export const FEATURE_PUBLIC_INTERFACES: Readonly<Record<string, readonly string[]>> = {
gallery: ['contracts', 'launchpad', 'paletteSearch', 'queries', 'react', 'utility', 'widget'],
generation: [
+ 'canvasGraph',
'components',
'contracts',
'graph',
diff --git a/invokeai/frontend/webv2/src/features/generation/canvasGraph.ts b/invokeai/frontend/webv2/src/features/generation/canvasGraph.ts
new file mode 100644
index 0000000000..487c1fb24f
--- /dev/null
+++ b/invokeai/frontend/webv2/src/features/generation/canvasGraph.ts
@@ -0,0 +1,10 @@
+/**
+ * Canvas graph compilation's lazy-load interface.
+ *
+ * Keep this separate from the shared `graph` interface: the Canvas invocation
+ * runtime is loaded on demand, while generate graph helpers are needed during
+ * workbench startup. Mixing both compilers in one interface pulls Canvas-only
+ * processing code into the initial editor graph.
+ */
+export { compileCanvasGraph } from './core/canvas/compileCanvasGraph';
+export type { CanvasCompileMode } from './core/canvas/types';
diff --git a/invokeai/frontend/webv2/src/features/generation/graph.ts b/invokeai/frontend/webv2/src/features/generation/graph.ts
index b83aab7726..f670562346 100644
--- a/invokeai/frontend/webv2/src/features/generation/graph.ts
+++ b/invokeai/frontend/webv2/src/features/generation/graph.ts
@@ -1,6 +1,7 @@
/**
- * Generation's graph-compilation surface: generate/canvas graph compilers and
- * the graph-builder primitives shared with other invocation sources.
+ * Generation's eagerly shared graph surface: the generate compiler, Canvas
+ * validation policies, and graph-builder primitives used by other invocation
+ * sources. Canvas compilation lives in the lazy `canvasGraph` interface.
* Curated, caller-driven export list — add a symbol only when a consumer needs it.
*/
export {
@@ -17,7 +18,6 @@ export {
toGraphContract,
toModelIdentifier,
} from './core/graphBuilder';
-export { compileCanvasGraph } from './core/canvas/compileCanvasGraph';
export { detectCanvasMode } from './core/canvas/canvasMode';
export {
type ControlAdapterKind,
@@ -33,4 +33,3 @@ export {
type RegionalReferenceImageInput,
} from './core/canvas/addRegionalGuidance';
export type { ControlLayerGraphInput } from './core/canvas/addControlLayers';
-export type { CanvasCompileMode } from './core/canvas/types';
diff --git a/invokeai/frontend/webv2/src/workbench/canvasDimsSync.ts b/invokeai/frontend/webv2/src/workbench/canvasDimsSync.ts
index 8f7c210ab3..0f9e43571f 100644
--- a/invokeai/frontend/webv2/src/workbench/canvasDimsSync.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvasDimsSync.ts
@@ -77,6 +77,25 @@ export type CanvasDimsReconcileResult =
/** Resize the bbox to the (grid-snapped) generate dims, keeping its top-left position. */
| { kind: 'set-bbox'; bbox: Bbox };
+const getDimsPatch = (width: number, height: number, aspectWidth = width, aspectHeight = height) => ({
+ aspectRatioId: deriveAspectRatioId(aspectWidth, aspectHeight),
+ aspectRatioValue: aspectHeight > 0 ? aspectWidth / aspectHeight : 1,
+ height,
+ width,
+});
+
+const createSnapshot = (
+ bbox: Pick<Bbox, 'width' | 'height'>,
+ dims: { width: number; height: number },
+ grid: number
+): CanvasDimsSnapshot => ({
+ bboxHeight: bbox.height,
+ bboxWidth: bbox.width,
+ dimsHeight: dims.height,
+ dimsWidth: dims.width,
+ grid,
+});
+
/**
* Decide which direction of the bbox <-> dims sync to apply.
*
@@ -99,7 +118,7 @@ export const reconcileCanvasDims = ({
}
const bboxIsOnGrid = bbox.width % grid === 0 && bbox.height % grid === 0;
- const gridChanged = prev !== null && prev.grid !== grid;
+ const gridChanged = prev && prev.grid !== grid;
if (bboxIsOnGrid && !gridChanged && bbox.width === dims.width && bbox.height === dims.height) {
return { kind: 'none' };
@@ -111,16 +130,10 @@ export const reconcileCanvasDims = ({
const width = clampDimension(bbox.width, grid);
const height = clampDimension(bbox.height, grid);
- return {
- aspectRatioId: deriveAspectRatioId(bbox.width, bbox.height),
- aspectRatioValue: bbox.height > 0 ? bbox.width / bbox.height : 1,
- height,
- kind: 'patch-dims',
- width,
- };
+ return { ...getDimsPatch(width, height, bbox.width, bbox.height), kind: 'patch-dims' };
}
- const dimsChanged = !prev || prev.dimsWidth !== dims.width || prev.dimsHeight !== dims.height;
+ const dimsChanged = prev!.dimsWidth !== dims.width || prev!.dimsHeight !== dims.height;
if (dimsChanged) {
const width = clampDimension(dims.width, grid);
@@ -152,7 +165,7 @@ export interface CanvasDimsSync {
const readFiniteDimension = (values: Record<string, unknown>, key: 'width' | 'height'): number | null => {
const raw = values[key];
- return typeof raw === 'number' && Number.isFinite(raw) && raw > 0 ? raw : null;
+ return Number.isFinite(raw as number) && (raw as number) > 0 ? (raw as number) : null;
};
const readModelBase = (values: Record<string, unknown>): string | null => {
@@ -213,59 +226,32 @@ export const createCanvasDimsSync = (store: CanvasDimsSyncStore): CanvasDimsSync
const bbox = project.canvas.document.bbox;
const grid = gridSizeForModelBase(readModelBase(generateValues));
const result = reconcileCanvasDims({ bbox, dims: { height, width }, grid, prev });
+ const projectId = project.id;
switch (result.kind) {
case 'none': {
- prev = { bboxHeight: bbox.height, bboxWidth: bbox.width, dimsHeight: height, dimsWidth: width, grid };
+ prev = createSnapshot(bbox, { height, width }, grid);
return;
}
case 'patch-dims': {
- prev = {
- bboxHeight: bbox.height,
- bboxWidth: bbox.width,
- dimsHeight: result.height,
- dimsWidth: result.width,
- grid,
- };
+ const { kind: _, ...patch } = result;
+ prev = createSnapshot(bbox, result, grid);
isSyncing = true;
try {
- store.commands.generation.patchSettings(
- {
- aspectRatioId: result.aspectRatioId,
- aspectRatioValue: result.aspectRatioValue,
- height: result.height,
- width: result.width,
- },
- project.id,
- 'system'
- );
+ store.commands.generation.patchSettings(patch, projectId, 'system');
} finally {
isSyncing = false;
}
return;
}
case 'set-bbox': {
- prev = {
- bboxHeight: result.bbox.height,
- bboxWidth: result.bbox.width,
- dimsHeight: result.bbox.height,
- dimsWidth: result.bbox.width,
- grid,
- };
+ const nextBbox = result.bbox;
+ prev = createSnapshot(nextBbox, nextBbox, grid);
isSyncing = true;
try {
- store.commands.canvas.apply(project.id, { bbox: result.bbox, type: 'setCanvasBbox' }, 'system');
- if (width !== result.bbox.width || height !== result.bbox.height) {
- store.commands.generation.patchSettings(
- {
- aspectRatioId: deriveAspectRatioId(result.bbox.width, result.bbox.height),
- aspectRatioValue: result.bbox.width / result.bbox.height,
- height: result.bbox.height,
- width: result.bbox.width,
- },
- project.id,
- 'system'
- );
+ store.commands.canvas.apply(projectId, { bbox: nextBbox, type: 'setCanvasBbox' }, 'system');
+ if (width !== nextBbox.width || height !== nextBbox.height) {
+ store.commands.generation.patchSettings(getDimsPatch(nextBbox.width, nextBbox.height), projectId, 'system');
}
} finally {
isSyncing = false;
diff --git a/invokeai/frontend/webv2/src/workbench/widgets/canvas/invoke/prepareCanvasInvocation.ts b/invokeai/frontend/webv2/src/workbench/widgets/canvas/invoke/prepareCanvasInvocation.ts
index 1a4fe8c07d..0da1db907f 100644
--- a/invokeai/frontend/webv2/src/workbench/widgets/canvas/invoke/prepareCanvasInvocation.ts
+++ b/invokeai/frontend/webv2/src/workbench/widgets/canvas/invoke/prepareCanvasInvocation.ts
@@ -42,14 +42,13 @@ import type { WorkbenchNotificationKind } from '@workbench/projectContracts';
import type { ProjectSettings } from '@workbench/settings/contracts';
import type { WorkbenchCommands, WorkbenchNotificationCommands } from '@workbench/workbenchStore';
+import { compileCanvasGraph, type CanvasCompileMode } from '@features/generation/canvasGraph';
import {
- compileCanvasGraph,
detectCanvasMode,
getControlValidationReason,
getControlValidationReasonMessage,
getRegionalGuidanceRejectionReason,
isRegionalGuidanceSupportedForBase,
- type CanvasCompileMode,
type ControlLayerGraphInput,
type ControlValidationReason,
type RegionalGuidanceInput,