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): hoist per-sample work off the pointer path

joshistoastdc5c9fe2c32d
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/input/pointerPipeline.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/input/pointerPipeline.ts
index 322b974aea..55f623fe7d 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/input/pointerPipeline.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/input/pointerPipeline.ts
@@ -142,10 +142,17 @@ export const createPointerPipeline = (deps: PointerPipelineDeps): PointerPipelin
let bboxQuickTap = false;
let bboxTapTimer: ReturnType<typeof setTimeout> | null = null;
- const buildPointerInput = (event: PointerEvent): PointerInput => {
- const el = deps.getInputElement();
- const rect = el?.getBoundingClientRect() ?? ({ left: 0, top: 0 } as DOMRect);
- const screenPoint: Vec2 = { x: event.clientX - rect.left, y: event.clientY - rect.top };
+ /**
+ * Reads the input element's viewport offset. Hoisted out of
+ * {@link buildPointerInput} so a coalesced batch pays for one layout read
+ * instead of one per sample — the whole batch comes from a single event, so
+ * the element cannot have moved between its samples.
+ */
+ const inputOrigin = (): { left: number; top: number } =>
+ deps.getInputElement()?.getBoundingClientRect() ?? { left: 0, top: 0 };
+
+ const buildPointerInput = (event: PointerEvent, origin = inputOrigin()): PointerInput => {
+ const screenPoint: Vec2 = { x: event.clientX - origin.left, y: event.clientY - origin.top };
return {
buttons: event.buttons,
documentPoint: deps.viewport.screenToDocument(screenPoint),
@@ -158,11 +165,12 @@ export const createPointerPipeline = (deps: PointerPipelineDeps): PointerPipelin
};
const buildBatch = (event: PointerEvent): PointerInput[] => {
+ const origin = inputOrigin();
const coalesced = event.getCoalescedEvents?.();
if (coalesced && coalesced.length > 0) {
- return coalesced.map(buildPointerInput);
+ return coalesced.map((sample) => buildPointerInput(sample, origin));
}
- return [buildPointerInput(event)];
+ return [buildPointerInput(event, origin)];
};
const releaseCapture = (pointerId: number): void => {
diff --git a/invokeai/frontend/webv2/src/workbench/canvas-engine/viewport.ts b/invokeai/frontend/webv2/src/workbench/canvas-engine/viewport.ts
index c5d5600a5f..4a76ec1159 100644
--- a/invokeai/frontend/webv2/src/workbench/canvas-engine/viewport.ts
+++ b/invokeai/frontend/webv2/src/workbench/canvas-engine/viewport.ts
@@ -29,7 +29,6 @@
import type { Mat2d, Rect, Vec2 } from '@workbench/canvas-engine/types';
-import { applyToPoint, invert } from '@workbench/canvas-engine/math/mat2d';
import { clampZoom, snapZoom } from '@workbench/canvas-engine/math/snapping';
import {
panBy as calculatePanBy,
@@ -102,17 +101,18 @@ export const createViewport = (initial?: Partial<ViewState>): Viewport => {
}
};
- const cssMatrix = (): Mat2d => ({ a: zoom, b: 0, c: 0, d: zoom, e: pan.x, f: pan.y });
-
const documentToScreen = (p: Vec2): Vec2 => ({ x: zoom * p.x + pan.x, y: zoom * p.y + pan.y });
- const screenToDocument = (p: Vec2): Vec2 => {
- const inverse = invert(cssMatrix());
- if (!inverse) {
- return { x: 0, y: 0 };
- }
- return applyToPoint(inverse, p);
- };
+ /**
+ * Inverts the CSS view transform (`scale(zoom)` then `translate(pan)`)
+ * directly rather than through a general matrix inverse: the view is only ever
+ * a uniform scale plus a translation, so the inverse is one subtraction and
+ * one division. This runs once per coalesced
+ * pointer sample — a brush batch can carry dozens — which is enough for the
+ * matrix allocation to be worth skipping.
+ */
+ const screenToDocument = (p: Vec2): Vec2 =>
+ zoom === 0 ? { x: 0, y: 0 } : { x: (p.x - pan.x) / zoom, y: (p.y - pan.y) / zoom };
const zoomAtPoint = (newZoom: number, screenAnchor: Vec2): void => {
const next = calculateZoomAtPoint({ pan, zoom }, newZoom, screenAnchor, clampZoom);