Skip to content

Commit

Permalink
Reduces allocation in attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Succo authored and Fabrice Vaillant committed Jun 27, 2024
1 parent 6d45f28 commit 667e7f3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
24 changes: 12 additions & 12 deletions internal/attribute/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ import (
// BoolSliceValue converts a bool slice into an array with same elements as slice.
func BoolSliceValue(v []bool) interface{} {
var zero bool
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// Int64SliceValue converts an int64 slice into an array with same elements as slice.
func Int64SliceValue(v []int64) interface{} {
var zero int64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]int64), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// Float64SliceValue converts a float64 slice into an array with same elements as slice.
func Float64SliceValue(v []float64) interface{} {
var zero float64
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]float64), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// StringSliceValue converts a string slice into an array with same elements as slice.
func StringSliceValue(v []string) interface{} {
var zero string
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
copy(cp.Elem().Slice(0, len(v)).Interface().([]string), v)
return cp.Elem().Interface()
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
reflect.Copy(cp, reflect.ValueOf(v))
return cp.Interface()
}

// AsBoolSlice converts a bool array into a slice into with same elements as array.
Expand Down
24 changes: 24 additions & 0 deletions internal/attribute/attribute_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,27 @@ func TestSliceValue(t *testing.T) {
})
}
}

func BenchmarkBoolSliceValue(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = BoolSliceValue([]bool{true, false, true, false})
}
}

func BenchmarkInt64SliceValue(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = Int64SliceValue([]int64{1, 2, 3, 4})
}
}

func BenchmarkFloat64SliceValue(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = Float64SliceValue([]float64{1.2, 3.4, 5.6, 7.8})
}
}

func BenchmarkStringSliceValue(b *testing.B) {
for n := 0; n < b.N; n++ {
_ = StringSliceValue([]string{"a", "b", "c", "d"})
}
}

0 comments on commit 667e7f3

Please sign in to comment.