Skip to content

svg: collect font and palette usage while drawing - #2995

Merged
alixander merged 2 commits into
masterfrom
perf/svg-write-time-usage
Nov 21, 2026
Merged

alixander merged 2 commits into
masterfrom
perf/svg-write-time-usage

Conversation

@alixander

@alixander alixander commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Human


AI

The renderer now records font glyphs and theme classes while drawing common shapes, connections, class rows and SQL tables. This avoids decoding the completed SVG to rediscover those values. Recording glyphs in drawing order preserves the font subset bytes.

Objects containing arbitrary SVG still go through the XML decoder so nested text and inherited styles keep their existing semantics. Adjacent objects share a decoder pass, and separated fragments reuse its input buffer. A palette fallback can run independently of the font collector. Sketch renderings and custom stylesheets retain their existing collection behavior.

Performance and tradeoffs

For an eight-actor sequence with 1,000 messages, median warm SVG time fell from 24.4 to 16.3 ms across 40 paired label edits. Compile plus render took 36.0 to 29.5 ms. First-call SVG time in a fresh process went from 25.8 to 18.5 ms across 20 pairs. Process startup is outside those timers.

The geometry and output-mode benchmarks below time the full d2svg.Render call with a precomputed layout and warm font caches. Each workload had 12 paired samples on an Apple M4 with Go 1.27.4. The time-change column shows the median within-pair change and a 95% paired bootstrap interval. Time and allocation columns show standalone medians. MB is cumulative allocation volume per render.

Workload Milliseconds, parent → PR Paired time change (95% interval) MB allocated, parent → PR
TPMJS architecture 7.31 → 5.58 -33.4% (-37.1% to -19.0%) 5.36 → 4.35
256 ovals 5.96 → 4.18 -30.9% (-33.3% to -25.5%) 4.58 → 3.54
256 flat rectangles 5.23 → 3.81 -29.7% (-34.9% to -20.0%) 4.30 → 3.24
256 rectangles, alternating flat and 3D 9.80 → 8.68 -7.7% (-19.7% to -5.2%) 8.00 → 7.56
256 rectangles, every eighth shape 3D 6.59 → 4.95 -27.1% (-40.7% to -9.5%) 5.19 → 4.42
256 3D shapes, last shape triggers a palette fallback 18.28 → 18.20 -4.6% (-14.4% to +7.4%) 14.18 → 13.33
8,553-character Markdown label 16.91 → 16.50 +1.4% (-7.1% to +20.7%) 11.37 → 11.29
Queue-workers with a custom base stylesheet 2.16 → 2.29 +10.0% (-1.8% to +13.6%) 2.06 → 1.95
Queue-workers in sketch mode 7.19 → 8.11 +4.0% (-2.4% to +25.8%) 5.61 → 5.35
Queue-workers as an embedded board 1.27 → 1.26 +0.4% (-19.1% to +9.0%) 1.22 → 1.21

An early version allocated about 40% more on the 256-oval board by opening a reader per shape. Ovals now report their classes directly. Alternating flat and 3D objects also exposed the cost of allocating a fresh XML input buffer per fragment. Reusing that buffer brought the final allocation volume to 7.56 MB against 8.00 MB on the parent. Three workloads had small or noisy timing changes in the first run. A separate, longer run used 24 paired samples per workload and 30 to 150 renders per sample. Its paired time changes were custom stylesheet -0.8% (-6.3% to +7.3%), Markdown +0.3% (-3.2% to +7.2%), sketch mode +1.5% (-6.8% to +4.6%). Intervals are 95% paired bootstrap estimates on this shared machine.

The extra maintenance cost is keeping the writer's font and class callbacks consistent with the XML collector as drawing code changes. The stripped native CLI grew by 320 bytes. The raw release-style TinyGo WASM grew by 7,771 bytes. Its Brotli-compressed download grew by 2,285 bytes (5,999,098 → 6,001,383 bytes).

Verification

  • The full Go suite and go test -race ./d2renderers/d2svg ./d2themes passed.
  • 1,024 saved target boards rendered in light, dark-switching, sketch, embedded-board and custom-stylesheet modes produced 5,120 byte-identical SVGs against the parent.
  • Five TinyGo WASM cases for shapes and connections, classes, Unicode, sequences and Markdown matched the parent's SVG hashes. The sequence timing harness checked the complete SVG hash on all 180 measured pairs.
go test -run '^$' -bench '^BenchmarkSVGMetadata$' -benchmem ./d2renderers/d2svg
  • sent from alixander's Codex

@alixander
alixander merged commit 25e67b4 into master Nov 21, 2026
14 checks passed
@alixander
alixander deleted the perf/svg-write-time-usage branch November 21, 2026 08:43
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