Skip to content

Commit 0e6e67d

Browse files
authored
Change SetMapping signature to return the mapping's ID, not update (#14030)
Part of #14016
1 parent 848b7b6 commit 0e6e67d

File tree

3 files changed

+55
-53
lines changed

3 files changed

+55
-53
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: breaking
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: Update `SetMapping` to return the mapping's ID rather than update the Location
11+
12+
# One or more tracking issues or pull requests related to the change
13+
issues: [14016, 14030]
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/mappings.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,43 +5,27 @@ package pprofile // import "go.opentelemetry.io/collector/pdata/pprofile"
55

66
import (
77
"errors"
8-
"fmt"
98
"math"
109
)
1110

1211
var errTooManyMappingTableEntries = errors.New("too many entries in MappingTable")
1312

14-
// SetMapping updates a MappingTable and a Location's MappingIndex to
15-
// add or update a mapping.
16-
func SetMapping(table MappingSlice, record Location, ma Mapping) error {
17-
idx := int(record.MappingIndex())
18-
if idx > 0 {
19-
if idx >= table.Len() {
20-
return fmt.Errorf("index value %d out of range for MappingIndex", idx)
21-
}
22-
mapAt := table.At(idx)
23-
if mapAt.Equal(ma) {
24-
// Mapping already exists, nothing to do.
25-
return nil
26-
}
27-
}
28-
13+
// SetMapping updates a MappingTable, adding or providing a value and returns
14+
// its index.
15+
func SetMapping(table MappingSlice, ma Mapping) (int32, error) {
2916
for j, m := range table.All() {
3017
if m.Equal(ma) {
3118
if j > math.MaxInt32 {
32-
return errTooManyMappingTableEntries
19+
return 0, errTooManyMappingTableEntries
3320
}
34-
// Add the index of the existing mapping to the indices.
35-
record.SetMappingIndex(int32(j)) //nolint:gosec // G115 overflow checked
36-
return nil
21+
return int32(j), nil //nolint:gosec // G115 overflow checked
3722
}
3823
}
3924

4025
if table.Len() >= math.MaxInt32 {
41-
return errTooManyMappingTableEntries
26+
return 0, errTooManyMappingTableEntries
4227
}
4328

4429
ma.CopyTo(table.AppendEmpty())
45-
record.SetMappingIndex(int32(table.Len() - 1)) //nolint:gosec // G115 overflow checked
46-
return nil
30+
return int32(table.Len() - 1), nil //nolint:gosec // G115 overflow checked
4731
}

pdata/pprofile/mappings_test.go

Lines changed: 23 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,52 +16,45 @@ func TestSetMapping(t *testing.T) {
1616
m.SetMemoryLimit(1)
1717
m2 := NewMapping()
1818
m2.SetMemoryLimit(2)
19-
loc := NewLocation()
2019

2120
// Put a first mapping
22-
require.NoError(t, SetMapping(table, loc, m))
21+
idx, err := SetMapping(table, m)
22+
require.NoError(t, err)
2323
assert.Equal(t, 1, table.Len())
24-
assert.Equal(t, int32(0), loc.MappingIndex())
24+
assert.Equal(t, int32(0), idx)
2525

2626
// Put the same mapping
2727
// This should be a no-op.
28-
require.NoError(t, SetMapping(table, loc, m))
28+
idx, err = SetMapping(table, m)
29+
require.NoError(t, err)
2930
assert.Equal(t, 1, table.Len())
30-
assert.Equal(t, int32(0), loc.MappingIndex())
31+
assert.Equal(t, int32(0), idx)
3132

3233
// Set a new mapping
3334
// This sets the index and adds to the table.
34-
require.NoError(t, SetMapping(table, loc, m2))
35+
idx, err = SetMapping(table, m2)
36+
require.NoError(t, err)
3537
assert.Equal(t, 2, table.Len())
36-
assert.Equal(t, int32(table.Len()-1), loc.MappingIndex()) //nolint:gosec // G115
38+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
3739

3840
// Set an existing mapping
39-
require.NoError(t, SetMapping(table, loc, m))
41+
idx, err = SetMapping(table, m)
42+
require.NoError(t, err)
4043
assert.Equal(t, 2, table.Len())
41-
assert.Equal(t, int32(0), loc.MappingIndex())
44+
assert.Equal(t, int32(0), idx)
4245
// Set another existing mapping
43-
require.NoError(t, SetMapping(table, loc, m2))
46+
idx, err = SetMapping(table, m2)
47+
require.NoError(t, err)
4448
assert.Equal(t, 2, table.Len())
45-
assert.Equal(t, int32(table.Len()-1), loc.MappingIndex()) //nolint:gosec // G115
46-
}
47-
48-
func TestSetMappingCurrentTooHigh(t *testing.T) {
49-
table := NewMappingSlice()
50-
loc := NewLocation()
51-
loc.SetMappingIndex(42)
52-
53-
err := SetMapping(table, loc, NewMapping())
54-
require.Error(t, err)
55-
assert.Equal(t, 0, table.Len())
56-
assert.Equal(t, int32(42), loc.MappingIndex())
49+
assert.Equal(t, int32(table.Len()-1), idx) //nolint:gosec // G115
5750
}
5851

5952
func BenchmarkSetMapping(b *testing.B) {
6053
for _, bb := range []struct {
6154
name string
6255
mapping Mapping
6356

64-
runBefore func(*testing.B, MappingSlice, Location)
57+
runBefore func(*testing.B, MappingSlice)
6558
}{
6659
{
6760
name: "with a new mapping",
@@ -75,7 +68,7 @@ func BenchmarkSetMapping(b *testing.B) {
7568
return m
7669
}(),
7770

78-
runBefore: func(_ *testing.B, table MappingSlice, _ Location) {
71+
runBefore: func(_ *testing.B, table MappingSlice) {
7972
m := table.AppendEmpty()
8073
m.SetMemoryLimit(1)
8174
},
@@ -84,8 +77,9 @@ func BenchmarkSetMapping(b *testing.B) {
8477
name: "with a duplicate mapping",
8578
mapping: NewMapping(),
8679

87-
runBefore: func(_ *testing.B, table MappingSlice, obj Location) {
88-
require.NoError(b, SetMapping(table, obj, NewMapping()))
80+
runBefore: func(_ *testing.B, table MappingSlice) {
81+
_, err := SetMapping(table, NewMapping())
82+
require.NoError(b, err)
8983
},
9084
},
9185
{
@@ -96,7 +90,7 @@ func BenchmarkSetMapping(b *testing.B) {
9690
return m
9791
}(),
9892

99-
runBefore: func(_ *testing.B, table MappingSlice, _ Location) {
93+
runBefore: func(_ *testing.B, table MappingSlice) {
10094
for i := range 100 {
10195
m := table.AppendEmpty()
10296
m.SetMemoryLimit(uint64(i)) //nolint:gosec // overflow checked
@@ -106,17 +100,16 @@ func BenchmarkSetMapping(b *testing.B) {
106100
} {
107101
b.Run(bb.name, func(b *testing.B) {
108102
table := NewMappingSlice()
109-
obj := NewLocation()
110103

111104
if bb.runBefore != nil {
112-
bb.runBefore(b, table, obj)
105+
bb.runBefore(b, table)
113106
}
114107

115108
b.ResetTimer()
116109
b.ReportAllocs()
117110

118111
for b.Loop() {
119-
_ = SetMapping(table, obj, bb.mapping)
112+
_, _ = SetMapping(table, bb.mapping)
120113
}
121114
})
122115
}

0 commit comments

Comments
 (0)