Skip to content

Commit

Permalink
Favour hashing strings in strmap
Browse files Browse the repository at this point in the history
  • Loading branch information
tonyhb committed Jan 31, 2024
1 parent 61f00a1 commit 941996a
Show file tree
Hide file tree
Showing 18 changed files with 910 additions and 4 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
17 changes: 14 additions & 3 deletions engine_stringmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package expr
import (
"context"
"fmt"
"strconv"
"sync"

"github.com/cespare/xxhash/v2"
"github.com/google/cel-go/common/operators"
"github.com/ohler55/ojg/jp"
"golang.org/x/sync/errgroup"
Expand Down Expand Up @@ -83,7 +85,16 @@ func (n *stringLookup) Search(ctx context.Context, variable string, input any) [
if !ok {
return nil
}
return n.strings[str]
return n.strings[n.hash(str)]
}

// hash hashes strings quickly via xxhash. this provides a _somewhat_ collision-free
// lookup while reducing memory for strings. note that internally, go maps store the
// raw key as a string, which uses extra memory. by compressing all strings via this
// hash, memory usage grows predictably even with long strings.
func (n *stringLookup) hash(input string) string {
ui := xxhash.Sum64String(input)
return strconv.FormatUint(ui, 36)
}

func (n *stringLookup) Add(ctx context.Context, p ExpressionPart) error {
Expand All @@ -93,7 +104,7 @@ func (n *stringLookup) Add(ctx context.Context, p ExpressionPart) error {

n.lock.Lock()
defer n.lock.Unlock()
val := p.Predicate.LiteralAsString()
val := n.hash(p.Predicate.LiteralAsString())

n.vars[p.Predicate.Ident] = struct{}{}

Expand All @@ -114,7 +125,7 @@ func (n *stringLookup) Remove(ctx context.Context, p ExpressionPart) error {
n.lock.Lock()
defer n.lock.Unlock()

val := p.Predicate.LiteralAsString()
val := n.hash(p.Predicate.LiteralAsString())

coll, ok := n.strings[val]
if !ok {
Expand Down
2 changes: 1 addition & 1 deletion engine_stringmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestEngineStringmap(t *testing.T) {
},
})
require.NoError(t, err)
require.Equal(t, 2, len(s.strings["123"]))
require.Equal(t, 2, len(s.strings[s.hash("123")]))
})
})

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/inngest/expr
go 1.21.0

require (
github.com/cespare/xxhash/v2 v2.2.0
github.com/google/cel-go v0.18.2
github.com/karlseguin/ccache/v2 v2.0.8
github.com/ohler55/ojg v1.21.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/antlr4-go/antlr/v4 v4.13.0 h1:lxCg3LAv+EUK6t1i0y1V6/SLeUi0eKEKdhQAlS8TVTI=
github.com/antlr4-go/antlr/v4 v4.13.0/go.mod h1:pfChB/xh/Unjila75QW7+VU4TSnWnnk9UTnmpPaOR2g=
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
22 changes: 22 additions & 0 deletions vendor/github.com/cespare/xxhash/v2/LICENSE.txt

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

72 changes: 72 additions & 0 deletions vendor/github.com/cespare/xxhash/v2/README.md

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

10 changes: 10 additions & 0 deletions vendor/github.com/cespare/xxhash/v2/testall.sh

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

Loading

0 comments on commit 941996a

Please sign in to comment.