mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
text/v2: add glyphImageCache.atime
If many runes were added at the same time, GCing the current cache did nothing and wasted time.
This commit is contained in:
parent
c1315b3238
commit
c5974390a8
@ -44,6 +44,7 @@ type glyphImageCacheEntry struct {
|
||||
|
||||
type glyphImageCache[Key comparable] struct {
|
||||
cache map[Key]*glyphImageCacheEntry
|
||||
atime int64
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
@ -51,9 +52,11 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
g.m.Lock()
|
||||
defer g.m.Unlock()
|
||||
|
||||
n := now()
|
||||
|
||||
e, ok := g.cache[key]
|
||||
if ok {
|
||||
e.atime = now()
|
||||
e.atime = n
|
||||
return e.image
|
||||
}
|
||||
|
||||
@ -66,7 +69,7 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
image: img,
|
||||
}
|
||||
if img != nil {
|
||||
e.atime = now()
|
||||
e.atime = n
|
||||
} else {
|
||||
// If the glyph image is nil, the entry doesn't have to be removed.
|
||||
// Keep this until the face is GCed.
|
||||
@ -75,7 +78,7 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
g.cache[key] = e
|
||||
|
||||
// Clean up old entries.
|
||||
|
||||
if g.atime < n {
|
||||
// cacheSoftLimit indicates the soft limit of the number of glyphs in the cache.
|
||||
// If the number of glyphs exceeds this soft limits, old glyphs are removed.
|
||||
// Even after cleaning up the cache, the number of glyphs might still exceed the soft limit, but
|
||||
@ -90,6 +93,9 @@ func (g *glyphImageCache[Key]) getOrCreate(face Face, key Key, create func() *eb
|
||||
delete(g.cache, key)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
g.atime = n
|
||||
|
||||
return img
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user