mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
internal/devicescale: use a slice instead of a map
runtime.mapaccess2 is one of heavy function calls. Updates #2601
This commit is contained in:
parent
4f2327536c
commit
10c9f489ce
@ -18,13 +18,14 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
)
|
)
|
||||||
|
|
||||||
type pos struct {
|
type posAndScale struct {
|
||||||
x, y int
|
x, y int
|
||||||
|
scale float64
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
m sync.Mutex
|
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.
|
// 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 {
|
func GetAt(x, y int) float64 {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
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)
|
s := impl(x, y)
|
||||||
cache[pos{x, y}] = s
|
cache = append(cache, posAndScale{
|
||||||
|
x: x,
|
||||||
|
y: y,
|
||||||
|
scale: s,
|
||||||
|
})
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -50,7 +59,6 @@ func ClearCache() {
|
|||||||
|
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
for k := range cache {
|
|
||||||
delete(cache, k)
|
cache = cache[:0]
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user