Skip to content

feat(layout): choose the engine by an estimate of the layout, refuse one elkjs cannot finish - #270

Merged
endrix merged 2 commits into
mainfrom
feat/layout-estimate
Dec 2, 2026
Merged

endrix merged 2 commits into
mainfrom
feat/layout-estimate

Conversation

@endrix

@endrix endrix commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Stage 8.5b of the mlir-viewer convergence plan, the platform half. The mlir-viewer half follows (its layout-cost.ts keeps what is MLIR's and calls this estimate).

What changes for a user

  • auto chooses the engine by what the layout is estimated to take. A graph goes to the ELK server when elkjs is estimated to need more than a second for it (AUTO_ELKJS_BUDGET_MS). The rule before was 150 nodes or 400 edges, which said little: of two graphs of 150 nodes on the default preset, elkjs lays one out in 0.7 s and the other in 21 s (the server in 0.2 s and 2.5 s).
  • A layout the server did not answer is refused when elkjs cannot finish it in the same time. A timed-out or failed server used to hand any graph to elkjs, on the extension host's thread. Now the layout fails with a LayoutRefusedError when elkjs is estimated to take longer than the server's timeout, or than a minute when the timeout is shorter, and the diagram says so on its status line. A graph elkjs can finish in time still falls back, and with no server at all (no Java, no jar) elkjs takes everything as before.
  • A layout that fails leaves the diagram as it was. The layout commands clear the edge routes before ELK is asked, and Untangled and Free Ports move the ports first. Both are put back when the layout fails, for any reason.
  • Every layout logs its estimate beside the time it took: [dialogram layout] elkjs: 40 nodes, 110 edges in 229 ms (estimated 228 ms).

The estimate (server/layout-cost.ts)

  • A layout is costed pass by pass. A pass is what one container lays out. Its nodes are layered by their longest path and the cost is the sum of the layers each edge spans (layerSpan).
  • The edges of a cycle are turned round as ELK turns them, by its greedy order (Eades, Lin, Smyth). A depth-first search in model order layered a synthesised netlist of 1,225 cells seven times deeper than ELK draws it.
  • A port of a container that an edge of the inside ends on is a node of the pass, as it is to ELK.
  • estimatePassMs turns a costed pass into milliseconds with the rates of an engine. estimateElkLayout does both for an ELK graph, with the rates of the preset its options ask for.
  • A product costs content it has not laid out yet the same way, from its own model, with its own rates (counted: false for edges that only order, orders: false for edges against the flow). That is what mlir-viewer does next.

The rates

  • Measured by test/layout-rates.measure.test.ts (runs only with DIALOGRAM_MEASURE_LAYOUT), fitted by scripts/fit-layout-rates.mjs: least squares on relative error, fewest terms within a tenth of the best fit.
  • 84 generated dataflow networks of 30 to 10,000 nodes, on elkjs 0.10.0 and the shipped server, with the options the platform gives a diagram. Every graph is within a factor of two of its estimate on both engines (test/fixtures/layout-rates/measured.jsonl, held by a test).
  • The options of Untangled and Free Ports over the default preset are a preset of their own (untangled), measured on 24 more graphs: about three times the middle preset.
  • Checked on seven graphs of picorv32 the rates were not fitted to, as written and as synthesised, 40 to 1,225 nodes: the estimate is 0.52 to 1.53 times what elkjs took, 0.40 to 1.36 times the server (checked.jsonl).
graph preset elkjs estimated server estimated
picorv32, source (290 nodes) middle 2,193 ms 1,503 ms 1,007 ms 402 ms
picorv32, synthesised (1,225) fast 2,340 ms 2,408 ms 1,015 ms 550 ms
axi_adapter, source (40) default 229 ms 228 ms 144 ms 145 ms
axi_adapter, synthesised (76) default 1,119 ms 578 ms 239 ms 139 ms
pcpi_div, synthesised (128) default 4,308 ms 4,081 ms 582 ms 507 ms
pcpi_fast_mul, synthesised (55) default 344 ms 528 ms 78 ms 106 ms
pcpi_mul, synthesised (196) middle 322 ms 462 ms 118 ms 110 ms

After the review

  • Routes and ports restored after a failed layout, and the temporary layout options taken off in every handler.
  • The refusal threshold is the server's timeout and no less than a minute (elkjsTakeoverMs).
  • A Free Ports layout is refused at its probe, so the server is asked once and the reader told once.
  • Nodes another algorithm lays out are counted (unpriced), and auto keeps the size rule for them.
  • The untangled preset.

Found on the way, not in this PR

  • The ELK server never answers a deep graph on a warm JVM: NetworkSimplex.tightTreeDFS overflows the stack, the server logs Error: null and leaves the request open, so the client waits out the whole timeout. -Xss64m cures it (10,000 and 20,000 nodes laid out). A follow-up PR.
  • The setting descriptions of ac-ide, chisel-ide, sv-ide and vhdl-ide still say "past 150 nodes or 400 edges".

Checks

  • packages/diagram-server: 148 files, 1,146 tests pass. Type check clean. scripts/check-neutrality.sh passes.

https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY

…use one elkjs cannot finish

A layered layout is costed before it runs (`server/layout-cost.ts`). A pass
is what one container lays out: its nodes are layered by their longest path,
as ELK layers them, and the cost is the sum of the layers each edge spans.
Rates measured on the platform's own presets turn it into milliseconds for
elkjs and for the ELK server.

- `auto` sends a graph to the server when elkjs is estimated to need more
  than a second for it, not past 150 nodes or 400 edges. Of two graphs of 150
  nodes on the default preset, elkjs lays one out in 0.7 s and the other in
  21 s.
- A layout the server was asked for and did not answer is refused when elkjs
  is estimated to take longer than the server's timeout. The diagram says so
  on its status line and stays as it was.
- Every layout logs its estimate beside the time it took.

The cycles of a graph are broken as ELK breaks them (its greedy order), and
a port of a container that an edge of the inside ends on is a node of the
pass. Without the two, a synthesised netlist was costed seven times too deep
and a unit drawn open at less than half.

The rates are a least-squares fit to 84 generated networks of 30 to 10,000
nodes (`test/layout-rates.measure.test.ts`, `scripts/fit-layout-rates.mjs`),
each within a factor of two of its estimate on both engines, and were
checked on seven graphs of picorv32 they were not fitted to.

Stage 8.5b of the mlir-viewer convergence plan, the platform half.

Claude-Session: https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY
- A layout that fails leaves the diagram as it was. The layout commands clear
  every edge route before ELK is asked, and Untangled and Free Ports move the
  ports first: a refused layout left straight edges and the new port order in
  the model. The routes (`layoutFromClearedRoutes`) and the ports are put
  back, for any failure.
- The options a layout command sets for its own run are taken off again when
  the layout fails, in the boundary-flow and serpentine handlers too.
- The refusal has a threshold of its own: the server's timeout, and no less
  than a minute (`elkjsTakeoverMs`). A short timeout no longer refuses a
  graph elkjs lays out in seconds.
- A Free Ports layout is refused at its probe. It used to swallow the refusal
  and ask the server for the same graph again, waiting the timeout twice. The
  status line is told once, for the probe, the layout and the pinned band.
- Nodes another algorithm than the layered one lays out are in no estimate.
  They are counted (`unpriced`), and `auto` keeps the size rule for them:
  such a graph was estimated at nothing and never went to the server.
- The options of Untangled and Free Ports over the default preset are a
  preset of their own, `untangled`, measured on 24 graphs: about three times
  the middle preset, which they were priced as.

Claude-Session: https://claude.ai/code/session_01EToj6NxqLABK3H5cucHNsY
@endrix
endrix merged commit 543a3dc into main Dec 2, 2026
1 check passed
@endrix
endrix deleted the feat/layout-estimate branch December 2, 2026 21:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant