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.

Merge remote-tracking branch 'origin/main' into perf/image-map-zoom

Lincoln Steinb558f56cf64a
diff --cc invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts
index c10beecb32,a802181533..d784624495
--- a/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts
+++ b/invokeai/frontend/webv2/src/workbench/image-map/imageMapTraces.ts
@@@ -4,13 -4,11 +4,11 @@@ import type { Layout } from 'plotly.js'
import type { ImageMapPoint } from './api';
import type { AxisRanges } from './imageMapViewport';
-import { getClusterColor } from './clusterPalette';
+import { CLUSTER_PALETTE, getClusterColor } from './clusterPalette';
/**
- * Pure trace/layout builders for the map, kept apart from the plotly host so
- * the math is testable without WebGL. Trace identity follows PhotoMapAI:
- * named traces in a fixed z-order, with "Current Image" always last so the
- * gold marker renders on top.
+ * Pure WebGL-independent trace builders follow PhotoMapAI's fixed named order, placing Current Image last so gold
+ * markers render above other traces.
*/
export const ALL_POINTS_TRACE = 'All Points';
@@@ -43,110 -41,27 +41,106 @@@ export interface ScatterTrace
const IMAGE_SYMBOL = 'circle';
const VIDEO_SYMBOL = 'diamond';
-export const buildAllPointsTrace = (points: ImageMapPoint[]): ScatterTrace => ({
- // The gallery's item key, so a click or hover resolves back to the kind the
- // point stands for — plotly carries strings, and a bare name would not say
- // whether it names an image or a video.
- customdata: points.map((point) => point.key),
- hoverinfo: 'none',
- marker: {
- color: points.map((point) => getClusterColor(point.cluster)),
- opacity: points.map((point) => (point.cluster < 0 ? NOISE_OPACITY : POINT_OPACITY)),
- size: 5,
- // Diamonds identify videos independently of cluster color without requiring hover.
- symbol: points.map((point) => (point.item.kind === 'video' ? VIDEO_SYMBOL : IMAGE_SYMBOL)),
- },
- mode: 'markers',
- name: ALL_POINTS_TRACE,
- type: 'scattergl',
- x: points.map((point) => point.x),
- y: points.map((point) => point.y),
-});
+/**
+ * The base points, split into one trace per distinct appearance.
+ *
+ * A single trace carrying per-point `color`/`opacity`/`symbol` arrays is what
+ * made zooming a large map slow: plotly reprocesses every one of those arrays
+ * on each relayout, and a zoom is a relayout per frame. Splitting the points
+ * so every marker property is scalar cuts a zoom step by roughly 14x at 170k
+ * points and 20x at 300k, and builds the scene about twice as fast while
+ * retaining half the heap.
+ *
+ * Each point keeps the appearance it had, but the ORDER changes: points now
+ * paint grouped rather than in gallery order, so where two points overlap the
+ * one on top can differ. Markers are translucent, so that is visible. Noise
+ * is emitted first, which is the deliberate part — dimmed points belong under
+ * the clustered ones. Within a group the original point order is preserved.
+ *
+ * The cost is hover. Plotly builds a kd-tree for hit-testing only on traces
+ * of 100k points or more, so one 170k trace had one and none of these do:
+ * hover goes from ~1.5ms to ~7ms at 170k and ~11ms at 300k, throttled to
+ * 20/s. Zoom was 180ms+ at those sizes, so this trades a cost nobody could
+ * work through for one that is merely warm.
+ *
+ * At most (palette + noise) x (image, video) groups exist, so the trace count
+ * is bounded by the palette rather than by the gallery. Below roughly 5k
+ * points the per-trace overhead makes this a small net loss (~2.5ms a frame
+ * at 200 points) — accepted rather than switched on a threshold, because
+ * making the draw order depend on gallery size is a worse bargain than the
+ * milliseconds.
+ */
+export const buildAllPointsTraces = (points: ImageMapPoint[]): ScatterTrace[] => {
+ // Bucketed by a small integer rather than a composed string key: this runs
+ // over every point on each data change, and building a key per point is the
+ // one part of it that would allocate.
+ const buckets: ImageMapPoint[][] = [];
+
+ for (const point of points) {
+ const cluster = clusterOf(point);
+ const colorSlot = cluster < 0 ? CLUSTER_PALETTE.length : cluster % CLUSTER_PALETTE.length;
+ const slot = colorSlot * 2 + (point.item.kind === 'video' ? 1 : 0);
+
+ (buckets[slot] ??= []).push(point);
+ }
+
+ const traces: ScatterTrace[] = [];
+ // Noise occupies the slots just past the palette, and emitting it first is
+ // what puts the dimmed points underneath.
+ const order = [...buckets.keys()].sort(
+ (left, right) => Number(right >= CLUSTER_PALETTE.length * 2) - Number(left >= CLUSTER_PALETTE.length * 2)
+ );
+
+ for (const slot of order) {
+ const group = buckets[slot];
+
+ if (group === undefined || group.length === 0) {
+ continue;
+ }
+
+ const first = group[0]!;
+ const cluster = clusterOf(first);
+
+ traces.push({
+ // The gallery's item key, so a click or hover resolves back to the kind
+ // the point stands for — plotly carries strings, and a bare name would
+ // not say whether it names an image or a video.
+ customdata: group.map((point) => point.key),
+ hoverinfo: 'none',
+ marker: {
+ color: getClusterColor(cluster),
+ opacity: cluster < 0 ? NOISE_OPACITY : POINT_OPACITY,
+ size: 5,
+ // Videos are diamonds. Color already carries the cluster, so kind
+ // needs the one remaining channel: without it a clip is
+ // pixel-identical to an image and can only be found by hovering
+ // points one at a time.
+ symbol: first.item.kind === 'video' ? VIDEO_SYMBOL : IMAGE_SYMBOL,
+ },
+ mode: 'markers',
+ name: ALL_POINTS_TRACE,
+ type: 'scattergl',
+ x: group.map((point) => point.x),
+ y: group.map((point) => point.y),
+ });
+ }
+
+ return traces;
+};
+
+/**
+ * The point's DBSCAN label, or noise for anything that cannot be one.
+ *
+ * The bucket slot below is an array index, so a non-integer label would write
+ * a string property that `buckets.keys()` never yields — those points would
+ * silently never reach a trace, and a fractional one would collide with
+ * another cluster's slot and take its colour. The endpoint declares `int` and
+ * the client does not validate the body, so this is the guard that keeps a
+ * contract slip from quietly deleting part of the map.
+ */
+const clusterOf = (point: ImageMapPoint): number => (Number.isInteger(point.cluster) ? point.cluster : -1);
- /**
- * The gallery's multi-selection (e.g. a cluster click), drawn larger with a
- * white outline over the base points. Empty when fewer than two items are
- * selected — a single selection is already marked by the gold target.
- */
+ /** White-outlined enlarged multi-selection trace; fewer than two items need only the gold current target. */
export const buildHighlightedPointsTrace = (
points: ImageMapPoint[],
selectedKeys: ReadonlySet<GalleryItemKey>