Skip to content

Commit

Permalink
Add LRU cache for WHEREEVAL script
Browse files Browse the repository at this point in the history
See #685
  • Loading branch information
tidwall committed May 11, 2023
1 parent 5642fc4 commit 05fbeab
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
14 changes: 13 additions & 1 deletion internal/server/scripts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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) {
Expand All @@ -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()
}

Expand Down
2 changes: 1 addition & 1 deletion internal/server/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 05fbeab

Please sign in to comment.