internal/devicescale: use a slice instead of a map

runtime.mapaccess2 is one of heavy function calls.

Updates #2601
This commit is contained in:
Hajime Hoshi 2023-08-20 04:55:50 +09:00
parent 4f2327536c
commit 10c9f489ce

View File

@ -18,13 +18,14 @@ import (
"sync"
)
type pos struct {
x, y int
type posAndScale struct {
x, y int
scale float64
}
var (
m sync.Mutex
cache = map[pos]float64{}
cache []posAndScale
)
// GetAt returns the device scale at (x, y), i.e. the number of device-dependent pixels per device-independent pixel.
@ -32,11 +33,19 @@ var (
func GetAt(x, y int) float64 {
m.Lock()
defer m.Unlock()
if s, ok := cache[pos{x, y}]; ok {
return s
for _, p := range cache {
if p.x == x && p.y == y {
return p.scale
}
}
s := impl(x, y)
cache[pos{x, y}] = s
cache = append(cache, posAndScale{
x: x,
y: y,
scale: s,
})
return s
}
@ -50,7 +59,6 @@ func ClearCache() {
m.Lock()
defer m.Unlock()
for k := range cache {
delete(cache, k)
}
cache = cache[:0]
}