Skip to content

armgit / cua

Public Imported from GitHub
0

Scale computer-use 2.0 with open-source drivers, cross-OS sandboxes, and benchmarks for training, evaluation, and data generation.

perf(spaces-web): picking a Space redraws one tile, not the grid; a scale benchmark (#6065)

ddupontd038071ee3ae
diff --git a/apps/cua-spaces-web/package.json b/apps/cua-spaces-web/package.json
index a767a4c178..1f5e785e8c 100644
--- a/apps/cua-spaces-web/package.json
+++ b/apps/cua-spaces-web/package.json
@@ -18,7 +18,8 @@
"parity": "node scripts/build-core-wasm.mjs && playwright test -c e2e/playwright.config.ts",
"parity:electron": "node scripts/build-core-wasm.mjs && VITE_CUA_FLAG_SPACES_REDESIGN=${VITE_CUA_FLAG_SPACES_REDESIGN:-1} vite build && (cd ../cua-spaces-desktop && rm -rf dist-electron && npx tsdown && node scripts/check-channels.mjs) && PARITY_HOST=electron playwright test -c e2e/playwright.config.ts",
"screenshots": "node scripts/screenshots.mjs",
- "perf": "node scripts/perf.mjs"
+ "perf": "node scripts/perf.mjs",
+ "scale": "node scripts/scale-bench.mjs"
},
"dependencies": {
"@base-ui/react": "^1.8.0",
diff --git a/apps/cua-spaces-web/scale-budget.json b/apps/cua-spaces-web/scale-budget.json
new file mode 100644
index 0000000000..58769698df
--- /dev/null
+++ b/apps/cua-spaces-web/scale-budget.json
@@ -0,0 +1,23 @@
+{
+ "100": {
+ "firstTile": 1380,
+ "filterKey": 50,
+ "selectAll": 50,
+ "pickOne": 50,
+ "longTasks": 147
+ },
+ "1000": {
+ "firstTile": 1479,
+ "filterKey": 114,
+ "selectAll": 236,
+ "pickOne": 50,
+ "longTasks": 720
+ },
+ "3000": {
+ "firstTile": 2373,
+ "filterKey": 407,
+ "selectAll": 711,
+ "pickOne": 156,
+ "longTasks": 1668
+ }
+}
diff --git a/apps/cua-spaces-web/scripts/scale-bench.mjs b/apps/cua-spaces-web/scripts/scale-bench.mjs
new file mode 100644
index 0000000000..a01cbed436
--- /dev/null
+++ b/apps/cua-spaces-web/scripts/scale-bench.mjs
@@ -0,0 +1,150 @@
+// SPDX-License-Identifier: FSL-1.1-MIT
+// Copyright (c) 2026 Cua AI, Inc.
+
+// The Spaces page at scale: the production build in headless Chromium with
+// the demo host listing N more Spaces (`?demo=scale-N`). For each N it
+// reports, as the median of RUNS loads:
+// firstTile navigation start to the first Space tile drawn
+// filterKey one character typed in the filter to the grid redrawn
+// selectAll ⌘A (select every Space shown) to the selection bar drawn
+// pickOne one Space's checkbox clicked to the selection bar drawn
+// longTasks main-thread tasks over 50 ms while the page settles, total ms
+// and fails when a number is over its budget in scale-budget.json.
+//
+// Usage:
+// node scripts/scale-bench.mjs build, measure, check
+// node scripts/scale-bench.mjs --no-build reuse dist/
+// node scripts/scale-bench.mjs --sizes 100,1000
+// node scripts/scale-bench.mjs --update write budgets: current numbers plus 50%
+import { spawn, spawnSync } from "node:child_process";
+import { existsSync, readFileSync, writeFileSync } from "node:fs";
+import { fileURLToPath } from "node:url";
+
+const root = fileURLToPath(new URL("..", import.meta.url));
+const argv = process.argv.slice(2);
+const has = (f) => argv.includes(f);
+const opt = (f, d) => (argv.includes(f) ? argv[argv.indexOf(f) + 1] : d);
+const SIZES = opt("--sizes", "100,500,1000").split(",").map(Number);
+const RUNS = Number(opt("--runs", "3"));
+const PORT = Number(opt("--port", "5299"));
+const budgetFile = `${root}scale-budget.json`;
+
+if (!has("--no-build")) {
+ for (const [cmd, a] of [
+ ["node", ["scripts/build-core-wasm.mjs"]],
+ ["npx", ["vite", "build", "--logLevel", "warn"]],
+ ]) {
+ const r = spawnSync(cmd, a, { cwd: root, stdio: ["ignore", "ignore", "inherit"], env: { ...process.env, VITE_CUA_FLAG_SPACES_REDESIGN: "1" } });
+ if (r.status !== 0) process.exit(r.status ?? 1);
+ }
+}
+
+const server = spawn("npx", ["vite", "preview", "--port", String(PORT), "--strictPort"], { cwd: root, stdio: "ignore" });
+const stop = () => server.kill("SIGTERM");
+process.on("exit", stop);
+for (let i = 0; i < 100; i++) {
+ try {
+ if ((await fetch(`http://localhost:${PORT}/`)).ok) break;
+ } catch {}
+ await new Promise((r) => setTimeout(r, 200));
+}
+
+const { chromium } = await import("@playwright/test");
+const browser = await chromium.launch();
+const median = (xs) => [...xs].sort((a, b) => a - b)[Math.floor(xs.length / 2)];
+
+async function once(n) {
+ const page = await browser.newPage({ viewport: { width: 1440, height: 900 } });
+ await page.addInitScript(() => {
+ window.__long = 0;
+ new PerformanceObserver((l) => l.getEntries().forEach((e) => (window.__long += e.duration))).observe({ type: "longtask", buffered: true });
+ });
+ await page.goto(`http://localhost:${PORT}/spaces?bridge=demo&demo=scale-${n}`);
+ await page.locator("[data-space-id]").first().waitFor({ timeout: 60_000 });
+ const firstTile = await page.evaluate(() => performance.now());
+ // Settled: no long task for a second.
+ await page.waitForTimeout(1500);
+ const longTasks = await page.evaluate(() => window.__long);
+ const total = await page.locator("[data-space-id]").count();
+
+ // One character in the filter, to the grid redrawn with fewer tiles.
+ const filter = page.locator("[data-spaces-filter]").first();
+ await filter.click();
+ const filterKey = await page.evaluate(
+ () =>
+ new Promise((resolve) => {
+ const before = document.querySelectorAll("[data-space-id]").length;
+ const input = document.activeElement;
+ const t0 = performance.now();
+ const obs = new MutationObserver(() => {
+ if (document.querySelectorAll("[data-space-id]").length !== before) {
+ obs.disconnect();
+ requestAnimationFrame(() => resolve(performance.now() - t0));
+ }
+ });
+ obs.observe(document.body, { childList: true, subtree: true });
+ const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value").set;
+ setter.call(input, "scale-00");
+ input.dispatchEvent(new Event("input", { bubbles: true }));
+ setTimeout(() => resolve(-1), 10_000);
+ }),
+ );
+ await filter.fill("");
+ await page.waitForTimeout(500);
+
+ // ⌘A on the grid: every Space shown picked, to the selection bar drawn.
+ await page.locator("main").first().click({ position: { x: 5, y: 5 } }).catch(() => {});
+ const t0 = await page.evaluate(() => performance.now());
+ await page.keyboard.press("Meta+a");
+ await page.locator("[data-selection-bar], [data-picked-count]").first().waitFor({ timeout: 10_000 }).catch(() => {});
+ const selectAll = (await page.evaluate(() => performance.now())) - t0;
+ await page.keyboard.press("Escape");
+ await page.waitForTimeout(500);
+
+ // Picking one Space (its checkbox), to the selection bar drawn.
+ const pickOne = await page.evaluate(
+ () =>
+ new Promise((resolve) => {
+ const box = document.querySelectorAll("[data-select]")[5];
+ const t0 = performance.now();
+ const obs = new MutationObserver(() => {
+ if (document.querySelector("[data-selection-bar]")) {
+ obs.disconnect();
+ requestAnimationFrame(() => resolve(performance.now() - t0));
+ }
+ });
+ obs.observe(document.body, { childList: true, subtree: true });
+ box.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ setTimeout(() => resolve(-1), 10_000);
+ }),
+ );
+ await page.close();
+ return { firstTile, filterKey, selectAll, pickOne, longTasks, total };
+}
+
+const results = {};
+for (const n of SIZES) {
+ const runs = [];
+ for (let i = 0; i < RUNS; i++) runs.push(await once(n));
+ results[n] = Object.fromEntries(["firstTile", "filterKey", "selectAll", "pickOne", "longTasks"].map((k) => [k, Math.round(median(runs.map((r) => r[k])))]));
+ results[n].tiles = runs[0].total;
+ console.log(`N=${n}`, JSON.stringify(results[n]));
+}
+await browser.close();
+stop();
+
+if (has("--update")) {
+ const budget = Object.fromEntries(Object.entries(results).map(([n, r]) => [n, Object.fromEntries(Object.entries(r).filter(([k]) => k !== "tiles").map(([k, v]) => [k, Math.max(50, Math.round(v * 1.5))]))]));
+ writeFileSync(budgetFile, JSON.stringify(budget, null, 2) + "\n");
+ console.log(`wrote ${budgetFile}`);
+} else if (existsSync(budgetFile)) {
+ const budget = JSON.parse(readFileSync(budgetFile, "utf8"));
+ const over = [];
+ for (const [n, r] of Object.entries(results))
+ for (const [k, v] of Object.entries(budget[n] ?? {})) if (r[k] > v) over.push(`N=${n} ${k} ${r[k]} > ${v}`);
+ if (over.length) {
+ console.error(`over budget:\n ${over.join("\n ")}`);
+ process.exit(1);
+ }
+ console.log("within budget");
+}
diff --git a/apps/cua-spaces-web/src/__tests__/spaces-grid-redraws.test.tsx b/apps/cua-spaces-web/src/__tests__/spaces-grid-redraws.test.tsx
new file mode 100644
index 0000000000..d3bfa8b017
--- /dev/null
+++ b/apps/cua-spaces-web/src/__tests__/spaces-grid-redraws.test.tsx
@@ -0,0 +1,44 @@
+// SPDX-License-Identifier: FSL-1.1-MIT
+// Copyright (c) 2026 Cua AI, Inc.
+
+// The Spaces grid at scale: picking one Space redraws that tile, not the
+// grid. With 3,000 Spaces every pick redrew 3,000 tiles (about 480 ms on a
+// laptop; scripts/scale-bench.mjs measures it in Chromium). Counts each
+// tile's draws on the real routes, the wasm core and the demo host.
+
+import { act, cleanup, fireEvent, waitFor } from "@testing-library/react";
+import { afterEach, describe, expect, it, vi } from "vitest";
+import { wasmBuilt } from "@/bridge/__tests__/testCore";
+import { mountApp, stubBrowserApis } from "./app-harness";
+
+const draws = new Map<string, number>();
+vi.mock("@/components/spaces/space-tile", async (actual) => {
+ const real = await actual<typeof import("@/components/spaces/space-tile")>();
+ return {
+ ...real,
+ SpaceTile: (props: Parameters<typeof real.SpaceTile>[0]) => {
+ draws.set(props.item.space.id, (draws.get(props.item.space.id) ?? 0) + 1);
+ return real.SpaceTile(props);
+ },
+ };
+});
+
+afterEach(cleanup);
+stubBrowserApis();
+
+describe.skipIf(!wasmBuilt)("the Spaces grid", () => {
+ it("redraws only the tile picked, and the first pick doesn't redraw the rest", { timeout: 30_000 }, async () => {
+ await mountApp("/spaces", { failsCreates: false, listsGhost: false, demo: { extraSpaces: 60 } });
+ await waitFor(() => expect(document.querySelectorAll("[data-space-id]").length).toBeGreaterThan(60));
+ // Let the page settle (thumbnails, resources, the first-run reads).
+ await act(() => new Promise((r) => setTimeout(r, 800)));
+ draws.clear();
+ const box = document.querySelector<HTMLElement>('[data-select="local:scale-7"]')!;
+ fireEvent.click(box);
+ await waitFor(() => expect(document.querySelector("[data-selection-bar]")).not.toBeNull());
+ expect(document.querySelector('[data-select="local:scale-7"]')?.getAttribute("aria-checked")).toBe("true");
+ const redrawn = [...draws.keys()];
+ expect(redrawn).toContain("local:scale-7");
+ expect(redrawn.length, `redrew ${redrawn.length} tiles: ${redrawn.slice(0, 8).join(", ")}…`).toBeLessThanOrEqual(3);
+ });
+});
diff --git a/apps/cua-spaces-web/src/bridge/adapters/demo.ts b/apps/cua-spaces-web/src/bridge/adapters/demo.ts
index b8a95ec36f..fcccdc2fc3 100644
--- a/apps/cua-spaces-web/src/bridge/adapters/demo.ts
+++ b/apps/cua-spaces-web/src/bridge/adapters/demo.ts
@@ -74,6 +74,9 @@ export interface DemoOptions {
noVault?: boolean;
/** Start with no Spaces (the first-Space empty state). */
noSpaces?: boolean;
+ /** This many more Spaces on the list, for scale (`?demo=scale-1000`;
+ * scripts/scale-bench.mjs). */
+ extraSpaces?: number;
/** An account in no team (`?demo=no-team`): Share explains that sharing is for teams. */
noTeam?: boolean;
/** A team with no free computers (`?demo=team-full`): adding a machine to it is refused, as the control plane does at one computer per seat. Simulated: the demo has no control plane. */
@@ -164,6 +167,31 @@ const DEMO_NOTES = "https://github.com/trycua/cua/releases/tag/cua-spaces-v0.9.3
* Space and the first Space offers see it too), and `?demo=intel-mac` /
* `?demo=linux-pc` (an Intel Mac, an x64 Linux PC with QEMU), and
* `?demo=creating-elsewhere` (the Mac mini is creating a Space). */
+/** `count` plain Spaces for scale: alternating systems, places and states,
+ * each with its own id, name and time (`?demo=scale-N`). */
+export function demoScaleRows(now: number, count: number): SpaceRow[] {
+ const oses = ["linux", "macos", "windows"] as const;
+ return Array.from({ length: count }, (_, i) => {
+ const os = oses[i % 3]!;
+ const on = i % 4 !== 0;
+ return {
+ id: `local:scale-${i}`,
+ name: `scale-${String(i).padStart(4, "0")}`,
+ provider: "local",
+ spacesdVersion: "0.6.0",
+ features: [],
+ addedAt: new Date(now - i * 60_000).toISOString(),
+ os,
+ osName: os === "linux" ? "Ubuntu" : os === "macos" ? "macOS" : "Windows",
+ kind: os === "macos" ? "vm" : "container",
+ arch: "arm64",
+ reachable: true,
+ power: "suspend",
+ powerState: on ? "running" : "stopped",
+ } as SpaceRow;
+ });
+}
+
export function demoOptionsFromSearch(search: string): DemoOptions {
const flags = new Set((new URLSearchParams(search).get("demo") ?? "").split(",").filter(Boolean));
return {
@@ -174,6 +202,10 @@ export function demoOptionsFromSearch(search: string): DemoOptions {
keyvaultLocked: flags.has("locked"),
noVault: flags.has("novault"),
noSpaces: flags.has("empty"),
+ ...(() => {
+ const scale = [...flags].find((f) => /^scale-\d+$/.test(f));
+ return scale ? { extraSpaces: Math.min(5000, Number(scale.slice(6))) } : {};
+ })(),
noTeam: flags.has("no-team"),
teamFull: flags.has("team-full"),
noThread: flags.has("nothread"),
@@ -263,7 +295,7 @@ export function createDemoAdapter(options: DemoOptions = {}): DataAdapter & { re
const timers = new Set<ReturnType<typeof setTimeout>>();
const state: DemoState = {
- rows: options.noSpaces ? [] : demoSpaceRows(now()),
+ rows: options.noSpaces ? [] : [...demoSpaceRows(now()), ...demoScaleRows(now(), options.extraSpaces ?? 0)],
items: demoKeyvaultItems(now()),
keyvaultLocked: Boolean(options.keyvaultLocked),
keyvaultDisabled: false,
diff --git a/apps/cua-spaces-web/src/components/spaces/selection.tsx b/apps/cua-spaces-web/src/components/spaces/selection.tsx
index 52cc750393..b0abc89acd 100644
--- a/apps/cua-spaces-web/src/components/spaces/selection.tsx
+++ b/apps/cua-spaces-web/src/components/spaces/selection.tsx
@@ -117,8 +117,22 @@ export function useSelection(shown: readonly SpaceItem[], paused: boolean): Sele
}
/** A tile's or row's checkbox. Over a picture it sits on a dark scrim; in the list it is plain. */
-export function SelectBox({ item, selection, onPicture = false, className }: { item: SpaceItem; selection: Selection; onPicture?: boolean; className?: string }) {
- const on = selection.has(item.space.id);
+export function SelectBox({
+ item,
+ selection,
+ picked,
+ onPicture = false,
+ className,
+}: {
+ item: SpaceItem;
+ selection: Selection;
+ /** Whether it is picked, when the caller already knows (a memoized grid
+ * tile passes it: its `selection` stays the same object across picks). */
+ picked?: boolean;
+ onPicture?: boolean;
+ className?: string;
+}) {
+ const on = picked ?? selection.has(item.space.id);
const can = selectable(item);
const why = can || on ? null : unselectableReason(item);
return (
diff --git a/apps/cua-spaces-web/src/routes/spaces/index.tsx b/apps/cua-spaces-web/src/routes/spaces/index.tsx
index 2fb546799e..88d1d710d9 100644
--- a/apps/cua-spaces-web/src/routes/spaces/index.tsx
+++ b/apps/cua-spaces-web/src/routes/spaces/index.tsx
@@ -3,7 +3,7 @@
import { createFileRoute } from "@tanstack/react-router";
import { MonitorIcon, PlusIcon, SearchIcon } from "lucide-react";
-import { useEffect, useMemo, useRef, useState } from "react";
+import { memo, useEffect, useMemo, useRef, useState, type RefObject } from "react";
import { emptyHome, listView, spaceDiskBytes, useBridge, useResources, useFirstSpaceOffers, useMachines, useNewSpaceWizard, useSpaceAccess, useSpaces, type Space } from "@/bridge";
import { EmptyHome } from "@/components/empty-home";
@@ -46,7 +46,8 @@ function SpacesPage() {
void navigate({ search: (prev: SpacesSearch) => nextSearch(prev, next), replace: true });
const { data: all = [], isLoading, error, listNotice, listStatus, startSpace, stopSpace, creatingElsewhere } = useSpaces();
// Machines' own desktops are on Machines, not here.
- const spaces = realSpaces(all);
+ // Stable while the list is: the tiles below redraw only when their own item changes.
+ const spaces = useMemo(() => realSpaces(all), [all]);
const { data: machines = [] } = useMachines();
const resources = useResources();
const [query, setQuery] = useState("");
@@ -116,6 +117,11 @@ function SpacesPage() {
for (const it of list) void (on ? startSpace : stopSpace)(it.space.id).catch(toastError(`Couldn't turn ${on ? "on" : "off"} ${it.space.name}`));
};
const actions: MenuActions = { power: (it, on) => power([it], on), remove: (it) => bulk.ask([it]) };
+ // The grid's tiles read the selection and the menus' actions through this,
+ // so a tile redraws only when its own Space, pick or state changes (picking
+ // one of 3,000 Spaces redrew all 3,000).
+ const live = useRef<GridLive>({ selection, actions });
+ live.current = { selection, actions };
// `?select=stopped` (the cleanup tip, a link): every stopped Space shown of yours, once the list is in.
const ready = !isLoading && shown.length > 0;
useEffect(() => {
@@ -244,30 +250,17 @@ function SpacesPage() {
/>
</>
) : (
- <ul className="grid grid-cols-[repeat(auto-fill,minmax(232px,1fr))] gap-x-5 gap-y-7">
+ <ul data-picking={selection.ids.size > 0 || undefined} className="group/grid grid grid-cols-[repeat(auto-fill,minmax(232px,1fr))] gap-x-5 gap-y-7">
{elsewhereTiles}
{shown.map((it) => (
- // Off-screen tiles skip layout and paint until they scroll in (with room for the selected ring).
- <li key={it.space.id} className={TILE_CELL}>
- <SpaceContextMenu item={it} actions={actions}>
- <SpaceTile
- item={it}
- accessKey={keyvault.signedIn.has(it.space.id) ? keyvault.accessKey(it.space.id) : undefined}
- selected={selection.has(it.space.id)}
- busy={bulk.running.has(it.space.id)}
- onClick={(e) => selection.onClick(it, e)}
- topLeft={
- <SelectBox
- item={it}
- selection={selection}
- onPicture
- className={selection.ids.size ? undefined : "opacity-0 group-focus-within/tile:opacity-100 group-hover/tile:opacity-100"}
- />
- }
- more={<SpaceMoreMenu item={it} actions={actions} />}
- />
- </SpaceContextMenu>
- </li>
+ <GridTile
+ key={it.space.id}
+ item={it}
+ selected={selection.has(it.space.id)}
+ busy={bulk.running.has(it.space.id)}
+ accessKey={keyvault.signedIn.has(it.space.id) ? keyvault.accessKey(it.space.id) : undefined}
+ live={live}
+ />
))}
</ul>
);
@@ -289,3 +282,60 @@ function SpacesPage() {
function SpacesPageSwitch() {
return useFlag("spaces_redesign") ? <SpacesPage /> : <LegacySpacesPage />;
}
+
+interface GridLive {
+ selection: ReturnType<typeof useSelection>;
+ actions: MenuActions;
+}
+
+/** One tile of the grid, redrawn only when its own props change (`live` is a
+ * stable ref to what every tile shares). */
+const GridTile = memo(function GridTile({
+ item,
+ selected,
+ busy,
+ accessKey,
+ live,
+}: {
+ item: SpaceItem;
+ selected: boolean;
+ busy: boolean;
+ accessKey: string | null | undefined;
+ live: RefObject<GridLive>;
+}) {
+ const actions = useMemo<MenuActions>(
+ () => ({ power: (it, on) => live.current.actions.power(it, on), remove: (it) => live.current.actions.remove(it) }),
+ [live],
+ );
+ // The checkbox reads whether it is picked and toggles it (the latest selection).
+ const selection = useMemo(
+ () => ({ ...live.current.selection, has: (id: string) => live.current.selection.has(id), toggle: (it: SpaceItem, e?: { shiftKey?: boolean }) => live.current.selection.toggle(it, e) }),
+ [live],
+ );
+ return (
+ // Off-screen tiles skip layout and paint until they scroll in (with room for the selected ring).
+ <li className={TILE_CELL}>
+ <SpaceContextMenu item={item} actions={actions}>
+ <SpaceTile
+ item={item}
+ accessKey={accessKey}
+ selected={selected}
+ busy={busy}
+ onClick={(e) => live.current.selection.onClick(item, e)}
+ topLeft={
+ <SelectBox
+ item={item}
+ selection={selection}
+ picked={selected}
+ onPicture
+ // Hidden until hovered, unless something is picked (the grid's `data-picking`): a
+ // CSS group, so the first pick doesn't redraw every tile.
+ className="opacity-0 group-focus-within/tile:opacity-100 group-hover/tile:opacity-100 group-data-[picking]/grid:opacity-100"
+ />
+ }
+ more={<SpaceMoreMenu item={item} actions={actions} />}
+ />
+ </SpaceContextMenu>
+ </li>
+ );
+});