Skip to content

Commit e0a0548

Browse files
committed
reduce allocations
1 parent 9b27b44 commit e0a0548

File tree

2 files changed

+37
-17
lines changed

2 files changed

+37
-17
lines changed

pdata/pprofile/attributes.go

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,38 @@ type attributable interface {
1313

1414
// BuildAttributes builds a [pcommon.Map] containing the attributes of a record.
1515
// The record can by any struct that implements an `AttributeIndices` method.
16-
func BuildAttributes(profile Profile, record attributable) (pcommon.Map, error) {
17-
attrs := map[string]any{}
16+
func BuildAttributes(profile Profile, record attributable) pcommon.Map {
17+
m := pcommon.NewMap()
18+
19+
for i := 0; i < record.AttributeIndices().Len(); i++ {
20+
id := int(record.AttributeIndices().At(i))
21+
a := profile.AttributeTable().At(id)
1822

19-
for i := range record.AttributeIndices().AsRaw() {
20-
a := profile.AttributeTable().At(i)
21-
attrs[a.Key()] = a.Value().AsRaw()
23+
key := a.Key()
24+
val := a.Value()
25+
26+
switch val.Type() {
27+
case pcommon.ValueTypeEmpty:
28+
m.PutStr(key, "")
29+
case pcommon.ValueTypeStr:
30+
m.PutStr(key, val.AsString())
31+
case pcommon.ValueTypeBool:
32+
m.PutBool(key, val.Bool())
33+
case pcommon.ValueTypeDouble:
34+
m.PutDouble(key, val.Double())
35+
case pcommon.ValueTypeInt:
36+
m.PutInt(key, val.Int())
37+
case pcommon.ValueTypeBytes:
38+
bs := m.PutEmptyBytes(key)
39+
val.Bytes().CopyTo(bs)
40+
case pcommon.ValueTypeMap:
41+
ma := m.PutEmptyMap(key)
42+
val.Map().CopyTo(ma)
43+
case pcommon.ValueTypeSlice:
44+
sl := m.PutEmptySlice(key)
45+
val.Slice().CopyTo(sl)
46+
}
2247
}
2348

24-
m := pcommon.NewMap()
25-
err := m.FromRaw(attrs)
26-
return m, err
49+
return m
2750
}

pdata/pprofile/attributes_test.go

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,24 @@ func TestBuildAttributes(t *testing.T) {
2222
att2.SetKey("bonjour")
2323
att2.Value().SetStr("monde")
2424

25-
attrs, err := BuildAttributes(profile, profile)
26-
require.NoError(t, err)
25+
attrs := BuildAttributes(profile, profile)
2726
assert.Equal(t, attrs, pcommon.NewMap())
2827

2928
// A Location with a single attribute
3029
loc := NewLocation()
31-
loc.AttributeIndices().Append(1)
30+
loc.AttributeIndices().Append(0)
3231

33-
attrs, err = BuildAttributes(profile, loc)
34-
require.NoError(t, err)
32+
attrs = BuildAttributes(profile, loc)
3533

3634
m := pcommon.NewMap()
3735
require.NoError(t, m.FromRaw(map[string]any{"hello": "world"}))
3836
assert.Equal(t, attrs, m)
3937

4038
// A Mapping with two attributes
4139
mapp := NewLocation()
42-
mapp.AttributeIndices().Append(1, 2)
40+
mapp.AttributeIndices().Append(0, 1)
4341

44-
attrs, err = BuildAttributes(profile, mapp)
45-
require.NoError(t, err)
42+
attrs = BuildAttributes(profile, mapp)
4643

4744
m = pcommon.NewMap()
4845
require.NoError(t, m.FromRaw(map[string]any{"hello": "world", "bonjour": "monde"}))
@@ -65,6 +62,6 @@ func BenchmarkBuildAttributes(b *testing.B) {
6562
b.ReportAllocs()
6663

6764
for n := 0; n < b.N; n++ {
68-
_, _ = BuildAttributes(profile, obj)
65+
_ = BuildAttributes(profile, obj)
6966
}
7067
}

0 commit comments

Comments
 (0)