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(webv2): apply warm preset switch synchronously

joshistoastc43572bfea4e
diff --git a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.test.ts b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.test.ts
index b2706cf4e8..b8047ae35a 100644
--- a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.test.ts
+++ b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.test.ts
@@ -21,6 +21,7 @@ describe('layout preset activation', () => {
apply: (presetId) => appliedPresetIds.push(presetId),
getActiveProjectId: () => 'project-a',
isCurrent: () => true,
+ isLoaded: () => false,
load: (preset) => (preset.id === 'compose' ? composeLoad.promise : editLoad.promise),
});
@@ -45,6 +46,7 @@ describe('layout preset activation', () => {
applyDeadlineMs: 0,
getActiveProjectId: () => activeProjectId,
isCurrent: () => true,
+ isLoaded: () => false,
load: () => new Promise(() => {}),
});
@@ -62,6 +64,7 @@ describe('layout preset activation', () => {
applyDeadlineMs: 0,
getActiveProjectId: () => 'project-a',
isCurrent: () => false,
+ isLoaded: () => false,
load: () => new Promise(() => {}),
});
@@ -76,6 +79,7 @@ describe('layout preset activation', () => {
applyDeadlineMs: 0,
getActiveProjectId: () => 'project-a',
isCurrent: () => true,
+ isLoaded: () => false,
load: () => new Promise(() => {}),
});
@@ -91,6 +95,7 @@ describe('layout preset activation', () => {
applyDeadlineMs: 0,
getActiveProjectId: () => 'project-a',
isCurrent: () => true,
+ isLoaded: () => false,
load: () => new Promise(() => {}),
});
@@ -101,4 +106,72 @@ describe('layout preset activation', () => {
expect(appliedPresetIds).toEqual([]);
});
+
+ it('applies synchronously when every widget is already loaded', () => {
+ const appliedPresetIds: string[] = [];
+ const activator = createLayoutPresetActivator({
+ apply: (presetId) => appliedPresetIds.push(presetId),
+ getActiveProjectId: () => 'project-a',
+ isCurrent: () => true,
+ isLoaded: () => true,
+ load: () => Promise.resolve(),
+ });
+
+ // No await: a warm switch must commit in the caller's own task, not a
+ // microtask later, so the click that triggered it paints with the layout.
+ void activator.activate(layoutPresets[1]);
+
+ expect(appliedPresetIds).toEqual(['edit']);
+ });
+
+ it('still honours the deadline when a widget is cold', async () => {
+ const appliedPresetIds: string[] = [];
+ const activator = createLayoutPresetActivator({
+ apply: (presetId) => appliedPresetIds.push(presetId),
+ applyDeadlineMs: 0,
+ getActiveProjectId: () => 'project-a',
+ isCurrent: () => true,
+ isLoaded: () => false,
+ load: () => new Promise(() => {}),
+ });
+
+ const activation = activator.activate(layoutPresets[0]);
+
+ expect(appliedPresetIds).toEqual([]);
+ await activation;
+ expect(appliedPresetIds).toEqual([layoutPresets[0].id]);
+ });
+
+ // Condition 2 of the fast-path contract: the fast path must bump
+ // latestRequestId itself, not rely on apply's downstream dispatch to do it.
+ // If it didn't, an earlier slow activation — still racing its deadline —
+ // would see requestId === latestRequestId when its timer fires and would
+ // overwrite the fast-path's result.
+ it('bumps the request id so an earlier pending slow activation cannot overwrite a later fast-path apply', async () => {
+ const appliedPresetIds: string[] = [];
+ let loaded = false;
+ const activator = createLayoutPresetActivator({
+ apply: (presetId) => appliedPresetIds.push(presetId),
+ applyDeadlineMs: 0,
+ getActiveProjectId: () => 'project-a',
+ isCurrent: () => true,
+ isLoaded: () => loaded,
+ load: () => new Promise(() => {}),
+ });
+
+ // Start a slow activation for a cold preset; it will sit waiting for its
+ // deadline timer.
+ const slowActivation = activator.activate(layoutPresets[0]);
+
+ // A later, fully-warm activation for a different preset takes the fast
+ // path and applies synchronously.
+ loaded = true;
+ void activator.activate(layoutPresets[1]);
+ expect(appliedPresetIds).toEqual(['edit']);
+
+ // When the slow activation's deadline fires, it must see it has been
+ // superseded and must not clobber the fast-path result.
+ await expect(slowActivation).resolves.toBeNull();
+ expect(appliedPresetIds).toEqual(['edit']);
+ });
});
diff --git a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts
index 890d5d42dd..d5b6a31073 100644
--- a/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts
+++ b/invokeai/frontend/webv2/src/workbench/layoutPresetActivation.ts
@@ -4,10 +4,12 @@ import { getLayoutWidgetTypeIds } from './layoutWidgetSet';
import { loadWidgets, warmWidgets } from './widgetRegistry';
/**
- * How long an activation waits for widget implementations before applying the
- * preset anyway. Warm widgets — the common case, given boot preloading and the
- * strip's hover preload — settle immediately and keep the atomic, no-fallback
- * reveal. A cold or failing chunk stops gating the entire layout at this
+ * How long a *cold* activation waits for widget implementations before
+ * applying the preset anyway. A fully warm switch (every widget already in
+ * memory) never consults this — it applies synchronously in the caller's own
+ * task. This deadline only bounds how long the tab waits before revealing a
+ * cold switch progressively; the tab has already acknowledged the press
+ * either way. A cold or failing chunk stops gating the entire layout at this
* deadline: the switch commits and the stragglers reveal progressively behind
* their own per-widget fallbacks.
*/
@@ -19,6 +21,8 @@ interface LayoutPresetActivatorDependencies {
applyDeadlineMs?: number;
getActiveProjectId: () => string;
isCurrent: (preset: LayoutPreset) => boolean;
+ /** Whether every widget the preset renders is already in memory. */
+ isLoaded: (preset: LayoutPreset) => boolean;
load: (preset: LayoutPreset) => Promise<unknown>;
}
@@ -42,6 +46,7 @@ export const createLayoutPresetActivator = ({
applyDeadlineMs = APPLY_DEADLINE_MS,
getActiveProjectId,
isCurrent,
+ isLoaded,
load,
}: LayoutPresetActivatorDependencies) => {
let latestRequestId = 0;
@@ -60,6 +65,18 @@ export const createLayoutPresetActivator = ({
activate: async (preset: LayoutPreset): Promise<LayoutPresetId | null> => {
const projectId = getActiveProjectId();
const requestId = ++latestRequestId;
+
+ // The common case — hover-preloaded, or a preset visited before. Awaiting
+ // here costs a microtask, which is enough to push the commit out of the
+ // click's own task and lose the frame on which the press was acknowledged.
+ // `requestId` is already bumped above, so a still-pending slow activation
+ // that lands after this one cannot overwrite it.
+ if (isLoaded(preset)) {
+ apply(preset.id);
+
+ return preset.id;
+ }
+
await waitForLoadOrDeadline(load(preset), applyDeadlineMs);
if (requestId !== latestRequestId || projectId !== getActiveProjectId() || !isCurrent(preset)) {
diff --git a/invokeai/frontend/webv2/src/workbench/workbenchStore.ts b/invokeai/frontend/webv2/src/workbench/workbenchStore.ts
index 964745a0e3..de07cdf8d4 100644
--- a/invokeai/frontend/webv2/src/workbench/workbenchStore.ts
+++ b/invokeai/frontend/webv2/src/workbench/workbenchStore.ts
@@ -15,7 +15,9 @@ import type { CanvasProjectMutation } from './canvasProjectMutations';
import { recordDiagnosticEntry } from './diagnostics/logger';
import { createLayoutPresetActivator, loadLayoutPresetWidgets } from './layoutPresetActivation';
import { resolveSavedLayoutPreset } from './layoutPresetSnapshots';
+import { getLayoutWidgetTypeIds } from './layoutWidgetSet';
import { getWorkbenchPreferences } from './settings/store';
+import { areWidgetsLoaded } from './widgetRegistry';
import {
createInitialWorkbenchState,
__workbenchReducerInternal,
@@ -526,6 +528,7 @@ export interface WorkbenchInternalStore {
}
export interface WorkbenchStoreOptions {
+ isLoaded?: (preset: LayoutPreset) => boolean;
loadLayoutPresetWidgets?: (preset: LayoutPreset) => Promise<unknown>;
}
@@ -666,6 +669,7 @@ export const createWorkbenchStore = (
return current.id === preset.id && current.snapshot === preset.snapshot;
},
+ isLoaded: options.isLoaded ?? ((preset) => areWidgetsLoaded(getLayoutWidgetTypeIds(preset.snapshot))),
load: options.loadLayoutPresetWidgets ?? loadLayoutPresetWidgets,
});
invalidateLayoutPresetActivation = layoutPresetActivator.invalidate;