Skip to content

Commit

Permalink
Make tree lookups concurrent
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Jan 8, 2024
1 parent 0e0c630 commit 2209dd4
Show file tree
Hide file tree
Showing 10 changed files with 270 additions and 30 deletions.
79 changes: 51 additions & 28 deletions expr.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/google/cel-go/common/operators"
"github.com/ohler55/ojg/jp"
"golang.org/x/sync/errgroup"
)

var (
Expand Down Expand Up @@ -201,8 +202,16 @@ func (a *aggregator) AggregateMatch(ctx context.Context, data map[string]any) ([
counts := map[groupID]int{}
// Store all expression parts per group ID for returning.
found := map[groupID][]ExpressionPart{}
// protect the above locks with a map.
lock := &sync.Mutex{}
// run lookups concurrently.
eg := errgroup.Group{}

add := func(all []ExpressionPart) {
// This is called concurrently, so don't mess with maps in goroutines
lock.Lock()
defer lock.Unlock()

for _, eval := range all {
counts[eval.GroupID] += 1
if _, ok := found[eval.GroupID]; !ok {
Expand All @@ -218,40 +227,54 @@ func (a *aggregator) AggregateMatch(ctx context.Context, data map[string]any) ([
//
// TODO: we should iterate through the expression in a top-down order, ensuring that if
// any of the top groups fail to match we quit early.
for path, tree := range a.artIdents {
x, err := jp.ParseString(path)
if err != nil {
return nil, err
}
res := x.Get(data)
if len(res) != 1 {
continue
}
for n, item := range a.artIdents {
tree := item
path := n
eg.Go(func() error {
x, err := jp.ParseString(path)
if err != nil {
return err
}
res := x.Get(data)
if len(res) != 1 {
return nil
}

cast, ok := res[0].(string)
if !ok {
// This isn't a string, so we can't compare within the radix tree.
continue
}
cast, ok := res[0].(string)
if !ok {
// This isn't a string, so we can't compare within the radix tree.
return nil
}

add(tree.Search(ctx, path, cast))
add(tree.Search(ctx, path, cast))
return nil
})
}

// Match on nulls.
for path, tree := range a.nullLookups {
x, err := jp.ParseString(path)
if err != nil {
return nil, err
}
for n, item := range a.nullLookups {
tree := item
path := n
eg.Go(func() error {
x, err := jp.ParseString(path)
if err != nil {
return err
}

res := x.Get(data)
if len(res) == 0 {
// This isn't present, which matches null in our overloads. Set the
// value to nil.
res = []any{nil}
}
// This matches null, nil (as null), and any non-null items.
add(tree.Search(ctx, path, res[0]))
res := x.Get(data)
if len(res) == 0 {
// This isn't present, which matches null in our overloads. Set the
// value to nil.
res = []any{nil}
}
// This matches null, nil (as null), and any non-null items.
add(tree.Search(ctx, path, res[0]))
return nil
})
}

if err := eg.Wait(); err != nil {
return nil, err
}

for k, count := range counts {
Expand Down
2 changes: 1 addition & 1 deletion expr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ func TestEmptyExpressions(t *testing.T) {
})
}

func TestNull(t *testing.T) {
func TestEvaluate_Null(t *testing.T) {
ctx := context.Background()
parser, err := newParser()
require.NoError(t, err)
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ require (
github.com/ohler55/ojg v1.21.0
github.com/plar/go-adaptive-radix-tree v1.0.5
github.com/stretchr/testify v1.8.4
golang.org/x/sync v0.6.0
google.golang.org/protobuf v1.31.0
)

require (
Expand All @@ -19,6 +21,5 @@ require (
golang.org/x/text v0.9.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ github.com/wsxiaoys/terminal v0.0.0-20160513160801-0940f3fc43a0 h1:3UeQBvD0TFrlV
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/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
27 changes: 27 additions & 0 deletions vendor/golang.org/x/sync/LICENSE

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

22 changes: 22 additions & 0 deletions vendor/golang.org/x/sync/PATENTS

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

135 changes: 135 additions & 0 deletions vendor/golang.org/x/sync/errgroup/errgroup.go

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

13 changes: 13 additions & 0 deletions vendor/golang.org/x/sync/errgroup/go120.go

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

14 changes: 14 additions & 0 deletions vendor/golang.org/x/sync/errgroup/pre_go120.go

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

3 changes: 3 additions & 0 deletions vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ github.com/stretchr/testify/require
## explicit; go 1.20
golang.org/x/exp/constraints
golang.org/x/exp/slices
# golang.org/x/sync v0.6.0
## explicit; go 1.18
golang.org/x/sync/errgroup
# golang.org/x/text v0.9.0
## explicit; go 1.17
golang.org/x/text/transform
Expand Down

0 comments on commit 2209dd4

Please sign in to comment.