From d9797423e54b53469693cf04b3e28675f86dedf6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 20 Aug 2023 04:58:36 +0900 Subject: [PATCH] ebiten, internal/builtinshader: use an array instead of a map runtime.mapaccess2 is one of heavy function calls. Updates #2601 --- internal/builtinshader/shader.go | 23 ++++++++++------------- shader.go | 19 ++++++------------- 2 files changed, 16 insertions(+), 26 deletions(-) diff --git a/internal/builtinshader/shader.go b/internal/builtinshader/shader.go index ed8fcea0c..620c0535c 100644 --- a/internal/builtinshader/shader.go +++ b/internal/builtinshader/shader.go @@ -28,6 +28,8 @@ const ( FilterLinear ) +const FilterCount = 2 + type Address int const ( @@ -36,19 +38,15 @@ const ( AddressRepeat ) +const AddressCount = 3 + const ( UniformColorMBody = "ColorMBody" UniformColorMTranslation = "ColorMTranslation" ) -type key struct { - Filter Filter - Address Address - UseColorM bool -} - var ( - shaders = map[key][]byte{} + shaders [FilterCount][AddressCount][2][]byte shadersM sync.Mutex ) @@ -132,12 +130,11 @@ func Shader(filter Filter, address Address, useColorM bool) []byte { shadersM.Lock() defer shadersM.Unlock() - k := key{ - Filter: filter, - Address: address, - UseColorM: useColorM, + var c int + if useColorM { + c = 1 } - if s, ok := shaders[k]; ok { + if s := shaders[filter][address][c]; s != nil { return s } @@ -165,6 +162,6 @@ func Shader(filter Filter, address Address, useColorM bool) []byte { } b := buf.Bytes() - shaders[k] = b + shaders[filter][address][c] = b return b } diff --git a/shader.go b/shader.go index 6c23774b9..e815c3d13 100644 --- a/shader.go +++ b/shader.go @@ -59,14 +59,8 @@ func (s *Shader) appendUniforms(dst []uint32, uniforms map[string]any) []uint32 return s.shader.AppendUniforms(dst, uniforms) } -type builtinShaderKey struct { - filter builtinshader.Filter - address builtinshader.Address - useColorM bool -} - var ( - builtinShaders = map[builtinShaderKey]*Shader{} + builtinShaders [builtinshader.FilterCount][builtinshader.AddressCount][2]*Shader builtinShadersM sync.Mutex ) @@ -74,12 +68,11 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u builtinShadersM.Lock() defer builtinShadersM.Unlock() - key := builtinShaderKey{ - filter: filter, - address: address, - useColorM: useColorM, + var c int + if useColorM { + c = 1 } - if s, ok := builtinShaders[key]; ok { + if s := builtinShaders[filter][address][c]; s != nil { return s } @@ -100,6 +93,6 @@ func builtinShader(filter builtinshader.Filter, address builtinshader.Address, u shader = s } - builtinShaders[key] = shader + builtinShaders[filter][address][c] = shader return shader }