Skip to content

ui-react: force graph layout blocks the main thread for up to half a second #2591

Open

Description

@schlessera

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.assign is three steps (abstractSynchronousLayout in graphology-layout-forceatlas2 0.10.1 index.js): build Float32Array node and edge matrices (helpers.graphToByteArrays), call iterate(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 no exports map, so graphology-layout-forceatlas2/iterate.js and /helpers.js are 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 iterate the same number of times and transfers the node matrix back. The worker does not import graphology itself.
  • Add a graphLayoutWorkerUrl config key next to pdfWorkerUrl (default ""). Document it in the README, with the ?url import as for pdf.js.
  • When a URL is configured and the worker starts, GraphCanvas sends 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 iterate calls on the main thread in batches of about 10, on one persistent pair of matrices. Calling assign repeatedly would reset each node's adaptive speed state and change the output. Yield between batches with scheduler.yield() when it exists, otherwise setTimeout(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-template carrying the upstream: 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 assign output exactly (the same Float32Array arithmetic). 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 (PerformanceObserver for longtask or long-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 Float32Array buffers exist in both Chromium targets and in Firefox for Android. scheduler.yield is feature-detected, with setTimeout(0) as the named fallback. No polyfill is added.
  • docs/integration-contract/package-api.md lists 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 lint

Contract impact

  • This touches the integration contract, so it needs the contract label, a CONTRACT: 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

  1. added
    type: fix
    area: ui-react
    priority: p2
    agent-ready
    contract
    upstream: brain-hosting-template
    on Oct 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    agent-readyarea: ui-reactcontractpriority: p2type: fixupstream: brain-hosting-template

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions