From 763c7e8b173ea660d9e6682737c12d2be64fd9cf Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Wed, 25 Nov 2026 09:34:45 +0100 Subject: [PATCH 1/2] perf(builtin): index numbers in uniq Extend the string index from #1007 to integer and float values, keyed the same way runtime.Equal compares them. uniq(1..999999) drops from hours to ~200ms. --- builtin/builtin.go | 34 ++++--------- builtin/builtin_test.go | 49 ++++++++++++++++++ builtin/utils.go | 107 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 165 insertions(+), 25 deletions(-) diff --git a/builtin/builtin.go b/builtin/builtin.go index f86b7efd9..d0f349dc6 100644 --- a/builtin/builtin.go +++ b/builtin/builtin.go @@ -966,36 +966,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 } diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 6418654bd..35b555850 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -3,6 +3,8 @@ package builtin_test import ( "errors" "fmt" + "math" + "math/rand" "reflect" "strings" "testing" @@ -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) { @@ -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 := 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{ diff --git a/builtin/utils.go b/builtin/utils.go index 262bb379d..39f7206bd 100644 --- a/builtin/utils.go +++ b/builtin/utils.go @@ -6,6 +6,7 @@ import ( "time" "github.com/expr-lang/expr/internal/deref" + "github.com/expr-lang/expr/vm/runtime" ) var ( @@ -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 +} From 302e42255d7e6d1b31ffd28d8eb31c2d04f5261c Mon Sep 17 00:00:00 2001 From: Anton Medvedev Date: Wed, 25 Nov 2026 09:49:52 +0100 Subject: [PATCH 2/2] test(builtin): compile on 32-bit targets --- builtin/builtin_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/builtin/builtin_test.go b/builtin/builtin_test.go index 35b555850..964a002cf 100644 --- a/builtin/builtin_test.go +++ b/builtin/builtin_test.go @@ -68,7 +68,7 @@ func TestBuiltin_UniqStrings(t *testing.T) { func TestBuiltin_UniqNumbers(t *testing.T) { type namedInt int - big := 1 << 53 + 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), @@ -807,7 +807,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}, }