Skip to content

Typed engine: about 20 KB less code, same layouts - #7

Merged
alixander merged 5 commits into
mainfrom
perf/smaller-typed-engine
Nov 22, 2026
Merged

alixander merged 5 commits into
mainfrom
perf/smaller-typed-engine

Conversation

@alixander

Copy link
Copy Markdown
Contributor

The typed engine with less code. Five small changes, each exact by construction: the same statements run in the same order, with the same floating-point operations. They shrink d2's release binaries by about 20 KB (darwin/amd64 -20,320 B, linux/amd64 -16,384 B, windows/amd64 -20,480 B), part of keeping d2's binaries under their 45 MB budget.

What changes (one commit each)

  1. retainedBytes and the storageBytes methods add cap(...) terms directly instead of passing the slices to variadic helpers (capsI32, capsF64, capsBool), whose calls built arrays of slice headers. The same integer sum.
  2. ensureNode, edges and resize are //go:noinline; none of their call sites is hot. grow stays inlinable (out of line it cost 0.7% more instructions).
  3. New node labels are copies of one template value, nlabelNone, instead of being built field by field at 14 inlined sites. The same value, NaN bits included.
  4. positionSelfEdges builds both self-loop shapes through one setSelfLoop. The multiplications by 1/2 stay in the function of the additions that use them, so the same fused multiply-adds compute the points (checked in the assembly for arm64, amd64 v1 and v3, ppc64le, s390x, riscv64 and loong64).
  5. rgraph and blockGraph share one incidence-list builder, incidence.build: the same counting sort, the same lists in the same order.

Checked

  • go test ./... and go test -race ./... pass, as do gofmt, vet and the js/wasm build. Every commit builds and vets on its own.
  • d2's corpus test (the 348 distinct requests of d2's end-to-end suite) gives identical results, and d2's 386 replayed dagre layout calls are byte-identical to the original engine's.
  • The tests also pass with every int() and int64() conversion rewritten to amd64's rule for NaN and out-of-range floats (the changes were made on arm64).
  • Speed: dagro's own corpus benchmark runs 0.3% more instructions (commit 2). d2's replayed dagre calls, with this and d2's matching adapter changes, take as long as before: sums of per-call minimums 15.4-15.6 ms against 15.5-15.8 ms in 8 alternating pairs, instructions within 0.1%.

d2 picks this up as v0.3.1 in d2lang/d2#2999.

🤖 Generated with Claude Code

alixander and others added 5 commits November 22, 2026 11:33
capsI32, capsF64 and capsBool took their slices as variadic arguments, so
every call built an array of slice headers on the stack: 6.6 KB of code
for what is a sum of capacities. retainedBytes and the storageBytes
methods now add cap(...) terms directly.

The value is the same integer sum (the same terms; integer addition is
exact and associative), so which states are kept is unchanged.
darwin/amd64 d2 release binary: -8,192 bytes.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The compiler inlined these small helpers at 5, 19 and 31 call sites;
ensureNode carries the append of a whole node label with it. None of the
sites is hot enough for the inlining to matter: each call grows a buffer,
clears one or lists all edges once per pass. They are now //go:noinline.
grow stays inlinable: some of its callers are hot (kept out of line it
cost 0.7% more instructions for under 2 KB of code).

Only where the code is placed changes: the same statements run in the
same order (no floating-point arithmetic is involved).
d2's darwin/amd64 release binary: -12.4 KB of code and tables; dagro's
corpus benchmark: +0.3% instructions.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
newNLabel was inlined at 14 sites, each building the label field by field.
It now returns a copy of nlabelNone, the same value (the same NaN bits,
inone ids, zero elsewhere), so each site is one copy. nlabelNone is a
package variable, since a struct cannot be a constant, and nothing writes
it.
d2's darwin/amd64 release binary: -0.8 KB of code and tables.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
positionSelfEdges built a self-loop's seven points in two places, each
with its own inlined copy of appendCenteredSelfEdgePoints. Both now call
setSelfLoop, which takes the node's width and height and halves them
itself: the multiplications by 1/2 stay in the function of the additions
that use them, so where the compiler fuses a multiply and an add (arm64)
the same four fused operations compute the points as before (checked in
the arm64 assembly: FMADDD/FMSUBD of x, y with width, height times 0.5).
About -1.3 KB of code and line tables in d2's darwin/amd64 binary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
rgraph.finish and blockGraph.finish were the same counting sort of edges
into per-node in- and out-lists, the block graph's through its global to
local node map. Both now embed incidence and call its build, which maps
the endpoints when given a map. The lists, their order (edge order) and
the buffers' sizes are the same as before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander
alixander merged commit 60673a7 into main Nov 22, 2026
2 checks passed
@alixander
alixander deleted the perf/smaller-typed-engine branch November 23, 2026 00:36
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