Skip to content

perf(paint): share static font instances across renders - #444

Merged
tannevaled merged 1 commit into
mainfrom
fix/443-shared-instances
Nov 17, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/443-shared-instances

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Closes #443
Refs go-slides/slides#117

paint.Fonts.glyphFont built a static instance of a variable font
(opentype.(*Font).Instance) into a cache that lives on the per-render Fonts.
A vector export creates one Fonts per page (vectorpage.go), so every page
rebuilt every instance.

The instance cache is now process-wide (paint/sharedinstances.go), the
companion of the shared parsed fonts from PR 291:

  • key: (parsed font identity, weight already clamped to the font's own axis);
  • bound: 64 entries, oldest evicted first; a failed build is not kept (the next
    caller retries, and the page still draws the default master as before);
  • concurrent pages: a per-entry sync.Once makes them wait for one build; the
    map is behind a mutex; Fonts.instances stays as a lock-free per-render view.

Output is unchanged: the same instance bytes, the same fallback.

Tests (go test -race ./paint . green): TestNewFontsShareInstances (two
NewFonts build once, counted through the instantiate seam, clamped weights
share), TestSharedInstancesConcurrent (16 parallel pages, one build),
TestSharedInstancesBounded, and the existing TestGlyphFontInstance.

BenchmarkGlyphFontPages: a cold 20-page export, one NewFonts per page, 4
weights of a variable font each (Outfit-wght); -count=5, M4 Max, allocation
counts are the stable column.

allocs/op B/op ns/op (indicative)
before (v0.47.2+13) 2,265,405 412,514,268 ~102 ms
after 113,716 20,676,888 ~5.2 ms

(Before, each page re-instanced; after, only the first page does: 20x fewer
allocations.) README updated.

🤖 Generated with Claude Code

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@tannevaled

Copy link
Copy Markdown
Contributor Author

Downstream measurement: go-slides v0.26.0 with this branch (1aa085b) swapped in through a replace, against v0.26.0 as released (engine v0.47.2). slidesbench -decks generated-100 -modes pdf,pptx -cores 1,all -rounds 5, on an Apple M4 Max with 16 CPUs, under macOS 27.2.1, at a 1-minute load of 6.0 when the run started. Each figure is the median of 5 rounds after a discarded warm-up; the range in brackets is the minimum and maximum. Peak memory is the resident size of the whole process tree.

Mode Tool Wall (ms) Peak (MiB) Output
pdf v0.26.0, 16 cores (engine v0.47.2) 2692 [2654–2738] 2183 1005.5 KiB
pdf this branch, 1 core 557 [545–575] 177 792.4 KiB
pdf this branch, 16 cores 260 [254–261] 171 792.4 KiB
pdf marp-cli 4.5.1 1549 [1538–1563] 1183 835.8 KiB
pptx v0.26.0, 16 cores (engine v0.47.2) 2640 [2609–2742] 2003 995.6 KiB
pptx this branch, 1 core 710 [689–776] 176 995.6 KiB
pptx this branch, 16 cores 287 [283–320] 194 995.6 KiB
pptx marp-cli 4.5.1 7281 [7252–7841] 1053 7.2 MiB

This undoes the regression bisected in go-slides/slides#117. Peak memory goes from about 2.1 GiB to 177 MiB, back to the level before engine v0.45.10. On one core, the PDF is now about 2.8× faster than marp-cli.

The PDF is smaller because each page used to embed its own instance of the font. pdfops objects counts 102 FontFile entries in the v0.47.2 PDF and 3 with this branch. I drew all 100 pages of both PDFs at 72 dpi with pdfconv from-pdf and compared them with pngeq: all 100 are pixel-identical, with a largest channel difference of 0.

@tannevaled
tannevaled merged commit 7084679 into main Nov 17, 2026
13 checks passed
@tannevaled
tannevaled deleted the fix/443-shared-instances branch November 17, 2026 17:20
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.

paint: variable-font instances are rebuilt for every vector page (cache lives on the per-render Fonts)

1 participant