Add LRU cache for WHEREEVAL script

See #685
This commit is contained in:
tidwall 2023-05-11 03:18:18 -07:00
parent 5642fc42cc
commit 05fbeabdb4
4 changed files with 17 additions and 2 deletions

1
go.mod
View File

@ -31,6 +31,7 @@ require (
github.com/tidwall/resp v0.1.1
github.com/tidwall/rtree v1.9.2
github.com/tidwall/sjson v1.2.4
github.com/tidwall/tinylru v1.2.1
github.com/xdg/scram v1.0.5
github.com/yuin/gopher-lua v0.0.0-20200816102855-ee81675732da
go.uber.org/atomic v1.5.0

2
go.sum
View File

@ -391,6 +391,8 @@ github.com/tidwall/rtree v1.9.2 h1:6HiSU/bf4a7l2smEC+fEum/WloHMFCIQKWHjahm0Do8=
github.com/tidwall/rtree v1.9.2/go.mod h1:iDJQ9NBRtbfKkzZu02za+mIlaP+bjYPnunbSNidpbCQ=
github.com/tidwall/sjson v1.2.4 h1:cuiLzLnaMeBhRmEv00Lpk3tkYrcxpmbU81tAY4Dw0tc=
github.com/tidwall/sjson v1.2.4/go.mod h1:098SZ494YoMWPmMO6ct4dcFnqxwj9r/gF0Etp19pSNM=
github.com/tidwall/tinylru v1.2.1 h1:VgBr72c2IEr+V+pCdkPZUwiQ0KJknnWIYbhxAVkYfQk=
github.com/tidwall/tinylru v1.2.1/go.mod h1:9bQnEduwB6inr2Y7AkBP7JPgCkyrhTV/ZpX0oOOpBI4=
github.com/tidwall/tinyqueue v0.1.1 h1:SpNEvEggbpyN5DIReaJ2/1ndroY8iyEGxPYxoSaymYE=
github.com/tidwall/tinyqueue v0.1.1/go.mod h1:O/QNHwrnjqr6IHItYrzoHAKYhBkLI67Q096fQP5zMYw=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=

View File

@ -17,6 +17,7 @@ import (
"github.com/tidwall/geojson/geo"
"github.com/tidwall/resp"
"github.com/tidwall/tile38/internal/log"
"github.com/tidwall/tinylru"
lua "github.com/yuin/gopher-lua"
luajson "layeh.com/gopher-json"
)
@ -224,13 +225,17 @@ func (pl *lStatePool) Shutdown() {
type lScriptMap struct {
m sync.Mutex
scripts map[string]*lua.FunctionProto
lru tinylru.LRUG[string, *lua.FunctionProto]
}
func (sm *lScriptMap) Get(key string) (script *lua.FunctionProto, ok bool) {
sm.m.Lock()
script, ok = sm.scripts[key]
if !ok {
script, ok = sm.lru.Get(key)
}
sm.m.Unlock()
return
return script, ok
}
func (sm *lScriptMap) Put(key string, script *lua.FunctionProto) {
@ -239,9 +244,16 @@ func (sm *lScriptMap) Put(key string, script *lua.FunctionProto) {
sm.m.Unlock()
}
func (sm *lScriptMap) PutLRU(key string, script *lua.FunctionProto) {
sm.m.Lock()
sm.lru.Set(key, script)
sm.m.Unlock()
}
func (sm *lScriptMap) Flush() {
sm.m.Lock()
sm.scripts = make(map[string]*lua.FunctionProto)
sm.lru.Clear()
sm.m.Unlock()
}

View File

@ -426,7 +426,7 @@ func (s *Server) parseSearchScanBaseTokens(
err = makeSafeErr(err)
return
}
s.luascripts.Put(shaSum, fn.Proto)
s.luascripts.PutLRU(shaSum, fn.Proto)
}
t.whereevals = append(t.whereevals, whereevalT{
c: s, luaState: luaState, fn: fn,