Skip to content

[font] Tweak Cmap cache#258

Merged
benoitkugler merged 7 commits intomainfrom
cmap-cache
Apr 22, 2026
Merged

[font] Tweak Cmap cache#258
benoitkugler merged 7 commits intomainfrom
cmap-cache

Conversation

@benoitkugler
Copy link
Copy Markdown
Contributor

@benoitkugler benoitkugler commented Apr 19, 2026

Thanks to @redawl investigation, I realized our current implementation is not optimal because it passes the Cmap cache array by value, forcing a copy, while passing a pointer is more performant (see pointer.txt column).

The new benchmark results are now also better when adding a cache for not supported runes (see not-found-cache.txt column).

There is a small cost to pay when the "not supported" cache is not hit, but it is small enough.

Closes #252

. old.txt pointer.txt not-found-cache.txt
sec/op sec/op vs base sec/op vs base
Cmap/latin_text-4 1004.5n ± 8% 122.5n ± 6% -87.81% (p=0.000 n=10) 145.1n ± 7% -85.55% (p=0.000 n=10)
Cmap/chinese_text-4 769.50n ± 4% 362.50n ± 3% -52.89% (p=0.000 n=10) 93.69n ± 8% -87.83% (p=0.000 n=10)
Cmap/mixed_text-4 1773.5n ± 3% 488.4n ± 3% -72.46% (p=0.000 n=10) 244.5n ± 3% -86.22% (p=0.000 n=10)
geomean 1.111µ 278.8n -74.90% 149.2n -86.57%

Comment thread font/cmap_cache.go

// cache21_0_13 is a cache for integer keys,
// with 0 <= key < 2097152
type cache21_0_13 [1 << 13]uint8
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can we document that this cache performs best on sparse keys? It generates collisions for keys less than 256 values apart. That could be useful for implementing an LRU policy when using the cache (as I think we do), but it's not immediately obvious when looking at the implementation (I had to sit here for a while and work out how this cache could manage such a large keyspace with such small storage).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thank you, I have clarified the documentation and mentioned sparse keys.

Copy link
Copy Markdown
Member

@whereswaldon whereswaldon left a comment

Choose a reason for hiding this comment

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

Thanks for tackling this! I have one non-blocking request for implementation docs, but the rest looks great.

@benoitkugler benoitkugler merged commit 5523d5c into main Apr 22, 2026
14 checks passed
@benoitkugler benoitkugler deleted the cmap-cache branch April 22, 2026 14:27
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.

NominalGlyph lookups for glyphs not in the font are not cached.

2 participants