Skip to content

Commit 98ca172

Browse files
authored
Deprecate PutLocation and introduce SetLocation (#14019)
Part of #14016
1 parent 02fbe61 commit 98ca172

File tree

5 files changed

+181
-9
lines changed

5 files changed

+181
-9
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: deprecation
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata/pprofile
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Deprecated `PutLocation` helper method
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14019]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Use this changelog template to create an entry for release notes.
2+
3+
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
4+
change_type: enhancement
5+
6+
# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
7+
component: pdata/pprofile
8+
9+
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
10+
note: Introduce `SetLocation` helper method
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14019]
14+
15+
# (Optional) One or more lines of additional information to render under the primary note.
16+
# These lines will be padded with 2 spaces and then inserted directly into the document.
17+
# Use pipe (|) for multiline entries.
18+
subtext:
19+
20+
# Optional: The change log or logs in which this entry should be included.
21+
# e.g. '[user]' or '[user, api]'
22+
# Include 'user' if the change is relevant to end users.
23+
# Include 'api' if there is a change to a library API.
24+
# Default: '[user]'
25+
change_logs: [api]

pdata/pprofile/locations.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var (
3030

3131
// PutLocation updates a LocationTable and a Stack's LocationIndices to
3232
// add or update a location.
33+
//
34+
// Deprecated: [v0.137.0] use SetLocation instead.
3335
func PutLocation(table LocationSlice, record Stack, loc Location) error {
3436
for i, locIdx := range record.LocationIndices().All() {
3537
idx := int(locIdx)
@@ -47,22 +49,30 @@ func PutLocation(table LocationSlice, record Stack, loc Location) error {
4749
return errTooManyLocationIndicesEntries
4850
}
4951

52+
id, err := SetLocation(table, loc)
53+
if err != nil {
54+
return err
55+
}
56+
record.LocationIndices().Append(id)
57+
return nil
58+
}
59+
60+
// SetLocation updates a LocationTable, adding or providing a value and returns
61+
// its index.
62+
func SetLocation(table LocationSlice, loc Location) (int32, error) {
5063
for j, a := range table.All() {
5164
if a.Equal(loc) {
5265
if j > math.MaxInt32 {
53-
return errTooManyLocationTableEntries
66+
return 0, errTooManyLocationTableEntries
5467
}
55-
// Add the index of the existing location to the indices.
56-
record.LocationIndices().Append(int32(j)) //nolint:gosec // G115 overflow checked
57-
return nil
68+
return int32(j), nil //nolint:gosec // G115 overflow checked
5869
}
5970
}
6071

6172
if table.Len() >= math.MaxInt32 {
62-
return errTooManyLocationTableEntries
73+
return 0, errTooManyLocationTableEntries
6374
}
6475

6576
loc.CopyTo(table.AppendEmpty())
66-
record.LocationIndices().Append(int32(table.Len() - 1)) //nolint:gosec // G115 overflow checked
67-
return nil
77+
return int32(table.Len() - 1), nil //nolint:gosec // G115 overflow checked
6878
}

pdata/pprofile/locations_test.go

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,45 @@ func TestPutLocation(t *testing.T) {
8787
require.Equal(t, indicesLen, stack.LocationIndices().Len())
8888
}
8989

90+
func TestSetLocation(t *testing.T) {
91+
table := NewLocationSlice()
92+
l := NewLocation()
93+
l.SetAddress(1)
94+
l2 := NewLocation()
95+
l2.SetAddress(2)
96+
97+
// Put a first value
98+
idx, err := SetLocation(table, l)
99+
require.NoError(t, err)
100+
assert.Equal(t, 1, table.Len())
101+
assert.Equal(t, int32(0), idx)
102+
103+
// Put the same string
104+
// This should be a no-op.
105+
idx, err = SetLocation(table, l)
106+
require.NoError(t, err)
107+
assert.Equal(t, 1, table.Len())
108+
assert.Equal(t, int32(0), idx)
109+
110+
// Set a new value
111+
// This sets the index and adds to the table.
112+
idx, err = SetLocation(table, l2)
113+
require.NoError(t, err)
114+
assert.Equal(t, 2, table.Len())
115+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
116+
117+
// Set an existing value
118+
idx, err = SetLocation(table, l)
119+
require.NoError(t, err)
120+
assert.Equal(t, 2, table.Len())
121+
assert.Equal(t, int32(0), idx)
122+
// Set another existing value
123+
idx, err = SetLocation(table, l2)
124+
require.NoError(t, err)
125+
assert.Equal(t, 2, table.Len())
126+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
127+
}
128+
90129
func BenchmarkFromLocationIndices(b *testing.B) {
91130
table := NewLocationSlice()
92131

@@ -172,3 +211,72 @@ func BenchmarkPutLocation(b *testing.B) {
172211
})
173212
}
174213
}
214+
215+
func BenchmarkSetLocation(b *testing.B) {
216+
for _, bb := range []struct {
217+
name string
218+
location Location
219+
220+
runBefore func(*testing.B, LocationSlice)
221+
}{
222+
{
223+
name: "with a new location",
224+
location: NewLocation(),
225+
},
226+
{
227+
name: "with an existing location",
228+
location: func() Location {
229+
l := NewLocation()
230+
l.SetAddress(1)
231+
return l
232+
}(),
233+
234+
runBefore: func(_ *testing.B, table LocationSlice) {
235+
l := table.AppendEmpty()
236+
l.SetAddress(1)
237+
},
238+
},
239+
{
240+
name: "with a duplicate location",
241+
location: NewLocation(),
242+
243+
runBefore: func(_ *testing.B, table LocationSlice) {
244+
_, err := SetLocation(table, NewLocation())
245+
require.NoError(b, err)
246+
},
247+
},
248+
{
249+
name: "with a hundred locations to loop through",
250+
location: func() Location {
251+
l := NewLocation()
252+
l.SetMappingIndex(1)
253+
return l
254+
}(),
255+
256+
runBefore: func(_ *testing.B, table LocationSlice) {
257+
for i := range 100 {
258+
l := table.AppendEmpty()
259+
l.SetAddress(uint64(i)) //nolint:gosec // overflow checked
260+
}
261+
262+
l := table.AppendEmpty()
263+
l.SetMappingIndex(1)
264+
},
265+
},
266+
} {
267+
b.Run(bb.name, func(b *testing.B) {
268+
table := NewLocationSlice()
269+
270+
if bb.runBefore != nil {
271+
bb.runBefore(b, table)
272+
}
273+
274+
b.ResetTimer()
275+
b.ReportAllocs()
276+
277+
for b.Loop() {
278+
_, _ = SetLocation(table, bb.location)
279+
}
280+
})
281+
}
282+
}

pdata/testdata/profile.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ func fillProfileOne(dic pprofile.ProfilesDictionary, profile pprofile.Profile) {
5757

5858
loc := pprofile.NewLocation()
5959
loc.SetAddress(1)
60-
_ = pprofile.PutLocation(dic.LocationTable(), dic.StackTable().AppendEmpty(), loc)
60+
id, _ := pprofile.SetLocation(dic.LocationTable(), loc)
61+
stack := dic.StackTable().AppendEmpty()
62+
stack.LocationIndices().Append(id)
6163

6264
sample := profile.Sample().AppendEmpty()
6365
sample.SetStackIndex(1)
@@ -76,7 +78,9 @@ func fillProfileTwo(dic pprofile.ProfilesDictionary, profile pprofile.Profile) {
7678

7779
loc := pprofile.NewLocation()
7880
loc.SetAddress(2)
79-
_ = pprofile.PutLocation(dic.LocationTable(), dic.StackTable().AppendEmpty(), loc)
81+
id, _ := pprofile.SetLocation(dic.LocationTable(), loc)
82+
stack := dic.StackTable().AppendEmpty()
83+
stack.LocationIndices().Append(id)
8084

8185
sample := profile.Sample().AppendEmpty()
8286
sample.SetStackIndex(1)

0 commit comments

Comments
 (0)