Skip to content

perf(svg): defer embedded font encoding until output is written - #1355

Merged
alecthomas merged 1 commit into
alecthomas:masterfrom
mattn:perf-lazy-svg-font
Aug 18, 2026
Merged

perf(svg): defer embedded font encoding until output is written#1355
alecthomas merged 1 commit into
alecthomas:masterfrom
mattn:perf-lazy-svg-font

Conversation

@mattn

@mattn mattn commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Registering the default SVG formatter base64-encodes the embedded 390 KB Liberation Mono font at init, in every program importing the formatters package. This adds an EmbedFontProvider option that takes the font as a func() string and defers the call until the formatter actually writes output. EmbedFont is unchanged and now wraps it.

GODEBUG=inittrace=1 init clock init heap
before 0.57 ms 509 KB
after 0.14 ms 34 KB

Part of a series reducing the cost of importing chroma (lexers + styles + formatters) from ~15.4 ms to ~1.1 ms.

Registering the default SVG formatter base64-encoded the embedded
390 KB Liberation Mono font during init, costing about 0.4 ms and half a
megabyte of allocations in every program importing the formatters
package. Add an EmbedFontProvider option that takes the font as a
function, and only call it when the formatter actually writes output.
EmbedFont is unchanged and now wraps EmbedFontProvider.

@alecthomas alecthomas left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@alecthomas
alecthomas merged commit cf4a8fb into alecthomas:master Aug 18, 2026
2 checks passed
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.

2 participants