Skip to content

Typed requests no longer link the JSON import - #6

Merged
alixander merged 2 commits into
mainfrom
perf/json-codec
Nov 22, 2026
Merged

alixander merged 2 commits into
mainfrom
perf/json-codec

Conversation

@alixander

Copy link
Copy Markdown
Contributor

Programs that lay out only typed Requests, as d2 does, no longer link elk-go's JSON import. Results are unchanged. d2's release binaries shrink by about 58 KB (darwin/amd64 -59,536 B, linux/amd64 -53,248 B, windows/amd64 -57,344 B), part of keeping them under their 45 MB budget.

What changes (one commit each)

  1. internal/core: the JSON import and the layout transfer move from Codec to their own type, JSONCodec (jsoncodec.go). It embeds *Codec and owns the five tables of JSON elements and the option-map scratch. The element steps shared with the typed import (bindNode, bindPort, addLabel, addEdge, ...) stay in Codec and no longer take the JSON element; JSONCodec records it right after each step succeeds. Layout(graph) uses a JSONCodec: its Codec still comes from the codec pool, and the JSON part has a pool of its own with the same bound. The moved code is otherwise verbatim, and PROVENANCE.md lists jsoncodec.go with the same ELK sources.
  2. internal/graph: TryGet asserts the stored value's type once instead of in both lookup paths, and Has and Unset go through the map's non-generic key functions, so each property type's instantiation is smaller.

The public API is unchanged.

Checked

  • go test ./... and go test -race ./... pass (32 packages), as do gofmt, vet and the js/wasm build. Every commit builds and vets on its own. Go 1.23 was not run locally; CI runs it.
  • d2's corpus test (356 requests) gives identical responses, and d2's 369 replayed ELK layout calls are byte-identical to the original's. d2's TinyGo tests (ci/test-wasm.sh, which runs the ELK adapter's tests) pass.
  • Speed: instructions retired per pass of d2's ELK replay are within 0.1% of v0.3.0.

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

🤖 Generated with Claude Code

alixander and others added 2 commits November 22, 2026 11:33
Codec held the JSON element bound to every model element (five tables of
graphjson pointers) and the option-map scratch only JSON imports use, so a
program that lays out only typed requests still linked the JSON import:
the Codec type, pooled through an interface, named graphjson's types, and
their MarshalJSON/UnmarshalJSON methods stayed reachable through
encoding/json's interface calls.

JSONCodec now owns all of that: it embeds *Codec, keeps the five tables and
the option scratch, and has the JSON-only code moved verbatim from codec.go
to jsoncodec.go (Import with its element importers and option import,
TransferLayout with its store, the port/edge/label coordinate tables).
Codec keeps the element steps shared with the typed import (bindNode,
bindPort, addLabel, addEdge, ...), which no longer take the JSON element:
JSONCodec records it right after each step succeeds. coordinatesOf is
split into Codec.nodeCoordinates (all the typed read-back needs) and
JSONCodec.coordinates (the rest). Layout(graph) acquires and releases a
JSONCodec; its Codec still comes from and goes back to the codec pool, and
the rest has a pool of its own with the same bound (2 MB in the slot, the
sync.Pool under gc, dropped under TinyGo). PROVENANCE.md lists
jsoncodec.go with the same ELK sources as codec.go.

Outputs are unchanged by construction: every model operation, check and
error happens in the same order; only where the JSON element pointers are
kept moved, and typed imports no longer append nil entries to tables they
never read. Tests that import JSON use NewJSONCodec/AcquireJSONCodec;
TestReleasedCodecBounded also checks the JSON codec pool. go test ./...
and -race (core, root, providers) pass; the d2 corpus test gives 356 of
356 responses identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
TryGet asserted the stored value's type in both of its lookup paths, and
Has and Unset resolved a property's key in generic code, so every
instantiation (one per property type) carried both copies. TryGet now
finds the stored value first (by the precomputed key, or for a property
without one by ID through the map's non-generic Raw) and asserts its type
once; Has and Unset call HasKey/DeleteKey, or Has/Delete by ID, and the
unused lookupKey goes.

Same lookups in the same order: the key path still checks the mask before
indexOf; the ID path is LookupPropertyKey then indexOf, as lookupKey and
indexOf were; a failed assertion returns the zero value and false, as
before. go test ./... and -race (graph, core, root) pass; the d2 corpus
test gives 356 of 356 responses identical.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander
alixander merged commit 729b065 into main Nov 22, 2026
3 checks passed
@alixander
alixander deleted the perf/json-codec 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