Skip to content

Commit 4f1e018

Browse files
authored
[shaping] adjust default cache size (#221)
1 parent 1b62c7c commit 4f1e018

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

shaping/lru.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ import (
55
"github.com/go-text/typesetting/harfbuzz"
66
)
77

8+
// adjust the cache size of the zero value, used by default
9+
// in [HarfbuzzShaper]
10+
const defaultFontCacheSize = 32
11+
812
// fontEntry holds a single key-value pair for an LRU cache.
913
type fontEntry struct {
1014
next, prev *fontEntry
@@ -21,7 +25,9 @@ type fontLRU struct {
2125
// https://git.sr.ht/~eliasnaur/gio/tree/e768fe347a732056031100f2c66987d6db258ea4/item/text/lru.go
2226
m map[*font.Font]*fontEntry
2327
head, tail *fontEntry
24-
maxSize int
28+
29+
// the actual cache size is [maxSizeOffset] + [defaultFontCacheSize]
30+
maxSizeOffset int
2531
}
2632

2733
// Get fetches the value associated with the given key, if any.
@@ -47,7 +53,7 @@ func (l *fontLRU) Put(k *font.Font, v *harfbuzz.Font) {
4753
val := &fontEntry{key: k, v: v}
4854
l.m[k] = val
4955
l.insert(val)
50-
if len(l.m) > l.maxSize {
56+
if len(l.m) > (defaultFontCacheSize + l.maxSizeOffset) {
5157
oldest := l.tail.next
5258
l.remove(oldest)
5359
delete(l.m, oldest.key)

shaping/shaping.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ type HarfbuzzShaper struct {
2424
// It is safe to adjust the size after using the shaper, though shrinking
2525
// it may result in many evictions on the next shaping.
2626
func (h *HarfbuzzShaper) SetFontCacheSize(size int) {
27-
h.fonts.maxSize = size
27+
h.fonts.maxSizeOffset = size - defaultFontCacheSize
2828
}
2929

3030
var _ Shaper = (*HarfbuzzShaper)(nil)

0 commit comments

Comments
 (0)