Repository navigation
Description
Context
The graph's scene effect builds the graph and then runs ForceAtlas2 synchronously inside a React effect (forceAtlas2.assign, packages/ui-react/src/components/graph/graph-canvas.tsx:485-491). The effect itself spans :414-532. The iteration budget is 300 for up to 150 nodes, 150 for up to 500 and 60 above that (forceIterations, same file:103-108). Measured during the review on desktop, the call took 41 ms at 100 nodes and 469 ms at 500 nodes. On a phone that is one task of one to two seconds, with input frozen while the graph opens or changes scene.
A library cannot name a worker script's URL itself. new URL(…, import.meta.url) inside a dependency does not survive Vite's dependency pre-bundling, which is why pdf.js receives its worker URL from the shell (packages/ui-react/src/lib/pdf.ts:1-22; pdfWorkerUrl, packages/ui-react/src/config.ts:50-56; README packages/ui-react/README.md:326-340). An empty URL means pdf.js parses on the main thread. This task follows the same precedent.
The canvas file comment declares it the only module that imports sigma or graphology (packages/ui-react/src/components/graph/graph-canvas.tsx:2-10). A worker module that imports the layout package widens that seam, so the comment must name it.
Guide performance/performance: "> 250ms: Offload to a Web Worker." Guide performance/break-up-long-tasks: "You MUST implement a fallback using setTimeout to ensure code executes without breaking."
Scope
forceAtlas2.assignis three steps (abstractSynchronousLayoutin graphology-layout-forceatlas2 0.10.1index.js): buildFloat32Arraynode and edge matrices (helpers.graphToByteArrays), calliterate(settings, nodes, edges)N times, and write the result back (helpers.assignLayoutChanges). Keep the first and last steps on the main thread, and move only the iteration loop. Version 0.10.1 has noexportsmap, sographology-layout-forceatlas2/iterate.jsand/helpers.jsare importable. Check this against the installed version before relying on it.- Add a module worker entry, exported as a new subpath, that receives the settings, the iteration count and the two matrices (transferred). It runs the package's
iteratethe same number of times and transfers the node matrix back. The worker does not import graphology itself. - Add a
graphLayoutWorkerUrlconfig key next topdfWorkerUrl(default""). Document it in the README, with the?urlimport as for pdf.js. - When a URL is configured and the worker starts,
GraphCanvassends the force layout to it and applies the returned positions. The separation pass, camera reset and refresh then run as they do today. - When no URL is configured, or the worker fails to start or errors, run the same
iteratecalls on the main thread in batches of about 10, on one persistent pair of matrices. Callingassignrepeatedly would reset each node's adaptive speed state and change the output. Yield between batches withscheduler.yield()when it exists, otherwisesetTimeout(0). - A newer scene or unmount cancels a pending layout (terminate or ignore the stale reply), so stale positions never land.
- Update the seam comment to name the worker module.
Out of scope
- graphology's continuous
FA2LayoutSupervisor, or any animated or live layout. - Changing the iteration budget, settings, seed positions or the radial and fixed layouts.
- Wiring the worker URL in the hosting template. That needs a separate issue in
schlessera/brain-hosting-templatecarrying theupstream:label; this repo ships the building block. - Moving the separation pass (
separateDisplayedNodes) off the main thread.
Acceptance criteria
- For a fixed 500-node fixture, positions from the worker path and from the batched fallback both equal the current synchronous
assignoutput exactly (the sameFloat32Arrayarithmetic). A unit test checks both paths against a stored golden made from the current code before the change. - Real-browser proof in Chromium: with the worker URL configured, opening a 500-node force graph records no main-thread long task over 50 ms attributable to layout (
PerformanceObserverforlongtaskorlong-animation-frame). The test fails on the tree before the change. - With no URL configured, the same browser test records the layout as slices of under 50 ms each.
- A scene change while a layout is pending applies only the newest scene's positions (unit test with a controllable fake worker).
- Android support: module workers and transferable
Float32Arraybuffers exist in both Chromium targets and in Firefox for Android.scheduler.yieldis feature-detected, withsetTimeout(0)as the named fallback. No polyfill is added. -
docs/integration-contract/package-api.mdlists the new subpath and config key as an additive change.
Verification
bun run test packages/ui-react/tests # golden + cancellation tests
node scripts/visual.mjs --project=ui-react-layout --filter=graph
bun run typecheck
bun run lintContract impact
- This touches the integration contract, so it needs the
contractlabel, aCONTRACT:commit prefix, and the doc updated in the same commit. - This changes a published package's behaviour, so it needs a changeset.
Additive: a new package subpath and a new optional config key. It ships in a minor.
Activity