From 7827e695ea2451b946ac35540434cdd985f29073 Mon Sep 17 00:00:00 2001 From: Fabrice Vaillant Date: Wed, 26 Jun 2024 11:40:55 +0200 Subject: [PATCH] Reduces allocation in attributes --- CHANGELOG.md | 1 + internal/attribute/attribute.go | 24 ++++++++++++------------ internal/attribute/attribute_test.go | 24 ++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41609fae5f5a..57b7288fe98d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -32,6 +32,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm ### Fixed +- Improved performance in all `{Bool,Int64,Float64,String}SliceValue` function of `go.opentelemetry.io/attributes` by reducing the number of allocations. (#5549) - Retry trace and span ID generation if it generated an invalid one in `go.opentelemetry.io/otel/sdk/trace`. (#5514) - Log a warning to the OpenTelemetry internal logger when a `Record` in `go.opentelemetry.io/otel/sdk/log` drops an attribute due to a limit being reached. (#5376) - Identify the `Tracer` returned from the global `TracerProvider` in `go.opentelemetry.io/otel/global` with its schema URL. (#5426) diff --git a/internal/attribute/attribute.go b/internal/attribute/attribute.go index f32766e57f66..822d84794741 100644 --- a/internal/attribute/attribute.go +++ b/internal/attribute/attribute.go @@ -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. diff --git a/internal/attribute/attribute_test.go b/internal/attribute/attribute_test.go index 7202700cac15..c0030c9cd6a7 100644 --- a/internal/attribute/attribute_test.go +++ b/internal/attribute/attribute_test.go @@ -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"}) + } +}