mirror of
https://git.sr.ht/~eliasnaur/gio
synced 2026-07-06 01:45:36 +00:00
text/shape: remove Family.Reset by introducing LRU caches
It was easy to forget Family.Reset, and the per-frame caching strategy is probably too aggressive. Use a static size for the caches and evict according to a least recently used policy. Reset is then no longer required, and we can delete it. Signed-off-by: Elias Naur <mail@eliasnaur.com>
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
// SPDX-License-Identifier: Unlicense OR MIT
|
||||
|
||||
package shape
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"testing"
|
||||
|
||||
"gioui.org/op"
|
||||
)
|
||||
|
||||
func TestLayoutLRU(t *testing.T) {
|
||||
c := new(layoutCache)
|
||||
put := func(i int) {
|
||||
c.Put(layoutKey{str: strconv.Itoa(i)}, nil)
|
||||
}
|
||||
get := func(i int) bool {
|
||||
_, ok := c.Get(layoutKey{str: strconv.Itoa(i)})
|
||||
return ok
|
||||
}
|
||||
testLRU(t, put, get)
|
||||
}
|
||||
|
||||
func TestuPathLRU(t *testing.T) {
|
||||
c := new(pathCache)
|
||||
put := func(i int) {
|
||||
c.Put(pathKey{str: strconv.Itoa(i)}, op.MacroOp{})
|
||||
}
|
||||
get := func(i int) bool {
|
||||
_, ok := c.Get(pathKey{str: strconv.Itoa(i)})
|
||||
return ok
|
||||
}
|
||||
testLRU(t, put, get)
|
||||
}
|
||||
|
||||
func testLRU(t *testing.T, put func(i int), get func(i int) bool) {
|
||||
for i := 0; i < maxSize; i++ {
|
||||
put(i)
|
||||
}
|
||||
for i := 0; i < maxSize; i++ {
|
||||
if !get(i) {
|
||||
t.Fatalf("key %d was evicted", i)
|
||||
}
|
||||
}
|
||||
put(maxSize)
|
||||
for i := 1; i < maxSize+1; i++ {
|
||||
if !get(i) {
|
||||
t.Fatalf("key %d was evicted", i)
|
||||
}
|
||||
}
|
||||
if i := 0; get(i) {
|
||||
t.Fatalf("key %d was not evicted", i)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user