Skip to content

Remove the Graph engine; LayoutTyped returns errors where it fell back (v0.4.0) - #10

Merged
alixander merged 3 commits into
mainfrom
perf/typed-only
Nov 26, 2026
Merged

alixander merged 3 commits into
mainfrom
perf/typed-only

Conversation

@alixander

Copy link
Copy Markdown
Contributor

dagro had two engines: the line-by-line port of dagre's JavaScript (Graph, Attrs, Layout and their phases) and the typed engine behind LayoutTyped, which D2 uses for every diagram. LayoutTyped handed inputs the typed engine does not take, and inputs on which it panicked, to the Graph engine. This removes the Graph engine (supersedes #9, which only made it optional behind a build tag).

Commit 1 pins the typed engine before anything is deleted. TestGolden runs 14,111 inputs (the 311-input D2 corpus, 3,000 D2-shaped random inputs, and random inputs with every option and unusual values) through v0.3.1's typed engine and records, per input, the kind of outcome, the dummy-name counter draws and a hash of the layout's exact bits in testdata/golden. 9,838 of them are laid out (605 with acyclicer greedy). The dagre.js oracle tests (D2 corpus, D2-profile compatibility oracle, TestLayoutMatchesDagreJS) now build LayoutInputs and call LayoutTyped.

Commit 2 removes the Graph engine. The golden files are unchanged by it: every input gets the same result.

LayoutTyped now returns

  • an error wrapping the new ErrUnsupportedInput where it used to fall back: no nodes, an empty node id or one containing "\x01", repeated ids, a parent out of range or a parent cycle, an edge endpoint out of range or with children, two edges with the same endpoints and name, more than 2^26 ranks;
  • a *PanicError when the layout panics, including where dagre itself fails (these used to panic through to the caller with assignment to entry in nil map). The guard also covers writing the output.

greedyFAS (acyclicer greedy) used a Graph copy; it is now ported onto the typed graph, checked by the 605 golden greedy layouts and TestLayoutMatchesDagreJS/cycle-greedy.

Removed API: Graph and its methods, NewGraph, GraphOptions, Edge, Attrs, Layout, LayoutOptions, DebugOrdering, Time, NoTime, LayoutInput.Graph. Breaking: to be tagged v0.4.0.

Local checks: go test ./..., go test -race ./..., go vet ./..., GOOS=js GOARCH=wasm go build ./..., the js/wasm TestD2ProfileRandomLayoutsAreFinite run, and TestLayoutMatchesDagreJS with the dagre 3.1.1 bundle all pass.

🤖 Generated with Claude Code

alixander and others added 3 commits November 25, 2026 18:17
Before the Graph engine goes, record what the typed engine produces so
the tests keep checking the same layouts without it.

TestGolden runs 14,111 inputs through the typed engine's steps (the D2
corpus, 3,000 D2-shaped random inputs and random inputs with every
option and unusual values) and compares each with testdata/golden: the
kind of outcome (a layout, an engine error, an input the typed engine
does not take, a panic), the number of dummy-name counter draws and a
hash of the layout's exact bits. 9,838 of the inputs are laid out.

The dagre.js oracle tests (TestLayoutMatchesDagreJS, the D2 corpus and
the D2-profile tests) now build LayoutInputs and call LayoutTyped
instead of building a Graph, with the same inputs and expected outputs;
the two fixtures that test Attrs key spelling and number coercion are
left out.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dagro kept two engines: the line-by-line port of dagre's JavaScript
(Graph, Attrs, Layout and their phases) and the typed engine behind
LayoutTyped, which D2 uses for every diagram. LayoutTyped handed the
inputs the typed engine does not take, and the inputs on which it
panicked, to the Graph engine. This removes the Graph engine and
everything only it used.

LayoutTyped now returns:
- an error wrapping the new ErrUnsupportedInput where it used to fall
  back: no nodes, an empty node id or one containing "\x01", repeated
  node ids, a parent out of range or a parent cycle, an edge endpoint
  out of range or with children, two edges with the same endpoints and
  name, and more than 2^26 ranks (the decline, now an error);
- a *PanicError (Value = the panic value, Unwrap gives it when it is an
  error) when the layout panics, including the places where dagre itself
  fails, which used to panic through to the caller with Go's
  "assignment to entry in nil map" and now say "dagre fails on this
  input: it sets "order" on a node missing from a layer". The panic
  guard covers updateTyped too. The state of a panicked call is dropped.

Acyclicer "greedy" ran dagre's greedyFAS on a Graph copy; greedyFAS is
now ported onto the typed graph (typed_util.go has it and the few
numeric and key-order helpers the engine kept using).

Removed exported API: Graph and its methods, NewGraph, GraphOptions,
Edge, Attrs, Layout, LayoutOptions, DebugOrdering, Time, NoTime and
LayoutInput.Graph. Point moves to typed_api.go unchanged. The tests that
compared the two engines go with the Graph engine; TestGolden (14,111
inputs) gives the same results as before this change, and the dagre.js
oracle tests, the retention, pool and single-node tests run on
LayoutTyped. New tests cover every unsupported-input error, the panic
errors and concurrent use. README, UPSTREAM.md and a package doc
describe the new API.

This is a breaking change, released as v0.4.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
…endent

CI (linux/amd64) found 145 golden differences that the darwin/arm64
recording did not show: NaN payloads in layouts differ between the two,
and where a failing layout stops (so how many counter values it drew)
depends on Go's platform-dependent conversion of NaN and infinities to
int. goldenDump now writes every NaN as "NaN", and a panic's draws are
not recorded.
A minlen of +Inf converts to a negative int on amd64; TestLayoutTypedDeclinesHugeRanks accepts that layout failure there.

The new golden files were recorded with this change's code and,
separately, with v0.3.1's typed engine (the first commit with this
golden format): the two sets of files are identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander
alixander merged commit 20bc94d into main Nov 26, 2026
2 checks passed
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