Repository navigation
[font] cache advances, pool the CFF interpreter, skip gvar point copies - #287
Merged
Merged
Conversation
egonelbre
requested review from
andydotxyz,
benoitkugler and
whereswaldon
as code owners
September 28, 2026 17:47
benoitkugler
requested changes
Sep 29, 2026
Contributor
There was a problem hiding this comment.
Thanks again for this PR.
Here, I'm a bit skeptical about the following points :
Face{}vsNewFace: we have adopted theNewFaceconstructor, why is it required to step back ? TheFace{}syntax should simply be avoided in all code (internal and external).- The advance cache is a nice addition. However, I feel like it should be separated in two instances, one for horizontal and one for vertical advances. For what I know, a large number of fonts do not include vertical metrics.
- Lastly, the svg caching seems nice : could you break it in a separate PR to ease the review ?
A zero Face is invalid, its cmap caches only work after clear(), so
NominalGlyph on one reports every rune below U+2000 as unsupported.
The package tests still built faces as Face{Font: f}. Use NewFace and
SetPpem everywhere.
On a variable font without HVAR or VVAR, every HorizontalAdvance and VerticalAdvance call extracted the full outline and applied the gvar deltas. The Face now caches the advance per glyph and SetCoords resets the cache. Horizontal and vertical advances live in separate slices, each allocated on first use, since most fonts are never asked for vertical advances. Each entry is a float32 with -1 marking an empty slot; advances are clamped to be non-negative, so no valid flag is needed. BenchmarkAdvanceNoHVar (15 glyphs, Apple M4 Max, count=6): old: 24.6µs ± 1% 85.3KiB/op 125 allocs/op new: 72ns ± 2% 0B/op 0 allocs/op -99.7% time
applyDeltasToPoints copied the full point list for every glyph. Only the inferred deltas read that copy, and only tuples with private point numbers produce inferred deltas. The copy must predate the first translation, so applyDeltasToPoints scans the tuples once before the loop and skips the copy when none has private point numbers. BenchmarkGlyphDataVar (15 glyphs, Apple M4 Max, count=6): old: 25.8µs ± 4% 113KiB/op 149 allocs/op new: 24.9µs ±33% 104KiB/op 137 allocs/op -7.9% B/op, -8% allocs
Every LoadGlyph heap-allocated a ps.Machine. The machine is about 4KB,
mostly argument stack. LoadGlyph now takes it from a sync.Pool and
clears it before returning it, so the pool holds no font data.
Machine.Run resets all the state it reads. Font is shared across
goroutines, hence a pool rather than one mutex-guarded machine.
BenchmarkGlyphData (200 glyphs, Apple M4 Max, count=6):
CFFTest.otf old: 3.97µs ±21% 28.4KiB/op 39 allocs/op
new: 1.69µs ±18% 4.6KiB/op 34 allocs/op -57.5% time
NotoSansCJKjp-VF old: 190µs ±55% 1244KiB/op 1850 allocs/op
new: 151µs ± 1% 294KiB/op 1650 allocs/op -20.4% time
Roboto-BoldItalic (glyf, unaffected): 133µs -> 121µs, ~ (p=0.065)
egonelbre
force-pushed
the
perf/font-face
branch
from
September 29, 2026 13:13
e0d1cd7 to
8967aa9
Compare
Contributor
Author
|
@benoitkugler done. Yeah, I did think about Face{}, but yeah, fixing tests makes more sense. SVG changes moved to #294. And I split the advances, the measurements from the differences were: |
benoitkugler
approved these changes
Sep 30, 2026
benoitkugler
left a comment
Contributor
There was a problem hiding this comment.
Very nice, thanks !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Three allocation or repeated-work removals in glyph loading, plus a test cleanup. Every commit carries its benchmark, measured against its parent.
The tests built faces as Face{Font: f}, which skips the cache init that NewFace does. They now use NewFace and SetPpem. No library code changes in that commit.
Cache advances on variable fonts without HVAR/VVAR. Each HorizontalAdvance or VerticalAdvance call extracted the full outline and applied the gvar deltas. Horizontal and vertical advances go in separate slices, each allocated on first use, since most fonts are never asked for vertical advances. An entry is a float32 with -1 for empty; advances are clamped non-negative so no flag is needed. SetCoords resets both. About 340x faster, zero allocations.
Skip copying the original gvar points unless a tuple has private point numbers. 8% fewer allocations on variable glyphs.
Pool the 4 KB CFF interpreter machine instead of allocating one per glyph. About 2x faster on CFF fonts, 6x less memory. TestLoadGlyphConcurrent checks that concurrent loads on one Font do not share state.