Skip to content

Commit a035f59

Browse files
committed
[DO NOT MERGE] [pdata] Try append for slice.CopyTo
1 parent c6cd1ae commit a035f59

File tree

3 files changed

+66
-5
lines changed

3 files changed

+66
-5
lines changed

pdata/internal/wrapper_slice.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,9 @@ func NewSlice(orig *[]otlpcommon.AnyValue, state *State) Slice {
2525
}
2626

2727
func CopyOrigSlice(dest, src []otlpcommon.AnyValue) []otlpcommon.AnyValue {
28-
if cap(dest) < len(src) {
29-
dest = make([]otlpcommon.AnyValue, len(src))
30-
}
31-
dest = dest[:len(src)]
28+
dest = dest[:0]
3229
for i := 0; i < len(src); i++ {
33-
CopyOrigValue(&dest[i], &src[i])
30+
dest = append(dest, CopyValue(src[i]))
3431
}
3532
return dest
3633
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
package internal // import "go.opentelemetry.io/collector/pdata/internal"
5+
6+
import (
7+
"testing"
8+
9+
otlpcommon "go.opentelemetry.io/collector/pdata/internal/data/protogen/common/v1"
10+
)
11+
12+
func BenchmarkCopyOrigSlice(b *testing.B) {
13+
tests := []struct {
14+
name string
15+
srcLen int
16+
}{
17+
{name: "0_to_7", srcLen: 0},
18+
{name: "1_to_7", srcLen: 1},
19+
{name: "7_to_7", srcLen: 7},
20+
{name: "10_to_7", srcLen: 10},
21+
{name: "20_to_7", srcLen: 20},
22+
{name: "50_to_7", srcLen: 50},
23+
}
24+
25+
for _, tt := range tests {
26+
b.Run(tt.name, func(b *testing.B) {
27+
src := make([]otlpcommon.AnyValue, tt.srcLen)
28+
for i := 0; i < tt.srcLen; i++ {
29+
src[i] = otlpcommon.AnyValue{Value: &otlpcommon.AnyValue_StringValue{StringValue: "test_value"}}
30+
}
31+
b.ResetTimer()
32+
b.ReportAllocs()
33+
for i := 0; i < b.N; i++ {
34+
dest := GenerateTestSlice()
35+
CopyOrigSlice(*dest.orig, src)
36+
}
37+
})
38+
}
39+
}

pdata/internal/wrapper_value.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,31 @@ func CopyOrigValue(dest, src *otlpcommon.AnyValue) {
6262
}
6363
}
6464

65+
func CopyValue(src otlpcommon.AnyValue) otlpcommon.AnyValue {
66+
switch sv := src.Value.(type) {
67+
case *otlpcommon.AnyValue_KvlistValue:
68+
mv := &otlpcommon.AnyValue_KvlistValue{KvlistValue: &otlpcommon.KeyValueList{}}
69+
if sv.KvlistValue != nil {
70+
mv.KvlistValue.Values = CopyOrigMap(mv.KvlistValue.Values, sv.KvlistValue.Values)
71+
}
72+
return otlpcommon.AnyValue{Value: mv}
73+
case *otlpcommon.AnyValue_ArrayValue:
74+
dv := &otlpcommon.AnyValue_ArrayValue{ArrayValue: &otlpcommon.ArrayValue{}}
75+
if sv.ArrayValue != nil {
76+
dv.ArrayValue.Values = CopyOrigSlice(dv.ArrayValue.Values, sv.ArrayValue.Values)
77+
}
78+
return otlpcommon.AnyValue{Value: dv}
79+
case *otlpcommon.AnyValue_BytesValue:
80+
bv := &otlpcommon.AnyValue_BytesValue{}
81+
bv.BytesValue = make([]byte, len(sv.BytesValue))
82+
copy(bv.BytesValue, sv.BytesValue)
83+
return otlpcommon.AnyValue{Value: bv}
84+
default:
85+
// Primitive immutable type, no need for deep copy.
86+
return otlpcommon.AnyValue{Value: sv}
87+
}
88+
}
89+
6590
func FillTestValue(dest Value) {
6691
dest.orig.Value = &otlpcommon.AnyValue_StringValue{StringValue: "v"}
6792
}

0 commit comments

Comments
 (0)