From 1392129e417c13d1917570da434cb00524e29add Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 12 Mar 2019 01:31:11 +0900 Subject: [PATCH] devicescale: Cache scale values --- internal/devicescale/devicescale.go | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/internal/devicescale/devicescale.go b/internal/devicescale/devicescale.go index 470663834..c2c646e98 100644 --- a/internal/devicescale/devicescale.go +++ b/internal/devicescale/devicescale.go @@ -18,12 +18,22 @@ import ( "sync" ) +type pos struct { + x, y int +} + var ( - m sync.Mutex + m sync.Mutex + cache = map[pos]float64{} ) func GetAt(x, y int) float64 { m.Lock() defer m.Unlock() - return impl(x, y) + if s, ok := cache[pos{x, y}]; ok { + return s + } + s := impl(x, y) + cache[pos{x, y}] = s + return s }