Repository navigation
Conversation
SetEdge held the edge logic, SetEdgeObject called it with the edge's name as its fourth argument (which SetEdge converts with jsConcatString, as graphlib accepts a name of any type), and an internal setEdge repeated the whole body for a named edge without a label. setEdge now is that body, with the optional label and the name, already a string, as plain parameters; SetEdge converts its arguments and calls it, SetEdgeObject and LayoutInput.Graph call it directly. Nothing changes for callers. This prepares the next commit: a program that builds Graphs only through LayoutInput.Graph then needs neither SetEdge nor its conversion of names. On its own it does not change the size of a program that links the Graph engine. In D2's WebAssembly module, which does, dagro's code is 28 bytes smaller (SetEdge 2,042 -> 970 bytes, the new setEdge 1,102, LayoutInput.Graph 2,417 -> 2,387, SetEdgeObject 215 -> 187). The old three functions stay in graph_setedge_test.go, which runs random calls through both and requires the same graph after every step, the same panics and the same default labels, and checks LayoutInput.Graph against its old edge loop on 3000 generated inputs (repeated ids and edges, odd names, invalid indices). Co-Authored-By: Claude Fable 5.2 <[email protected]>
LayoutTyped runs the typed engine and, for the inputs that engine does not
accept or gives up on, falls back to the Graph engine (Layout on
in.Graph(), or the Attrs engine with the drawn counter values replayed).
That fallback is the one reference from the typed API to Layout, the Attrs
engine and most of Graph, so a program that only calls LayoutTyped links
both engines. D2's WebAssembly module is such a program, and no diagram of
D2's test suites reaches the fallback: with it left out, D2's Go tests (e2e
suite included) pass except for three graphs of TestLayoutIrregularGraphs
that only a Go caller can make (a repeated object, a repeated edge, an edge
endpoint outside g.Objects).
The fallback now lives in typed_fallback.go, and a build that sets the
dagro_typed_only tag gets typed_only.go in its place. The tag is the only
switch. Without it LayoutTyped does what it did before, under every
compiler: leaving the fallback out of TinyGo builds by default would have
changed what LayoutTyped returns for every TinyGo user (and D2's own
TestLayoutIrregularGraphs fails in a build without it).
With the tag, LayoutTyped returns the error of in.Graph() for an invalid
input and the new ErrGraphEngine for any other input the typed engine does
not accept (repeated or empty node ids, ids with "\x01", repeated edges,
edges at a node with children, where the Graph engine panics, and
acyclicer "greedy"); a panic of the typed engine reaches the caller
(runGuarded's recover is not linked either). The typed engine's greedy
acyclicer runs the Graph implementation of greedyFAS, so it moves to
typed_fallback.go too: with the tag, build and buildTyped leave acyclicer
"greedy" to the Attrs engine, which lays such graphs out the same. Layout
and the Graph API work in every build.
D2's WebAssembly module (TinyGo without a scheduler, wasm-opt, custom
sections stripped), built from one D2 tree against v0.3.1 and against this
commit:
- with the tag: 16,296,398 -> 16,185,971 bytes (-110,427), after
brotli -q 11 4,868,603 -> 4,828,419 (-40,184). dagro's code goes from
234,281 bytes in 272 functions to 76,763 in 133, and D2's adapter,
which now has the typed engine's run inlined (50,080 bytes of dagro
before), from 68,311 to 118,681: about 107 KB less between them. What
is left of dagro there is the typed engine, 8,000 bytes that build a
Graph for in.Graph() and 4,130 of putLState.
- without the tag: 16,296,398 -> 16,296,328 bytes (-70). dagro's code
is 28 bytes smaller and D2's adapter, where LayoutTyped is inlined, 43.
Both modules produce the outputs of the module built with v0.3.1 on D2's
behaviour harness (24,817 hashed outputs of 519 scripts, 0 differences).
Tests:
- go test -tags dagro_typed_only ./... runs the suite without the
fallback (added to CI). The tests that compare LayoutTyped with
Layout(in.Graph()) hold it to that only where the typed engine lays the
input out, and to the documented error or panic elsewhere
(typedOnlyKind); TestLayoutTypedWithoutGraphFallback and
TestLayoutTypedPanicWithoutGraphFallback cover those two directly.
- TestTypedOnlyLinksNoGraphEngine builds testdata/typedonly, which uses
the typed API as D2 does, with and without the tag and reads the
symbol tables: with it, no function of the Graph engine is linked
and every dagro symbol belongs to the typed engine or to building a
Graph (LayoutInput.Graph is typed API).
Co-Authored-By: Claude Fable 5.2 <[email protected]>
Contributor
Author
|
Superseded by #10: instead of a build tag, the Graph engine is removed and LayoutTyped returns errors where it fell back (released as v0.4.0). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Draft, because merging it only pays off if D2 decides to build its WebAssembly module with the new tag. That decision is described at the end.
What it does
LayoutTypedruns the typed engine and falls back to theGraphengine for inputs the typed engine does not accept, or when it panics. That fallback is the only reference from the typed API toLayout, the Attrs engine and most ofGraph, so a program that only callsLayoutTypedlinks both engines.Graph: one setEdge behind SetEdge, SetEdgeObject and LayoutInput.Graphis a refactoring that letsLayoutInput.Graphbuild a graph withoutSetEdge's conversion of names.LayoutTyped: a dagro_typed_only build tag leaves the Graph engine outmoves the fallback totyped_fallback.go. A build with-tags dagro_typed_onlygetstyped_only.goinstead:LayoutTypedthen returns the newErrGraphEnginefor a valid input the typed engine does not accept, and a panic of the typed engine reaches the caller.Without the tag nothing changes, under any compiler. A differential test over 66,000 generated cases gives byte-identical results, errors and panics for v0.3.1 and this branch, and D2's module built against it differs by 70 bytes with identical output.
What the tag is worth to D2
D2's WebAssembly module goes from 16,296,398 to 16,185,971 bytes (-110,427), and from 4,868,603 to 4,828,419 compressed with brotli (-40,184). Its output is identical on all 24,817 results of a 519-script comparison.
The decision
D2 may set the tag only if no diagram can reach the fallback, because otherwise its WebAssembly build would answer differently from today for that diagram. A review went through every route:
LayoutTypedto theGraphengine (empty, repeated or"\x01"node ids, bad indices, parent cycles, edges at containers, repeated edges, the greedy acyclicer) is excluded by what D2's adapter passes: ids are object positions, indices are in range, containers get no edges, and it never sets the acyclicer.panicstatements are excluded by bounds and per-site arguments.Graphengine in D2's Go tests, the 519 scripts, 65,641 generated D2 scripts, 5.3 million random inputs in the adapter's domain and 2.15 million mutants of two captured D2 requests.What cannot be excluded by argument is the case the fallback exists for: an unknown run-time error somewhere in the typed engine. With the tag such a diagram would fail with the panic, where today it gets the Attrs engine's layout. No such diagram is known.
If D2 takes the tag, two things should come first:
"0".."n-1", integer sizes,minlen1, every edge named, edges only between leaves, parallel edges and self-loops). Without the tag such a regression is invisible, because the fallback hides it.TestLayoutIrregularGraphs, which getErrGraphEngineunder the tag.🤖 Generated with Claude Code