Repository navigation
Typed engine: about 20 KB less code, same layouts - #7
Merged
Merged
Conversation
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]>
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.
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)
retainedBytesand thestorageBytesmethods addcap(...)terms directly instead of passing the slices to variadic helpers (capsI32,capsF64,capsBool), whose calls built arrays of slice headers. The same integer sum.ensureNode,edgesandresizeare//go:noinline; none of their call sites is hot.growstays inlinable (out of line it cost 0.7% more instructions).nlabelNone, instead of being built field by field at 14 inlined sites. The same value, NaN bits included.positionSelfEdgesbuilds both self-loop shapes through onesetSelfLoop. 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).rgraphandblockGraphshare one incidence-list builder,incidence.build: the same counting sort, the same lists in the same order.Checked
go test ./...andgo test -race ./...pass, as do gofmt, vet and the js/wasm build. Every commit builds and vets on its own.int()andint64()conversion rewritten to amd64's rule for NaN and out-of-range floats (the changes were made on arm64).d2 picks this up as v0.3.1 in d2lang/d2#2999.
🤖 Generated with Claude Code