Skip to content

D2.js: WebAssembly module less than half the size - #3046

Merged
alixander merged 43 commits into
masterfrom
perf/smaller-wasm
Nov 26, 2026
Merged

alixander merged 43 commits into
masterfrom
perf/smaller-wasm

Conversation

@alixander

@alixander alixander commented Nov 26, 2026 •

Copy link
Copy Markdown
Collaborator

Makes D2.js's WebAssembly module less than half the size and faster, with identical output on D2's end-to-end corpus.

Raw Brotli
master (89a3c1f), TinyGo 0.44.0 22,487,341 5,557,964
this branch, TinyGo fork v0.44.0-d2.3 (what CI installs) 10,584,415 (−52.9%) 3,580,823 (−35.6%)

Both modules were built here, each with its own build flags, with the same LLVM (22.1.8) and Binaryen (131). CI uses Binaryen 133 and the TinyGo release binaries, so the modules it ships differ a little in size; their sizes were not measured. Brotli is brotli -q 11 on the module file.

On that corpus the module takes less than half of master's time: 44% and 49% of master 5d40877's summed request time in two runs that alternated master's module and this one. Against the same commit built with v0.44.0-d2.2, it starts 2 ms sooner (36.9 against 38.9 ms) and renders 150 corpus diagrams 15% faster (468 against 549 ms, best of 9 runs).

Master's removal of isometric rendering (#3048) took 4.5 MB out of master's module. This branch had already left isometric out of D2.js, so the merge barely changed its own module.

What changed

  • Build without a goroutine scheduler (ci/wasm-flags.sh). On WebAssembly TinyGo's scheduler needs Binaryen's asyncify pass, which made the code about two thirds larger. D2.js calls into the module and every call runs to completion, so it is built with -scheduler=none -panic-unwind=explicit and a 4 MB stack. That needs d2lang/tinygo, which ci/install-wasm-tools.sh installs. ci/test-wasm.sh checks that the module's go statements are exactly the ones listed in ci/wasm-goroutines.txt, each with its reason. Where D2 starts goroutines, a scheduler.none build does the same work in order (d2js registration, TALA seeds and route flavors). CI tests TALA's goroutine-free variants natively, and a change to ci/wasm-flags.sh, ci/wasm-goroutines.txt or a *_nosched.go file selects the wasm job.
  • Dependencies that keep their tables as constant data: mathjax-go v0.1.1 (−0.82 MB), elk-go v0.3.2 (−0.20 MB). mathjax-go v0.1.2 (d2lang/mathjax-go#212) also embeds its glyph outlines compressed and decompresses them the first time a formula needs one (−108 KB).
  • Smaller code and data in D2:
    • one HTML entity table;
    • grapheme segmentation without the word and sentence tables;
    • no goquery in Markdown measuring;
    • one encoding per bundled font, and all ten fonts as one brotli stream in wasm builds (−1.0 MB raw);
    • one sort implementation for D2's own sorts in TinyGo builds (lib/xsort, −87 KB).
  • Bundled fonts decoded one allocation per face (d2renderers/d2fonts/ttf_stream.go). For every pointer, on every collection, TinyGo's collector walks forward to the last block of the object pointed into, so pointers to faces sliced from one 1.6 MB buffer crossed every later face. Renders are about 1% faster.
  • Simpler code that is also smaller:
    • TALA's 19 shape wrapper types became one type driven by tables (nodeshape: 2,233 → 1,045 lines);
    • lib/shape's 23 shape types became one struct driven by per-kind data, plus a small wrapper for shapes drawn from paths, with its public API unchanged;
    • shared arrowhead markers in d2svg (366 → 190 lines);
    • one guarded implementation of TALA's straight-line and duplicate routing (−365 lines).
  • Native only: smaller repeats removed in d2raster, d2scenebuild, d2svgimport and fontface (+586/−826 lines). These serve the CLI's PNG, GIF, PDF and PPTX output; D2.js links none of the code they touch.
  • CI keeps the savings: ci/build-wasm.sh fails if the module, raw or compressed with Brotli, grows past ci/wasm-size-budget.txt (set about 3% above the module built here; it should be tightened once CI prints its own sizes). A test in lib/xsort fails if a D2 package that D2.js links calls a generic slices sort, which TinyGo would copy for each element type.

Overall the branch deletes 5,904 lines and adds 4,244.

How it was checked

  • Every output is identical: a harness runs 519 scripts through the module, covering compile and render for each layout engine, sketch, dark theme, appendix, ASCII and animation, plus LSP and format: 24,824 hashed outputs, 0 differences against master's module (89a3c1f).
  • Tests: go test ./... (92 packages with tests), CI's -race, dagre_shadowmaps, scheduler.none and xsort_small runs and ci/checks_test.py pass, and ci/test-wasm.sh and d2.js's unit (58) and integration (11) tests pass with v0.44.0-d2.3.
  • Refactors: those in lib/shape, TALA's nodeshape, d2svg, d2scenebuild, d2raster and d2svgimport were each first proven against the old code by a golden test that pinned the old code's exact results as digests, and fontface's by one that was not committed. Those tests were recorded on darwin/arm64, where Go fuses multiply-adds, and fail on CI's linux/amd64, so 4d47999 removes them. D2's e2e suites, which pin SVG bytes and PNG pixels on every platform, pass on amd64. TALA's routing change (ed6ffd1) had no golden test: each removed unguarded helper was checked against its guarded twin, then by the routing tests, the e2e suites and the harness.
  • Differences callers can see: in D2.js, the wasm font table behaves differently in a single corner case, documented in d2js/d2wasm/fonts_test.go. Go callers of the dagre layout get dagro v0.4.0's errors for graphs only Go code can build (below). TALA's straight-line and duplicate routing can notice a cancellation at a different point, and it skips a labelled edge with fewer than two points, where the old code panicked; no harness or e2e input has one.

Decided along the way

  • dagro v0.4.0 (d2lang/dagro#10) keeps only the typed engine D2 uses. The original port of dagre's JavaScript, which LayoutTyped fell back to, is gone (−116 KB). All four graphs in TestLayoutIrregularGraphs, which only a Go caller can build, now get errors wrapping dagro.ErrUnsupportedInput. Before, dagro laid out the one with a repeated object, D2 returned its own errors for an edge endpoint outside g.Objects and for a repeated edge, and only the parent cycle got an error from dagro.
  • d2lang/brotli v0.1.0 is andybalholm/brotli without http.go and unused API. With it, nothing in D2.js imports net/http any more (−93 KB).

TinyGo fork v0.44.0-d2.3

CI now installs v0.44.0-d2.3. It gives each pointer a GC stack slot only while a collection can find it in use; before, every tracked pointer had a slot for the whole function (d2lang/tinygo#1). The module is 601 KB smaller than with v0.44.0-d2.2 (158 KB with Brotli) and renders about 15% faster, with identical harness output, and the stress collector below found no freed-memory reads. The pass went through four adversarial review rounds; the last two found nothing that valid Go reaches, and its remaining assumptions (code that breaks the rules for unsafe pointers) are written down in transform/gc.go.

v0.44.0-d2.2, which d2.3 builds on, made the module about 170 KB smaller than v0.44.0-d2.1. It stores the table of global GC roots as runs of equally spaced ranges (about −200 KB), and it roots two kinds of pointers that TinyGo 0.44.0 does not (about +20 KB): a value read from a map, and a global that LLVM retypes to an integer. Under the fork's stress collector (-tags=gc.stress, a collection at every 67th allocation), 17 scripts sent through every layout engine and renderer give 993 results. Built with v0.44.0-d2.1, which roots these as TinyGo 0.44.0 does, a TALA render read freed memory; built with v0.44.0-d2.2 or v0.44.0-d2.3, none did. Master's own build was not run this way, since only the fork has that collector.

🤖 Generated with Claude Code

alixander and others added 26 commits November 25, 2026 18:05
The WebAssembly module carried Unicode data it did not need:
- html.UnescapeString builds the standard library's HTML entity table in
  code the first time it is used, over 100 KB of code. internal/htmlentity
  unescapes exactly as it does, using the table golang.org/x/net/html
  already holds as data. d2ir, d2compiler, d2svg and textmeasure use it.
- uniseg.Graphemes also tracks word and sentence boundaries, whose tables
  were linked in with it. textmeasure iterates over grapheme clusters with
  a small helper (graphemes.go) that finds the same clusters without them.
- markdown_svg.go reads the HTML that goldmark renders with x/net/html
  directly instead of through goquery, which is now a test dependency only.

htmlentity and graphemes are tested against the code they replace.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Embedded, the interface gave the struct value itself, not only its pointer,
the font.Face method set, and TinyGo added a case that copies the 1.5 KB
struct to each of the four font.Face dispatch functions (50 KB together).
Only *metricPreservingFace is ever created; Close and Glyph now forward to
the base face explicitly.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Native builds register four separately encoded WOFF files, for
SourceSansPro-Semibold and SourceCodePro-Bold, -Semibold and -Italic; every
other face's whole-font encoding is its TTF. The WebAssembly build now uses
the TTF for those four as well, which saves 0.39 MB. The SVG renderer
embeds a whole-font encoding only when a face cannot be subset, and a test
shows that this never happens for the bundled faces. The one case where a
caller can see the difference is documented in d2js/d2wasm/fonts_test.go.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The WebAssembly module is built without TinyGo's scheduler (next commit).
Built with the scheduler.none tag, D2 does in order what it otherwise does
in goroutines:
- d2js registers its API without waiting in a goroutine
  (js_sched.go, js_nosched.go);
- TALA tries its seeds (seeds_*.go) and route flavors
  (routing/flavors_*.go) one after the other.
With one CPU, as under TinyGo, the cooperative scheduler already ran them in
this order, so the results are the same. Native builds are unchanged, and
the TALA tests also run with the tag.

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

On WebAssembly, TinyGo's scheduler needs Binaryen's asyncify pass, which
rewrites every function that could be suspended and made the module's code
about two thirds larger. D2.js calls into the module and every call runs to
completion, so the module is now built with -scheduler=none
-panic-unwind=explicit -tags=goroutines.inert -buildmode=c-shared and a
4 MB stack; ci/wasm-flags.sh explains each flag. That needs TinyGo from
https://github.com/d2lang/tinygo, which ci/install-wasm-tools.sh installs.

ci/test-wasm.sh checks that the module's go statements are exactly the ones
listed, each with its reason, in ci/wasm-goroutines.txt, and ci/wasm-strip
removes the function names that -no-debug leaves in the module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Both now keep their tables as constant data in TinyGo builds:
- mathjax-go: glyphs, outlines, delimiters and the operator dictionary,
  -0.82 MB in the WebAssembly module;
- elk-go: property keys and option registrations, -0.20 MB, and nothing of
  ELK runs at start-up any more.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
D2 sorts with slices.SortFunc, SortStableFunc and Sort at 84 call sites,
and TinyGo instantiates pdqsort and its helpers for every element type:
92 KB of the WebAssembly module. lib/xsort calls those same functions in
normal builds. Built with tinygo, or with the xsort_small tag, it uses
sort.Slice and sort.SliceStable, which are generated from the same template
and give the same order, ties included. A differential test checks this,
and CI runs the affected packages' tests with -tags xsort_small.

-87 KB in the WebAssembly module.

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

The ten bundled TTFs (1.64 MB) are now embedded in WebAssembly builds as one
brotli stream of 0.5 MB, decoded once at start-up (about 13 ms in node).
Only one stream helps: compressing each file on its own makes the
transferred, brotli-compressed module larger, while one stream keeps what
the faces share. go generate rebuilds ttf_stream.br, and
TestBundledTTFStream fails if a font changes without it.

-1.02 MB raw, -23 KB brotli.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The package had 19 types, one per shape, each embedding lib/shape.Shape and
implementing TALA's Shape interface with mostly constant data: label
positions per tier, port counts, center and mirrored ports. They are now one
Shape struct whose short methods read a table of per-kind policies; the snap
points that depend on the box keep their exact formulas. layoutgraph.Node
has a named Shape field instead of embedding it.

The package goes from 2,233 to 1,045 lines. equivalence_test.go pins the
results of every method for every kind, recorded from the old code.
-63 KB in the WebAssembly module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The 23 unexported shape types are now one shape struct and a small pathShape
wrapper, so that only shapes with custom paths implement
PathCommandProvider, as before. Each kind is a shapeKind value in its own
file that says what differs from a rectangle. The exported API is unchanged.
equivalence_test.go pins every constructor and method over many boxes,
recorded from the old code.

-36 KB in the WebAssembly module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The check whether a rune is renderable was written three times; it is now
two helpers. The two bounded caches with a per-call overflow map are one
generic spillCache. The COLRv1 transform paints share one case, and an
error path reuses glyphDataKind. Golden digests of coverage and shaping,
recorded from the old code, are unchanged.

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

The 16-case arrowhead switch built every polygon from hand-written point
lists; each outline is now a row of a table. routeInk, routeCasing and the
tubes share their frame loop and triangle strip, the copied label-box and
root-rectangle code is shared, and a duplicate of shapeSupportsShadow is
gone. TestSceneGolden pins 437 scenes recorded from the old code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The preflight walk and the retained-vector validator each had their own copy
of the per-node and per-primitive validation; preflight.enterNode and shared
helpers now do it once. d2svgimport loses a few repeated blocks as well.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
About 370 lines of per-arrowhead Sprintf blocks are now about 150: one point
formatter and one element builder cover all arrowheads, and arrowheads with
the same outline share it. d2wasm loses a repeated block as well.

-19 KB in the WebAssembly module.

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

Routing kept two copies of the straight-line and duplicate-reordering logic:
one with a work guard, used by RouteEdges and the OVG router, and one
without, used by StraightEdgesFallback and ReorderDuplicates. The guarded
code is now the only one; a stage without a budget of its own uses a guard
without a limit that checks the context at every step. Results are
unchanged; only when a cancellation is noticed can differ.

-365 lines.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
D2.js no longer renders isometric diagrams. On js/wasm, d2svg builds a
stub of renderIsometric instead of isometric.go, so the module no longer
links the isometric renderer or what only it needs: d2scenebuild,
d2raster, d2svgimport, the go-text/harfbuzz font stack, lib/imageasset
and the image decoders. render and renderSVG reject isometric: true
(from options or vars.d2-config.isometric) with a 400 error: "isometric
rendering is not available in D2.js (the D2 CLI and Go library render
it); render with isometric: false". Compile still reports the resolved
mode. The CLI and Go library build the same code as before.

The module is 2,580,918 bytes (18.7%) smaller, 846,150 bytes (18.5%)
with Brotli.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
dagro v0.4.0 drops the original port of dagre's JavaScript (the Graph and
Attrs engines). LayoutTyped used to fall back to it for inputs the typed
engine does not take, and when the typed engine panicked. No D2 diagram
reached that fallback: every way into it needs a graph D2 does not build (an
empty or repeated id, a repeated edge, an edge at a container), and fuzzing
with millions of D2-shaped inputs found none. dagro pins the typed engine's
results on 14,111 inputs recorded before the deletion.

The three graphs in TestLayoutIrregularGraphs that only a Go caller can
build now get dagro's errors (ErrUnsupportedInput), and a panic inside dagro
comes back as an error (PanicError) instead of crashing the caller.

-116 KB raw, -34 KB brotli in the WebAssembly module.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
andybalholm/brotli's http.go imports net/http, so importing the package
links net/http's initialization (and much of crypto/tls, crypto/x509 and
encoding/asn1) into D2.js although nothing calls it. d2lang/brotli
v0.1.0 is upstream v1.3.0 with http.go, the matchfinder/flate packages,
NewWriterV2 and NewReaderOptions removed; the decoder and the encoder D2
uses are unchanged (the fonts stream regenerates byte for byte, and
re-encoding the bundled emoji font reproduces its embedded stream).

Once isometric rendering leaves the WebAssembly build, this is the last
thing that pulls net/http into it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Conflicts, each resolved by porting master's change into the branch's
restructured code:

- lib/shape/shape_c4_person.go: master named the vertical padding and
  minimum height ratio (c4VerticalPadding, c4MinimumHeightRatio) and added
  FitC4PersonBody (07704de). The branch's c4PersonKind
  functions already use the named constants (auto-merged); FitC4PersonBody
  is kept, and master's shapeC4Person.GetDefaultPadding method is dropped,
  since the per-kind data (paddingX 10, paddingY defaultPadding) replaces it.
- d2renderers/d2scenebuild/labels.go: master placed shape labels through
  d2target.Shape.LabelTopLeft, which honors MotionLabelTopLeft and
  MotionLabelBox (e3ccba1, c8ed333). The branch's shapeLabelPlacement
  keeps its shared shapeLabelBox and now returns LabelTopLeft.
- d2renderers/d2scenebuild/markdown.go: same change for markdown labels;
  markdownShapeLabelTopLeft uses shapeLabelBox and LabelTopLeft.
- d2renderers/d2svg/d2svg.go:
  - connection label mask: master multiplies the mask opacity by
    MotionLabel.Opacity for labels (c8ed333); the branch's single
    label-or-icon mask path does the same when the connection has a label.
  - rectangle case: master draws a DrawingOutline body and outline first
    (4176c9c); it now precedes the branch's early 3D break.
  - hexagon and default cases: master builds the multiple copy from
    s.GetType(), since s now comes from targetShape.Geometry() (e3ccba1); the branch's
    writer.paths form keeps that.
  - LaTeX shape labels: master positions them with LabelTopLeft
    (e3ccba1); the branch's latexLabel helper now gets that point.

Semantic conflicts (master code using what this branch changed) are fixed
in the commits that follow.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master's boardLinkSyntax (5ca3b1a) unescapes a link with
html.UnescapeString. This branch replaced that call with
internal/htmlentity (a7dc372) so that D2.js does not link the html
package's entity table; boardLinkSyntax now calls htmlentity.Unescape, as
validateBoardLinks does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master added seven slices.Sort, SortFunc and SortStableFunc calls (spread
resets and edge rules in d2ir; board groups, board alignment, centered
branches and handoffs in TALA). Each slices instantiation adds its own copy
of pdqsort to TinyGo builds, so they now call xsort.Ordered, xsort.Func and
xsort.StableFunc like the rest of D2 (538092a). Native builds still run
the same slices functions; xsort_small builds use sort.Slice and
sort.SliceStable, which give the same order.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master's bb10f64 changed Diagram.BoundingBox for shape labels: outside
and border labels are placed against OuterLabelBox (which grows the box by
the 3D or multiple offset), and border labels round their bounds outward.
That changes the scene's bounds, and so the digest, of 30 of the 441
recorded scenes: label/OUTSIDE_*/{rectangle,hexagon}/{3d,multiple} and
label/BORDER_TOP_CENTER/*. Every other scene, including all inside-label
and BORDER_LEFT_BOTTOM cases, still matches the digest recorded from the
old code.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master's ae76799 clamps class and sql_table border radii to half the
shorter side (d2themes.ClampBorderRadius), insets their row dividers by the
clamped radius and closes the rounded clip path with a bare Z. The golden's
variant 1 draws every shape with border-radius 6, so the class and
sql_table digests change. With that variant's radius set to 0, both digests
are the same before and after the merge; every other shape's digest is
unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Since D2 imports github.com/d2lang/brotli instead of andybalholm/brotli
(408f554), net/http is no longer linked, and with it went the last
reachable time.AfterFunc (context.WithTimeout and WithDeadline are not
reachable from d2js either). ci/test-wasm.sh found the module's go
statements to be context/context.go:523 and regexp2's fastclock only, so
time/sleep.go:182 leaves ci/wasm-goroutines.txt. The list matched at
bc39e68, the commit before.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Measured on the merged branch with the TinyGo fork that CI installs
(v0.44.0-d2.1): 11,435,129 bytes raw and 3,747,106 with Brotli, against
26,940,410 and 6,706,062 for master built as it ships. Of that, leaving out
isometric rendering accounts for 2.6 MB raw and 0.85 MB with Brotli, which
its own changelog line states; the rest is 48% and 31%.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander alixander changed the title D2.js: WebAssembly module about half the size D2.js: WebAssembly module less than half the size Nov 26, 2026
alixander and others added 3 commits November 26, 2026 00:50
ci/sub/bin/fmt.sh runs goimports with -local=...,github.com/d2lang, which
puts github.com/d2lang/brotli and lib/xsort in the group of D2's own
imports. CI's gen and js-check steps failed on the difference.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The build that lists the module's go statements writes everything to one
file, and any line that is not such a statement fails the check. On a
fresh CI runner the go command first reports "go: downloading ..." for
every module, so the check failed there although the list was right.

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

The refactors of lib/shape, TALA's nodeshape, d2svg, d2scenebuild, d2raster
and d2svgimport each added a test that pinned the old code's exact results
as digests (14,000 lines with their data) to prove the new code equivalent.
They were recorded on darwin/arm64, where Go fuses multiply-adds, and fail
on CI's linux/amd64; recorded there instead, they would fail on Macs. The
refactors were proven on arm64 and in the WebAssembly module, which has no
fused operations, and D2's e2e suites, which pin SVG bytes and PNG pixels on
every platform, pass on CI's amd64 with them.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
alixander and others added 3 commits November 26, 2026 00:57
v0.44.0-d2.2 stores the table of global GC roots as runs of equally spaced
ranges instead of one entry per element (it was 34,483 entries), which
makes the WebAssembly module about 170 KB smaller. It also fixes two kinds
of pointers that TinyGo 0.44.0 did not root: a value read from a map, and a
global that LLVM retypes to an integer because it is only copied whole.
Under a collector that collects at every allocation, D2 read freed memory
with the previous compiler on one diagram and does not with this one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master removed isometric diagram rendering (#3048) and preserves NaN
payloads in box intersections (#3047).

Take master's deletions, including the isometric files this branch had
simplified, and drop what this branch added for isometric in D2.js: the
renderIsometric stub, the 400 error for isometric: true, and the notes
about it in the README, the type definitions and both changelogs. The CI
step that runs tests with xsort_small no longer names
d2renderers/d2isometric. The changelog keeps master's wording of the font
cache entry: D2.js registers its bundled fonts and uses those caches too.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
d2js/js/README.md: put back the Development paragraph naming TinyGo
0.44.0-d2.2 from d2lang/tinygo. Resolving the isometric conflict with
master's whole file had dropped it, and the build refuses stock TinyGo.

Changelogs: sizes and speed against master 40e942d, whose module lost
4.5 MB with isometric rendering. The module is a further 49.7% smaller
(32.6% with Brotli) and takes 55% of the time. next.md gains an entry for
the TinyGo switch (#2966) these numbers build on, states the asyncify
growth for the code as a whole, as ci/wasm-flags.sh does, and limits the
shared sort routine to D2's own sorts.

CI: test TALA's scheduler.none variants natively. ci/wasm-flags.sh,
ci/wasm-goroutines.txt and *_nosched.go now select the wasm job and
ci/wasm-strip the packed browser job; the first three and ci/wasm-strip
selected no job before.

THIRD_PARTY_NOTICES.txt: the TinyGo runtime and wasm_exec.js in the
module come from d2lang/tinygo v0.44.0-d2.2.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander alixander changed the title D2.js: WebAssembly module less than half the size D2.js: WebAssembly module about half the size Nov 26, 2026
alixander and others added 6 commits November 26, 2026 04:25
On every collection, for every pointer, TinyGo's collector finds the
object pointed into by walking forward to the object's last block. D2
keeps pointers to the first byte of each bundled face, and with the
faces sliced from one 1.6 MB buffer each of those walks crossed every
later face. Each face now has an allocation of its own, and the decoder
checks that the stream ends exactly after the last face.

Renders in D2.js are about 1% faster; output is unchanged.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
mathjax-go now embeds its glyph outlines compressed with brotli and
decompresses them the first time a formula needs one
(d2lang/mathjax-go#212). The D2.js module is 107 KB smaller: 11,152,864
bytes, 49.8% of master's 22,392,675. The changelogs give the sizes
against master.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master released D2.js 0.2.0 (#3050, #3051) and keeps the D2 grammar and
conformance cases in source (#3049). None of it changes Go code, so the
size and harness comparisons against 40e942d still hold.

The D2.js changelog entry of this branch moves from the released 0.2.0
section to Next and now states its sizes against 0.2.0.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
The release notes and the D2.js changelog now give rounded figures: the
module is about half the size and a third smaller with Brotli, takes a
little over half the time, and gives identical output on D2's
end-to-end corpus. Both modules in that comparison were built here with
the same LLVM and Binaryen; CI uses the release binaries and Binaryen
133, so the modules it ships differ a little.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master formats D2 fences in Markdown and stdin safely (#3052), adding
github.com/aymanbagabas/go-udiff. go.mod keeps it and this branch's
replacement of andybalholm/brotli by d2lang/brotli.

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

Master accepts theme names (#3053), unambiguous literals and nested board
names (#3054) and hash-prefixed labels and keyword variable names
(#3056), and publishes D2.js nightlies (#3055). d2compiler/compile.go
imports both master's d2themescatalog and this branch's htmlentity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
v0.44.0-d2.3 gives each pointer a GC stack slot only while a collection
can find it in use (d2lang/tinygo#1). The D2.js module is 601 KB
smaller, 10,558,431 bytes (158 KB smaller with Brotli), and renders
about 15% faster, with identical harness output; test-wasm.sh and the
d2.js tests pass with it.

ci/install-wasm-tools.sh installs it, and d2js/js/README.md and both
THIRD_PARTY_NOTICES copies name it. The changelogs now say the module
is less than half the size and takes less than half the time.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander alixander changed the title D2.js: WebAssembly module about half the size D2.js: WebAssembly module less than half the size Nov 26, 2026
alixander and others added 4 commits November 26, 2026 07:29
Master resolves Markdown variables with D2's scope rules and fixes
variable resolution in templates, globs, Markdown links and steps
(#3057, #3060); lib/textmeasure now does the substitution, so
d2ir/compile.go no longer imports it and keeps this branch's htmlentity.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Master adds the CLI's MCP and LSP servers (#3058, #3059), uvx
distribution (#3061) and a setup-d2 doc (#3062), and makes native builds
embed the TTFs the way master's WebAssembly initializer did (9064887).

d2fonts_embed.go and d2fonts_woff.go are master's, limited to native
builds, so the native CLI's font bytes match master's exactly.
WebAssembly builds keep this branch's d2fonts_embed_wasm.go, which loads
the same TTFs from ttf_stream.br. go.sum is master's, tidied.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ci/wasm-size measures every module ci/build-wasm.sh builds, raw and with
Brotli at quality 11, against ci/wasm-size-budget.txt, and fails the
build if the module outgrows it. The budget starts about 3% above
today's module because CI's Binaryen and TinyGo release binary differ a
little from the local build; tighten it once CI prints its sizes.

lib/xsort's TestD2JSSortsThroughXsort lists the packages D2.js links (go
list for js/wasm with TinyGo's tags) and fails on a direct generic
slices sort in any of D2's own, which TinyGo instantiates once per
element type.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alixander
alixander merged commit d70ec71 into master Nov 26, 2026
20 checks passed
@alixander
alixander deleted the perf/smaller-wasm branch November 26, 2026 22:04
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