Skip to content

[font] cache advances, pool the CFF interpreter, skip gvar point copies - #287

Merged
benoitkugler merged 4 commits into
go-text:mainfrom
egonelbre:perf/font-face
Sep 30, 2026
Merged

benoitkugler merged 4 commits into
go-text:mainfrom
egonelbre:perf/font-face

Conversation

@egonelbre

@egonelbre egonelbre commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

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.

@benoitkugler benoitkugler left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks again for this PR.

Here, I'm a bit skeptical about the following points :

  • Face{} vs NewFace : we have adopted the NewFace constructor, why is it required to step back ? The Face{} 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 egonelbre changed the title [font] make the zero-value Face valid, cache advances and SVG documents, pool the CFF interpreter [font] cache advances, pool the CFF interpreter, skip gvar point copies Sep 29, 2026
@egonelbre

Copy link
Copy Markdown
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:

┌───────────────────────────────┬──────────────┐
│            variant            │ ns/op (n=10) │
├───────────────────────────────┼──────────────┤
│ combined struct, one slice    │ 85.5 ± 3%    │
├───────────────────────────────┼──────────────┤
│ split, struct with valid flag │ 70.5 ± 2%    │
├───────────────────────────────┼──────────────┤
│ split, float32 with -1        │ 72.5 ± 2%    │
└───────────────────────────────┴──────────────┘

@benoitkugler benoitkugler left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Very nice, thanks !

@benoitkugler
benoitkugler merged commit 55f71f9 into go-text:main Sep 30, 2026
7 checks passed
@egonelbre
egonelbre deleted the perf/font-face branch September 30, 2026 09: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.

2 participants