Skip to content

Commit f3a2d96

Browse files
authored
Reduces allocation in attributes (#5549)
Remove one allocation in all SliceValue function (going from 3 to 2). Here is benchstat results ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/internal/attribute cpu: 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ BoolSliceValue-8 128.4n ± 22% 103.8n ± 25% -19.12% (p=0.007 n=10) Int64SliceValue-8 167.9n ± 7% 130.8n ± 5% -22.13% (p=0.000 n=10) Float64SliceValue-8 133.8n ± 14% 122.6n ± 4% -8.33% (p=0.000 n=10) StringSliceValue-8 166.4n ± 9% 158.5n ± 10% -4.75% (p=0.037 n=10) geomean 148.0n 127.5n -13.88% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ BoolSliceValue-8 32.000 ± 0% 8.000 ± 0% -75.00% (p=0.000 n=10) Int64SliceValue-8 88.00 ± 0% 64.00 ± 0% -27.27% (p=0.000 n=10) Float64SliceValue-8 88.00 ± 0% 64.00 ± 0% -27.27% (p=0.000 n=10) StringSliceValue-8 152.0 ± 0% 128.0 ± 0% -15.79% (p=0.000 n=10) geomean 78.34 45.25 -42.23% │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ BoolSliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) Int64SliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) Float64SliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) StringSliceValue-8 3.000 ± 0% 2.000 ± 0% -33.33% (p=0.000 n=10) geomean 3.000 2.000 -33.33% ```
1 parent 1d783e1 commit f3a2d96

File tree

3 files changed

+52
-12
lines changed

3 files changed

+52
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
3333

3434
### Fixed
3535

36+
- Improved performance in all `{Bool,Int64,Float64,String}SliceValue` function of `go.opentelemetry.io/attributes` by reducing the number of allocations. (#5549)
3637
- Retry trace and span ID generation if it generated an invalid one in `go.opentelemetry.io/otel/sdk/trace`. (#5514)
3738
- 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)
3839
- Identify the `Tracer` returned from the global `TracerProvider` in `go.opentelemetry.io/otel/global` with its schema URL. (#5426)

internal/attribute/attribute.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,33 +14,33 @@ import (
1414
// BoolSliceValue converts a bool slice into an array with same elements as slice.
1515
func BoolSliceValue(v []bool) interface{} {
1616
var zero bool
17-
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero)))
18-
copy(cp.Elem().Slice(0, len(v)).Interface().([]bool), v)
19-
return cp.Elem().Interface()
17+
cp := reflect.New(reflect.ArrayOf(len(v), reflect.TypeOf(zero))).Elem()
18+
reflect.Copy(cp, reflect.ValueOf(v))
19+
return cp.Interface()
2020
}
2121

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

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

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

4646
// AsBoolSlice converts a bool array into a slice into with same elements as array.

internal/attribute/attribute_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,3 +94,42 @@ func TestSliceValue(t *testing.T) {
9494
})
9595
}
9696
}
97+
98+
// sync is a global used to ensure the benchmark are not optimized away.
99+
var sync any
100+
101+
func BenchmarkBoolSliceValue(b *testing.B) {
102+
b.ReportAllocs()
103+
s := []bool{true, false, true, false}
104+
b.ResetTimer()
105+
for n := 0; n < b.N; n++ {
106+
sync = BoolSliceValue(s)
107+
}
108+
}
109+
110+
func BenchmarkInt64SliceValue(b *testing.B) {
111+
b.ReportAllocs()
112+
s := []int64{1, 2, 3, 4}
113+
b.ResetTimer()
114+
for n := 0; n < b.N; n++ {
115+
sync = Int64SliceValue(s)
116+
}
117+
}
118+
119+
func BenchmarkFloat64SliceValue(b *testing.B) {
120+
b.ReportAllocs()
121+
s := []float64{1.2, 3.4, 5.6, 7.8}
122+
b.ResetTimer()
123+
for n := 0; n < b.N; n++ {
124+
sync = Float64SliceValue(s)
125+
}
126+
}
127+
128+
func BenchmarkStringSliceValue(b *testing.B) {
129+
b.ReportAllocs()
130+
s := []string{"a", "b", "c", "d"}
131+
b.ResetTimer()
132+
for n := 0; n < b.N; n++ {
133+
sync = StringSliceValue(s)
134+
}
135+
}

0 commit comments

Comments
 (0)