Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 9 additions & 25 deletions builtin/builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -993,36 +993,20 @@ var Builtins = []*Function{

size := v.Len()
ret := []any{}
var seenStrings map[string]struct{}

eq := func(value any) bool {
for _, r := range ret {
if runtime.Equal(value, r) {
return true
}
}

return false
}
// Scan small sets linearly and index them once they grow.
var index *uniqIndex

for i := 0; i < size; i += 1 {
value := v.Index(i).Interface()
// Strings can only equal other strings in runtime.Equal.
// Index them once the scan grows, keeping small sets cheap.
if str, ok := value.(string); ok && len(ret) >= 16 {
if seenStrings == nil {
seenStrings = make(map[string]struct{})
for _, r := range ret {
if s, ok := r.(string); ok {
seenStrings[s] = struct{}{}
}
}
}
if _, exists := seenStrings[str]; exists {
if index == nil && len(ret) >= 16 {
index = newUniqIndex(ret)
}
if index != nil {
if index.has(value) {
continue
}
seenStrings[str] = struct{}{}
} else if eq(value) {
index.add(value)
} else if containsEqual(ret, value) {
continue
}

Expand Down
51 changes: 50 additions & 1 deletion builtin/builtin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package builtin_test
import (
"errors"
"fmt"
"math"
"math/rand"
"reflect"
"strings"
"testing"
Expand All @@ -18,6 +20,7 @@ import (
"github.com/expr-lang/expr/conf"
"github.com/expr-lang/expr/parser"
"github.com/expr-lang/expr/test/mock"
"github.com/expr-lang/expr/vm/runtime"
)

func TestBuiltin_UniqStrings(t *testing.T) {
Expand Down Expand Up @@ -63,6 +66,52 @@ func TestBuiltin_UniqStrings(t *testing.T) {
require.Equal(t, want, got)
}

func TestBuiltin_UniqNumbers(t *testing.T) {
type namedInt int
big := int64(1) << 53
base := []any{
1, int8(1), uint64(1), float32(1), 1.0, 1.5, float32(1.5),
-1, uint64(math.MaxUint64), uint(math.MaxUint), int64(-1), math.Exp2(64),
big, big + 1, float64(big), float64(big + 1), uint64(big + 1),
0, math.Copysign(0, -1), math.NaN(), math.NaN(), math.Inf(1), math.Inf(-1),
namedInt(1), time.Duration(1), true, nil, "1", []any{1},
}
for i := 0; i < 40; i++ {
base = append(base, i, float64(i)/2)
}

// The indexed path must agree with a plain runtime.Equal scan in any order.
rnd := rand.New(rand.NewSource(1))
for n := 0; n < 50; n++ {
values := append(append([]any{}, base...), base...)
rnd.Shuffle(len(values), func(i, j int) { values[i], values[j] = values[j], values[i] })

want := []any{}
next:
for _, v := range values {
for _, w := range want {
if runtime.Equal(v, w) {
continue next
}
}
want = append(want, v)
}

env := map[string]any{"values": values}
got, err := expr.Eval(`uniq(values)`, env)
require.NoError(t, err)
require.Equal(t, len(want), len(got.([]any)))
for i := range want {
w, g := want[i], got.([]any)[i]
if f, ok := w.(float64); ok && math.IsNaN(f) {
require.True(t, math.IsNaN(g.(float64)))
continue
}
require.Equal(t, w, g, "index %d", i)
}
}
}

func TestBuiltin(t *testing.T) {
ArrayWithNil := []any{42}
env := map[string]any{
Expand Down Expand Up @@ -816,7 +865,7 @@ func TestBuiltin_bitOpsFunc(t *testing.T) {
{`bitxor(15, 32)`, 47},
{`bitshl(39, 3)`, 312},
{`bitshr(5, 1)`, 2},
{`bitushr(-5, 2)`, 4611686018427387902},
{`bitushr(-5, 2)`, int((math.MaxUint - 4) >> 2)}, // uint(-5) >> 2
{`bitnand(35, 9)`, 34},
}

Expand Down
107 changes: 107 additions & 0 deletions builtin/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/expr-lang/expr/internal/deref"
"github.com/expr-lang/expr/vm/runtime"
)

var (
Expand Down Expand Up @@ -88,3 +89,109 @@ func bitFunc(name string, fn func(x, y int) (any, error)) *Function {
Types: types(new(func(int, int) int)),
}
}

func containsEqual(values []any, value any) bool {
for _, v := range values {
if runtime.Equal(value, v) {
return true
}
}
return false
}

// uniqIndex finds values equal under runtime.Equal without a linear scan for
// strings and numbers. Strings only equal strings. Integers equal integers
// via int(x) and floats via float64(x), so integers are also keyed as
// float64 for float lookups. Everything else falls back to a scan.
type uniqIndex struct {
strings map[string]struct{}
ints map[int]struct{}
intFloats map[float64]struct{}
floats map[float64]struct{}
other []any
}

func newUniqIndex(values []any) *uniqIndex {
u := &uniqIndex{
strings: map[string]struct{}{},
ints: map[int]struct{}{},
intFloats: map[float64]struct{}{},
floats: map[float64]struct{}{},
}
for _, v := range values {
u.add(v)
}
return u
}

func (u *uniqIndex) has(value any) bool {
if s, ok := value.(string); ok {
_, found := u.strings[s]
return found
}
if i, f, ok := intKeys(value); ok {
_, found := u.ints[i]
if !found {
_, found = u.floats[f]
}
return found
}
if f, ok := floatKey(value); ok {
_, found := u.floats[f]
if !found {
_, found = u.intFloats[f]
}
return found
}
return containsEqual(u.other, value)
}

func (u *uniqIndex) add(value any) {
if s, ok := value.(string); ok {
u.strings[s] = struct{}{}
} else if i, f, ok := intKeys(value); ok {
u.ints[i] = struct{}{}
u.intFloats[f] = struct{}{}
} else if f, ok := floatKey(value); ok {
u.floats[f] = struct{}{}
} else {
u.other = append(u.other, value)
}
}

// intKeys mirrors the conversions runtime.Equal applies to integers.
func intKeys(value any) (int, float64, bool) {
switch x := value.(type) {
case int:
return int(x), float64(x), true
case int8:
return int(x), float64(x), true
case int16:
return int(x), float64(x), true
case int32:
return int(x), float64(x), true
case int64:
return int(x), float64(x), true
case uint:
return int(x), float64(x), true
case uint8:
return int(x), float64(x), true
case uint16:
return int(x), float64(x), true
case uint32:
return int(x), float64(x), true
case uint64:
return int(x), float64(x), true
}
return 0, 0, false
}

func floatKey(value any) (float64, bool) {
switch x := value.(type) {
case float32:
return float64(x), true
case float64:
return x, true
}
return 0, false
}
Loading