Skip to content

Commit

Permalink
Add ccache
Browse files Browse the repository at this point in the history
One day we'll look at modern LRU algorithms, such as S3LRU or Sieve.
  • Loading branch information
tonyhb committed Jan 5, 2024
1 parent 83de348 commit 5a753fe
Show file tree
Hide file tree
Showing 17 changed files with 1,386 additions and 15 deletions.
27 changes: 16 additions & 11 deletions caching_parser.go
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
package expr

import (
"sync"
"sync/atomic"
"time"

"github.com/google/cel-go/cel"
// "github.com/karlseguin/ccache/v2"
"github.com/karlseguin/ccache/v2"
)

var (
CacheTime = time.Hour

replace = []string{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t"}
)

// NewCachingParser returns a CELParser which lifts quoted literals out of the expression
// as variables and uses caching to cache expression parsing, resulting in improved
// performance when parsing expressions.
func NewCachingParser(env *cel.Env) CELParser {
func NewCachingParser(env *cel.Env, cache *ccache.Cache) CELParser {
if cache == nil {
cache = ccache.New(ccache.Configure())
}
return &cachingParser{
env: env,
cache: cache,
env: env,
}
}

type cachingParser struct {
// cache is a global cache of precompiled expressions.
// cache *ccache.Cache
stupidNoInternetCache sync.Map
cache *ccache.Cache

env *cel.Env

Expand All @@ -35,20 +40,20 @@ type cachingParser struct {
func (c *cachingParser) Parse(expr string) (*cel.Ast, *cel.Issues, LiftedArgs) {
expr, vars := liftLiterals(expr)

// TODO: ccache, when I have internet.
if cached, ok := c.stupidNoInternetCache.Load(expr); ok {
p := cached.(ParsedCelExpr)
if cached := c.cache.Get(expr); cached != nil {
cached.Extend(CacheTime)
p := cached.Value().(ParsedCelExpr)
atomic.AddInt64(&c.hits, 1)
return p.AST, p.Issues, vars
}

ast, issues := c.env.Parse(expr)

c.stupidNoInternetCache.Store(expr, ParsedCelExpr{
c.cache.Set(expr, ParsedCelExpr{
Expr: expr,
AST: ast,
Issues: issues,
})
}, CacheTime)

atomic.AddInt64(&c.misses, 1)
return ast, issues, vars
Expand Down
8 changes: 5 additions & 3 deletions expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ import (
"github.com/stretchr/testify/require"
)

func BenchmarkCachingEvaluate1_000(b *testing.B) { benchEval(1_000, NewCachingParser(newEnv()), b) }
func BenchmarkCachingEvaluate1_000(b *testing.B) {
benchEval(1_000, NewCachingParser(newEnv(), nil), b)
}

// func BenchmarkNonCachingEvaluate1_000(b *testing.B) { benchEval(1_000, EnvParser(newEnv()), b) }

Expand Down Expand Up @@ -71,7 +73,7 @@ func evaluate(b *testing.B, i int, parser TreeParser) error {

func TestEvaluate(t *testing.T) {
ctx := context.Background()
parser, err := NewTreeParser(NewCachingParser(newEnv()))
parser, err := NewTreeParser(NewCachingParser(newEnv(), nil))
require.NoError(t, err)
e := NewAggregateEvaluator(parser, testBoolEvaluator)

Expand Down Expand Up @@ -159,7 +161,7 @@ func TestEvaluate(t *testing.T) {

func TestEvaluate_ArrayIndexes(t *testing.T) {
ctx := context.Background()
parser, err := NewTreeParser(NewCachingParser(newEnv()))
parser, err := NewTreeParser(NewCachingParser(newEnv(), nil))
require.NoError(t, err)
e := NewAggregateEvaluator(parser, testBoolEvaluator)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go 1.21.0

require (
github.com/google/cel-go v0.18.2
github.com/karlseguin/ccache/v2 v2.0.8
github.com/ohler55/ojg v1.21.0
github.com/plar/go-adaptive-radix-tree v1.0.5
github.com/stretchr/testify v1.8.4
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ github.com/google/cel-go v0.18.2/go.mod h1:kWcIzTsPX0zmQ+H3TirHstLLf9ep5QTsZBN9u
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/karlseguin/ccache/v2 v2.0.8 h1:lT38cE//uyf6KcFok0rlgXtGFBWxkI6h/qg4tbFyDnA=
github.com/karlseguin/ccache/v2 v2.0.8/go.mod h1:2BDThcfQMf/c0jnZowt16eW405XIqZPavt+HoYEtcxQ=
github.com/karlseguin/expect v1.0.2-0.20190806010014-778a5f0c6003 h1:vJ0Snvo+SLMY72r5J4sEfkuE7AFbixEP2qRbEcum/wA=
github.com/karlseguin/expect v1.0.2-0.20190806010014-778a5f0c6003/go.mod h1:zNBxMY8P21owkeogJELCLeHIt+voOSduHYTFUbwRAV8=
github.com/ohler55/ojg v1.21.0 h1:niqSS6yl3PQZJrqh7pKs/zinl4HebGe8urXEfpvlpYY=
github.com/ohler55/ojg v1.21.0/go.mod h1:gQhDVpQLqrmnd2eqGAvJtn+NfKoYJbe/A4Sj3/Vro4o=
github.com/plar/go-adaptive-radix-tree v1.0.5 h1:rHR89qy/6c24TBAHullFMrJsU9hGlKmPibdBGU6/gbM=
Expand All @@ -26,6 +30,8 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0 h1:3UeQBvD0TFrlVjOeLOBz+CPAI8dnbqNSVwUwRrkp7vQ=
github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0/go.mod h1:IXCdmsXIht47RaVFLEdVnh1t+pgYtTAhQGj73kz+2DM=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc h1:mCRnTeVUjcrhlRmO0VK8a6k6Rrf6TF9htwo2pJVSjIU=
golang.org/x/exp v0.0.0-20230515195305-f3d0a9c9a5cc/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
Expand Down
2 changes: 1 addition & 1 deletion parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -979,7 +979,7 @@ func TestParse(t *testing.T) {
func TestParse_LiftedVars(t *testing.T) {
ctx := context.Background()

cachingCelParser := NewCachingParser(newEnv())
cachingCelParser := NewCachingParser(newEnv(), nil)

assert := func(t *testing.T, tests []parseTestInput) {
t.Helper()
Expand Down
1 change: 1 addition & 0 deletions vendor/github.com/karlseguin/ccache/v2/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 5 additions & 0 deletions vendor/github.com/karlseguin/ccache/v2/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

105 changes: 105 additions & 0 deletions vendor/github.com/karlseguin/ccache/v2/bucket.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 5a753fe

Please sign in to comment.