Skip to content

Commit d70ec71

Browse files
authored
Merge pull request #3046 from d2lang/perf/smaller-wasm
D2.js: WebAssembly module less than half the size
2 parents 89a3c1f + a304cdc commit d70ec71

218 files changed

Lines changed: 4446 additions & 5904 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎.github/workflows/ci.yml‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,10 @@ jobs:
110110
run: go test -race ./d2lsp/lspserver -count=1
111111
- name: Test the dagre adapter with its TinyGo-only shadow maps compiled in
112112
run: go test -tags dagre_shadowmaps ./d2layouts/d2dagrelayout -count=1
113+
- name: Test TALA with the goroutine-free variants the WebAssembly module runs
114+
run: go test -tags scheduler.none ./d2layouts/d2talalayout/... -count=1 -timeout=15m
115+
- name: Test the sort callers with the TinyGo-only shared sorter compiled in
116+
run: go test -tags xsort_small ./lib/xsort ./lib/textmeasure ./d2ir ./d2lib ./d2renderers/d2fonts ./d2layouts/... ./e2etests -count=1 -timeout=30m
113117
- name: Verify representative release archive and size budget
114118
run: ./ci/release/build.sh --rebuild --run=linux/amd64 --version=v0.0.0-ci
115119
- uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2

‎THIRD_PARTY_NOTICES.txt‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -937,7 +937,12 @@ software is freely granted, provided that this notice
937937
is preserved.
938938

939939
TinyGo (WebAssembly build)
940-
https://github.com/tinygo-org/tinygo/tree/v0.44.0
940+
https://github.com/d2lang/tinygo/tree/v0.44.0-d2.3
941+
942+
The TinyGo runtime in the WebAssembly module and wasm_exec.js come from
943+
d2lang/tinygo v0.44.0-d2.3: TinyGo 0.44.0
944+
(https://github.com/tinygo-org/tinygo/tree/v0.44.0) with changes to its
945+
compiler, its runtime and wasm_exec.js.
941946

942947
Copyright (c) 2018-2026 The TinyGo Authors. All rights reserved.
943948

‎ci/build-wasm.sh‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
set -eu
33
cd "$(dirname "$0")/.."
44
. ./ci/sub/lib.sh
5+
. ./ci/wasm-flags.sh
56

67
# D2 and its npm package have independent versions.
78
d2_version=$(git_describe_ref)
89
js_version=${NPM_VERSION:-$(awk -F'"' '/"version"/ {print $4}' d2js/js/package.json)}
9-
# The default 64 KiB goroutine stack overflows during D2 initialization.
10-
exec tinygo build -target=wasm -stack-size=1MB -no-debug \
10+
out=${1:?expected output .wasm path}
11+
# -buildmode=c-shared keeps the module loaded after its initialization, which
12+
# is when d2js registers the API.
13+
# shellcheck disable=SC2086 # wasm_flags is a list of flags.
14+
tinygo build $wasm_flags -buildmode=c-shared \
1115
-ldflags="-X github.com/d2lang/d2/lib/version.Version=$d2_version -X github.com/d2lang/d2/d2js/d2wasm.jsVersion=$js_version" \
12-
-o "${1:?expected output .wasm path}" ./d2js
16+
-o "$out" ./d2js
17+
# -no-debug leaves the function names in the module.
18+
go run ./ci/wasm-strip "$out"
19+
# Fails if the module outgrows ci/wasm-size-budget.txt.
20+
go run ./ci/wasm-size "$out"

‎ci/checks.py‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,15 @@ def select_jobs(event, tag=False, paths=(), toolchain_changed=False):
4848
if path.startswith("d2layouts/d2talalayout/") or path in (".github/workflows/tala-fuzz.yml", "go.mod", "go.sum"):
4949
jobs["fuzz"] = True
5050

51-
wasm_specific = re.search(r"(?:^|[/_])(?:wasm|tinygo)(?:[_.]|$)", path)
51+
wasm_specific = re.search(r"(?:^|[/_])(?:wasm|tinygo)(?:[-_.]|$)", path)
5252
wasm_test = path.endswith("_test.go") and (
5353
path.startswith("d2js/d2wasm/") or (path.startswith("d2layouts/d2elklayout/") and wasm_specific)
5454
)
55-
if ci_policy or wasm_test or path in (".github/workflows/wasm.yml", "ci/test-wasm.sh"):
55+
# ci/test-wasm.sh builds the module with ci/wasm-flags.sh and checks its go statements against
56+
# ci/wasm-goroutines.txt. The module runs the *_nosched.go variants instead of goroutines.
57+
if ci_policy or wasm_test or path.endswith("_nosched.go") or path in (
58+
".github/workflows/wasm.yml", "ci/test-wasm.sh", "ci/wasm-flags.sh", "ci/wasm-goroutines.txt",
59+
):
5660
jobs["wasm"] = True
5761
if path.startswith(("d2js/", ".github/actions/browser-smoke/")) or path in (
5862
".github/workflows/browser-smoke.yml", ".github/workflows/npm-stage.yml", "ci/sub", ".gitmodules",

‎ci/checks_test.py‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ def test_pull_request_paths(self):
2424
(["d2renderers/internal/svgframe/frame.go"], {"browser": "native"}),
2525
(["d2js/js/src/worker.browser.js", "d2lib/board_reveal.go"], {"browser": "packed"}),
2626
(["ci/install-wasm-tools.sh"], {"browser": "packed"}),
27+
(["ci/wasm-strip/main.go"], {"browser": "packed"}),
28+
(["ci/wasm-flags.sh"], {"wasm": True}),
29+
(["ci/wasm-goroutines.txt"], {"wasm": True}),
30+
(["d2js/js_nosched.go"], {"wasm": True}),
31+
(["d2layouts/d2talalayout/seeds_nosched.go"], {"archives": "size", "fuzz": True, "wasm": True}),
2732
(["ci/sub"], {"archives": "full", "browser": "packed"}),
2833
([".gitmodules"], {"archives": "full", "browser": "packed"}),
2934
(["ci/test-wasm.sh", "d2lib/d2.go"], {"wasm": True}),

‎ci/install-wasm-tools.sh‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,16 @@
33
set -eu
44
cd "${RUNNER_TEMP:?}"
55

6-
tinygo_version=0.44.0
6+
# TinyGo comes from https://github.com/d2lang/tinygo: 0.44.0 with what the
7+
# build in ci/wasm-flags.sh needs.
8+
tinygo_version=0.44.0-d2.3
79
binaryen_version=version_133
810
tinygo=tinygo${tinygo_version}.linux-amd64.tar.gz
911
binaryen=binaryen-${binaryen_version}-x86_64-linux.tar.gz
10-
curl --retry 5 --retry-all-errors -fsSL "https://github.com/tinygo-org/tinygo/releases/download/v$tinygo_version/$tinygo" -o "$tinygo"
12+
curl --retry 5 --retry-all-errors -fsSL "https://github.com/d2lang/tinygo/releases/download/v$tinygo_version/$tinygo" -o "$tinygo"
1113
curl --retry 5 --retry-all-errors -fsSL "https://github.com/WebAssembly/binaryen/releases/download/$binaryen_version/$binaryen" -o "$binaryen"
1214
sha256sum --check --strict <<EOF
13-
ed0f45543a49ff2a99120a5a1b8814a0750e1fda1a66f8e2a0a172576134aaf9 $tinygo
15+
962c751b0fe9e7641f388af577231f805630949af2ded31069f1f48f722b11de $tinygo
1416
2dc9c7813f5375db93d96ead4b78222fcc3e2677bbb832297af4797782a37489 $binaryen
1517
EOF
1618
tar -xzf "$tinygo"

‎ci/release/changelogs/next.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@
3838

3939
- Build D2 with Go 1.27.4 and update its Go dependencies, including Brotli and the Go networking libraries. Source installs require Go 1.27.4 or newer.
4040

41+
- D2.js: the WebAssembly module is built with TinyGo instead of Go, reducing its Brotli-compressed size by about 40%. [#2966](https://github.com/d2lang/d2/pull/2966)
42+
43+
- D2.js: the TinyGo-built WebAssembly module is less than half the size (11.9 MB smaller) and a third smaller when compressed with Brotli (2.0 MB), and runs D2's end-to-end corpus in less than half the time, with identical output on that corpus. D2.js calls into the module and every call runs to completion, so the module is now built without a goroutine scheduler, which on WebAssembly rewrote every function that could be running when a goroutine is switched out and made the code about two thirds larger. The build needs TinyGo from [d2lang/tinygo](https://github.com/d2lang/tinygo/releases), which also gives each pointer a garbage-collector stack slot only while a collection can find it in use. The module also drops its function names, a second HTML entity table, two unused Unicode segmentation tables and four separately encoded copies of bundled fonts, stores the tables of the formula renderer and of the ELK layout engine as constant data, keeps the formula renderer's glyph outlines compressed until a formula needs them and its bundled fonts compressed until it starts, and runs D2's own sorts through one shared routine. [#3046](https://github.com/d2lang/d2/pull/3046)
44+
4145
- The d2 binary is about 3 MB smaller: PPTX export writes its XML parts without `text/template`, whose method lookups by name kept unused methods throughout the program. [#3000](https://github.com/d2lang/d2/pull/3000)
4246

4347
- TALA routes larger cross-container diagrams within its existing search budget. It preserves the original hierarchy lanes on compact diagrams and indexes previously routed segments. [#2891](https://github.com/d2lang/d2/issues/2891)

‎ci/test-wasm.sh‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
11
#!/bin/sh
22
set -eu
33
cd "$(dirname "$0")/.."
4+
. ./ci/wasm-flags.sh
45

6+
# The module must not have gained a go statement that it needs to run. Without
7+
# the tag that makes them inert, TinyGo lists the reachable ones instead of
8+
# building.
9+
dir=$(mktemp -d)
10+
trap 'rm -rf "$dir"' EXIT
11+
# shellcheck disable=SC2086 # wasm_flags is a list of flags.
12+
tinygo build $(printf '%s' "$wasm_flags" | sed 's/ -tags=goroutines.inert//') -buildmode=c-shared \
13+
-o "$dir/d2.wasm" ./d2js >"$dir/out.txt" 2>&1 || true
14+
# The go command reports module downloads on the same stream.
15+
grep -v '^go: downloading ' "$dir/out.txt" >"$dir/build.txt" || true
16+
message=': attempted to start a goroutine without a scheduler'
17+
if grep -v "$message\$" "$dir/build.txt" >&2; then
18+
echo "ci/test-wasm.sh: listing the module's go statements failed" >&2
19+
exit 1
20+
fi
21+
sed -e "s|$message\$||" \
22+
-e 's|^.*/pkg/mod/golang.org/toolchain@[^/]*/src/||' \
23+
-e "s|^$(go env GOROOT)/src/||" \
24+
-e "s|^$(tinygo env TINYGOROOT)/src/||" \
25+
-e 's|^.*/pkg/mod/||' "$dir/build.txt" | sort >"$dir/found.txt"
26+
grep -v '^#' ci/wasm-goroutines.txt | grep . | sort >"$dir/listed.txt"
27+
if ! diff "$dir/listed.txt" "$dir/found.txt" >&2; then
28+
echo "ci/test-wasm.sh: the go statements in the module (>) are not the ones in ci/wasm-goroutines.txt (<)" >&2
29+
exit 1
30+
fi
31+
32+
# TinyGo runs every test in a goroutine of its own, so the tests need the
33+
# scheduler that the module is built without. What is particular to the
34+
# module's build is covered by the go statements above and by d2.js's own
35+
# tests, which run the module.
536
CI=1 tinygo test -target=wasm -stack-size=1MB -no-debug -timeout=30m \
637
./d2js/d2wasm ./d2layouts/d2elklayout "$@"

‎ci/wasm-flags.sh‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#!/bin/sh
2+
# How D2 builds its WebAssembly module. Sourced by ci/build-wasm.sh and
3+
# ci/test-wasm.sh.
4+
#
5+
# D2.js calls into the module and every call runs to completion, so the module
6+
# is built without TinyGo's goroutine scheduler. On WebAssembly that scheduler
7+
# depends on Binaryen's asyncify pass, which rewrites every function that could
8+
# be running when a goroutine is switched out and made the code two thirds
9+
# larger.
10+
#
11+
# -scheduler=none No scheduler, and so no asyncify.
12+
# -panic-unwind=explicit recover still works: a panic returns through its
13+
# callers, which check for it after each call.
14+
# -tags=goroutines.inert A go statement starts nothing, where TinyGo would
15+
# otherwise refuse to build. ci/wasm-goroutines.txt
16+
# lists the ones in the module and why each can go
17+
# unstarted.
18+
# -stack-size=4MB Everything runs on one stack. With a scheduler each
19+
# call, and each TALA seed and route flavor inside
20+
# it, had 1 MB of its own.
21+
#
22+
# The tag, and calls from JavaScript into a module without a scheduler at all,
23+
# need TinyGo from https://github.com/d2lang/tinygo, which
24+
# ci/install-wasm-tools.sh installs.
25+
case $(tinygo version) in
26+
*-d2.*) ;;
27+
*)
28+
echo "The WebAssembly build needs TinyGo from https://github.com/d2lang/tinygo (see ci/install-wasm-tools.sh), found: $(tinygo version)" >&2
29+
exit 1
30+
;;
31+
esac
32+
wasm_flags="-target=wasm -scheduler=none -panic-unwind=explicit -tags=goroutines.inert -stack-size=4MB -no-debug"

‎ci/wasm-goroutines.txt‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# The go statements that D2's WebAssembly module can reach.
2+
#
3+
# The module is built without a goroutine scheduler (ci/wasm-flags.sh), so none
4+
# of them starts anything. ci/test-wasm.sh fails when this list no longer
5+
# matches the module. Before adding a line, make sure that the module can do
6+
# without that goroutine: nothing waits for it, and nothing needs what it would
7+
# have done. Code that fans out and collects, like TALA's seeds and route
8+
# flavors, needs a variant that runs in the caller instead (see the files with
9+
# the scheduler.none build tag).
10+
11+
# A cancelable context derived from one that is not the standard library's own
12+
# is watched by a goroutine. D2 derives from the standard ones only.
13+
context/context.go:523:2
14+
15+
# The clock behind regexp2's match timeouts, which Chroma's lexers set. It
16+
# could not tick during a call either.
17+
github.com/dlclark/regexp2/[email protected]/fastclock.go:90:3

0 commit comments

Comments
 (0)