Skip to content

perf(styles): parse embedded styles lazily - #1354

Merged
alecthomas merged 2 commits into
alecthomas:masterfrom
mattn:perf-lazy-styles
Aug 18, 2026
Merged

perf(styles): parse embedded styles lazily#1354
alecthomas merged 2 commits into
alecthomas:masterfrom
mattn:perf-lazy-styles

Conversation

@mattn

@mattn mattn commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Importing the styles package parses all 79 embedded style XML files at init. This parses each style lazily on first lookup instead, keyed by file name, which by convention is the lowercased style name — a new test enforces that invariant and that every embedded style parses. Only the swapoff fallback is parsed eagerly, and the registry is now mutex-guarded, so concurrent use is safer than before.

GODEBUG=inittrace=1 init clock init heap
before 5.4 ms 2.2 MB / 44,621 allocs
after 0.06 ms 14 KB / 225 allocs

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

Importing the styles package parsed all embedded style XML files during
init, costing about 5.4 ms and 44k allocations before main starts. Parse
each style on first lookup instead, keyed by file name, which by
convention is the lowercased style name; a new test enforces that
invariant and that every embedded style parses. Registry access is now
guarded by a mutex, so concurrent use is safer than before. Only the
swapoff fallback style is parsed eagerly.
Comment thread styles/api.go Outdated
@alecthomas
alecthomas merged commit 3af0c4c 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