From 05fbeabdb49f5213ccb1221bb9724b9695456944 Mon Sep 17 00:00:00 2001 From: tidwall Date: Thu, 11 May 2023 03:18:18 -0700 Subject: [PATCH] Add LRU cache for WHEREEVAL script See #685 --- go.mod | 1 + go.sum | 2 ++ internal/server/scripts.go | 14 +++++++++++++- internal/server/token.go | 2 +- 4 files changed, 17 insertions(+), 2 deletions(-) diff --git a/go.mod b/go.mod index f8f926eb..ca456945 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index ad184a38..517e0409 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/internal/server/scripts.go b/internal/server/scripts.go index 072ce185..d8abb3c5 100644 --- a/internal/server/scripts.go +++ b/internal/server/scripts.go @@ -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() } diff --git a/internal/server/token.go b/internal/server/token.go index 24b35a12..2b3ed628 100644 --- a/internal/server/token.go +++ b/internal/server/token.go @@ -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,