Skip to content

Commit cf2eae3

Browse files
authored
Merge pull request #632 from getsentry/txiao/chore/remove-unused-profile-ids-from-speedscope
chore(flamegraph): Remove unused profile ids from speedscope
2 parents 8fdc1b4 + 818cf30 commit cf2eae3

File tree

7 files changed

+20
-100
lines changed

7 files changed

+20
-100
lines changed

internal/chunk/sample_test.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ func TestCallTrees(t *testing.T) {
4848
SampleCount: 2,
4949
StartNS: 10_000_000,
5050
Frame: frame.Frame{Function: "function0"},
51-
ProfileIDs: make(map[string]struct{}),
5251
Profiles: make(map[examples.ExampleMetadata]struct{}),
5352
Children: []*nodetree.Node{
5453
{
@@ -61,7 +60,6 @@ func TestCallTrees(t *testing.T) {
6160
Occurrence: 1,
6261
SampleCount: 2,
6362
Frame: frame.Frame{Function: "function1"},
64-
ProfileIDs: make(map[string]struct{}),
6563
Profiles: make(map[examples.ExampleMetadata]struct{}),
6664
Children: []*nodetree.Node{
6765
{
@@ -74,7 +72,6 @@ func TestCallTrees(t *testing.T) {
7472
SampleCount: 1,
7573
StartNS: 40_000_000,
7674
Frame: frame.Frame{Function: "function2"},
77-
ProfileIDs: make(map[string]struct{}),
7875
Profiles: make(map[examples.ExampleMetadata]struct{}),
7976
},
8077
},
@@ -115,7 +112,6 @@ func TestCallTrees(t *testing.T) {
115112
SampleCount: 1,
116113
StartNS: 10_000_000,
117114
Frame: frame.Frame{Function: "function0"},
118-
ProfileIDs: make(map[string]struct{}),
119115
Profiles: make(map[examples.ExampleMetadata]struct{}),
120116
Children: []*nodetree.Node{
121117
{
@@ -128,7 +124,6 @@ func TestCallTrees(t *testing.T) {
128124
SampleCount: 1,
129125
StartNS: 10_000_000,
130126
Frame: frame.Frame{Function: "function1"},
131-
ProfileIDs: make(map[string]struct{}),
132127
Profiles: make(map[examples.ExampleMetadata]struct{}),
133128
},
134129
},
@@ -169,7 +164,6 @@ func TestCallTrees(t *testing.T) {
169164
SampleCount: 1,
170165
StartNS: 10_000_000,
171166
Frame: frame.Frame{Function: "function0"},
172-
ProfileIDs: make(map[string]struct{}),
173167
Profiles: make(map[examples.ExampleMetadata]struct{}),
174168
},
175169
{
@@ -182,7 +176,6 @@ func TestCallTrees(t *testing.T) {
182176
SampleCount: 1,
183177
StartNS: 20_000_000,
184178
Frame: frame.Frame{Function: "function1"},
185-
ProfileIDs: make(map[string]struct{}),
186179
Profiles: make(map[examples.ExampleMetadata]struct{}),
187180
},
188181
},

internal/flamegraph/flamegraph.go

Lines changed: 19 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -77,15 +77,12 @@ func addCallTreeToFlamegraph(flamegraphTree *[]*nodetree.Node, callTree []*nodet
7777
type (
7878
flamegraph struct {
7979
samples [][]int
80-
samplesProfileIDs [][]int
8180
samplesProfiles [][]int
8281
sampleCounts []uint64
8382
sampleDurationsNs []uint64
8483
frames []speedscope.Frame
8584
framesIndex map[string]int
8685
frameInfos []speedscope.FrameInfo
87-
profilesIDsIndex map[string]int
88-
profilesIDs []string
8986
profilesIndex map[examples.ExampleMetadata]int
9087
profiles []examples.ExampleMetadata
9188
endValue uint64
@@ -97,11 +94,10 @@ type (
9794
}
9895

9996
flamegraphSample struct {
100-
stack []int
101-
count uint64 // count refers to the individual sample counts
102-
duration uint64
103-
profileIDs map[string]struct{}
104-
profiles map[examples.ExampleMetadata]struct{}
97+
stack []int
98+
count uint64 // count refers to the individual sample counts
99+
duration uint64
100+
profiles map[examples.ExampleMetadata]struct{}
105101
}
106102
)
107103

@@ -129,7 +125,6 @@ func (f *flamegraph) Less(i, j int) bool {
129125

130126
func (f *flamegraph) Swap(i, j int) {
131127
f.samples[i], f.samples[j] = f.samples[j], f.samples[i]
132-
f.samplesProfileIDs[i], f.samplesProfileIDs[j] = f.samplesProfileIDs[j], f.samplesProfileIDs[i]
133128
f.samplesProfiles[i], f.samplesProfiles[j] = f.samplesProfiles[j], f.samplesProfiles[i]
134129
f.sampleCounts[i], f.sampleCounts[j] = f.sampleCounts[j], f.sampleCounts[i]
135130
f.sampleDurationsNs[i], f.sampleDurationsNs[j] = f.sampleDurationsNs[j], f.sampleDurationsNs[i]
@@ -141,35 +136,27 @@ func (f *flamegraph) Push(item any) {
141136
f.samples = append(f.samples, sample.stack)
142137
f.sampleCounts = append(f.sampleCounts, sample.count)
143138
f.sampleDurationsNs = append(f.sampleDurationsNs, sample.duration)
144-
f.samplesProfileIDs = append(f.samplesProfileIDs, f.getProfileIDsIndices(sample.profileIDs))
145139
f.samplesProfiles = append(f.samplesProfiles, f.getProfilesIndices(sample.profiles))
146140
}
147141

148142
func (f *flamegraph) Pop() any {
149143
n := len(f.samples) - 1
150144

151-
profileIDs := make(map[string]struct{})
152-
for _, i := range f.samplesProfileIDs[n] {
153-
profileIDs[f.profilesIDs[i]] = struct{}{}
154-
}
155-
156145
profiles := make(map[examples.ExampleMetadata]struct{})
157146
for _, i := range f.samplesProfiles[n] {
158147
profiles[f.profiles[i]] = struct{}{}
159148
}
160149

161150
sample := flamegraphSample{
162-
stack: f.samples[n],
163-
count: f.sampleCounts[n],
164-
duration: f.sampleDurationsNs[n],
165-
profileIDs: profileIDs,
166-
profiles: profiles,
151+
stack: f.samples[n],
152+
count: f.sampleCounts[n],
153+
duration: f.sampleDurationsNs[n],
154+
profiles: profiles,
167155
}
168156

169157
f.samples = f.samples[0:n]
170158
f.sampleCounts = f.sampleCounts[0:n]
171159
f.sampleDurationsNs = f.sampleDurationsNs[0:n]
172-
f.samplesProfileIDs = f.samplesProfileIDs[0:n]
173160
f.samplesProfiles = f.samplesProfiles[0:n]
174161

175162
return sample
@@ -186,14 +173,13 @@ func toSpeedscope(
186173
defer s.Finish()
187174

188175
fd := &flamegraph{
189-
frames: make([]speedscope.Frame, 0),
190-
frameInfos: make([]speedscope.FrameInfo, 0),
191-
framesIndex: make(map[string]int),
192-
maxSamples: maxSamples,
193-
profilesIDsIndex: make(map[string]int),
194-
profilesIndex: make(map[examples.ExampleMetadata]int),
195-
samples: make([][]int, 0),
196-
sampleCounts: make([]uint64, 0),
176+
frames: make([]speedscope.Frame, 0),
177+
frameInfos: make([]speedscope.FrameInfo, 0),
178+
framesIndex: make(map[string]int),
179+
maxSamples: maxSamples,
180+
profilesIndex: make(map[examples.ExampleMetadata]int),
181+
samples: make([][]int, 0),
182+
sampleCounts: make([]uint64, 0),
197183
}
198184
for _, tree := range trees {
199185
stack := make([]int, 0, profile.MaxStackDepth)
@@ -206,7 +192,6 @@ func toSpeedscope(
206192
aggProfiles := make([]interface{}, 1)
207193
aggProfiles[0] = speedscope.SampledProfile{
208194
Samples: fd.samples,
209-
SamplesProfiles: fd.samplesProfileIDs,
210195
SamplesExamples: fd.samplesProfiles,
211196
Weights: fd.sampleCounts,
212197
SampleCounts: fd.sampleCounts,
@@ -226,7 +211,6 @@ func toSpeedscope(
226211
Shared: speedscope.SharedData{
227212
Frames: fd.frames,
228213
FrameInfos: fd.frameInfos,
229-
ProfileIDs: fd.profilesIDs,
230214
Profiles: fd.profiles,
231215
},
232216
Profiles: aggProfiles,
@@ -272,7 +256,6 @@ func (f *flamegraph) visitCalltree(node *nodetree.Node, currentStack *[]int) {
272256
currentStack,
273257
uint64(node.SampleCount),
274258
node.DurationNS,
275-
node.ProfileIDs,
276259
node.Profiles,
277260
)
278261
} else {
@@ -295,7 +278,6 @@ func (f *flamegraph) visitCalltree(node *nodetree.Node, currentStack *[]int) {
295278
currentStack,
296279
uint64(diffCount),
297280
diffDuration,
298-
node.ProfileIDs,
299281
node.Profiles,
300282
)
301283
}
@@ -308,46 +290,24 @@ func (f *flamegraph) addSample(
308290
stack *[]int,
309291
count uint64,
310292
duration uint64,
311-
profileIDs map[string]struct{},
312293
profiles map[examples.ExampleMetadata]struct{},
313294
) {
314295
f.totalSamples++
315296
cp := make([]int, len(*stack))
316297
copy(cp, *stack)
317298

318299
heap.Push(f, flamegraphSample{
319-
stack: cp,
320-
count: count,
321-
duration: duration,
322-
profileIDs: profileIDs,
323-
profiles: profiles,
300+
stack: cp,
301+
count: count,
302+
duration: duration,
303+
profiles: profiles,
324304
})
325305
for f.overCapacity() {
326306
heap.Pop(f)
327307
}
328308
f.endValue += count
329309
}
330310

331-
func (f *flamegraph) getProfileIDsIndices(profileIDsMap map[string]struct{}) []int {
332-
profileIDs := make([]string, 0, len(profileIDsMap))
333-
for id := range profileIDsMap {
334-
profileIDs = append(profileIDs, id)
335-
}
336-
sort.Strings(profileIDs)
337-
338-
indices := make([]int, 0, len(profileIDsMap))
339-
for _, id := range profileIDs {
340-
if idx, ok := f.profilesIDsIndex[id]; ok {
341-
indices = append(indices, idx)
342-
} else {
343-
indices = append(indices, len(f.profilesIDs))
344-
f.profilesIDsIndex[id] = len(f.profilesIDs)
345-
f.profilesIDs = append(f.profilesIDs, id)
346-
}
347-
}
348-
return indices
349-
}
350-
351311
func (f *flamegraph) getProfilesIndices(profilesMap map[examples.ExampleMetadata]struct{}) []int {
352312
profiles := make([]examples.ExampleMetadata, 0, len(profilesMap))
353313
for profile := range profilesMap {

internal/flamegraph/flamegraph_test.go

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -148,12 +148,6 @@ func TestFlamegraphAggregation(t *testing.T) {
148148
{2, 0},
149149
{0, 1},
150150
},
151-
SamplesProfiles: [][]int{
152-
{},
153-
{},
154-
{},
155-
{},
156-
},
157151
SamplesExamples: [][]int{
158152
{0},
159153
{1},
@@ -293,11 +287,6 @@ func TestFlamegraphAggregation(t *testing.T) {
293287
{0, 1, 2},
294288
{0, 1},
295289
},
296-
SamplesProfiles: [][]int{
297-
{},
298-
{},
299-
{},
300-
},
301290
SamplesExamples: [][]int{
302291
{0},
303292
{0},
@@ -442,10 +431,6 @@ func TestFlamegraphAggregation(t *testing.T) {
442431
{1},
443432
{0, 1, 2},
444433
},
445-
SamplesProfiles: [][]int{
446-
{},
447-
{},
448-
},
449434
SamplesExamples: [][]int{
450435
{0},
451436
{0},
@@ -589,7 +574,6 @@ func TestAnnotatingWithExamples(t *testing.T) {
589574
SampleCount: 2,
590575
Occurrence: 2,
591576
Frame: frame.Frame{Function: "function1"},
592-
ProfileIDs: make(map[string]struct{}),
593577
Profiles: make(map[examples.ExampleMetadata]struct{}),
594578
Children: []*nodetree.Node{
595579
{
@@ -602,7 +586,6 @@ func TestAnnotatingWithExamples(t *testing.T) {
602586
Occurrence: 1,
603587
StartNS: 40_000_000,
604588
Frame: frame.Frame{Function: "function2"},
605-
ProfileIDs: make(map[string]struct{}),
606589
Profiles: make(map[examples.ExampleMetadata]struct{}),
607590
},
608591
},
@@ -626,7 +609,6 @@ func TestAnnotatingWithExamples(t *testing.T) {
626609
{0, 1},
627610
{0},
628611
},
629-
SamplesProfiles: [][]int{{}, {}},
630612
SamplesExamples: [][]int{{0, 1}, {0, 1}},
631613
Type: "sampled",
632614
Unit: "count",

internal/nodetree/nodetree.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ type (
4444
Occurrence uint32 `json:"-"`
4545
SampleCount int `json:"-"`
4646
StartNS uint64 `json:"-"`
47-
ProfileIDs map[string]struct{} `json:"profile_ids,omitempty"`
4847
Profiles map[examples.ExampleMetadata]struct{} `json:"profiles,omitempty"`
4948
}
5049
)
@@ -66,7 +65,6 @@ func NodeFromFrame(f frame.Frame, start, end, fingerprint uint64) *Node {
6665
Occurrence: 1,
6766
SampleCount: 1,
6867
StartNS: start,
69-
ProfileIDs: map[string]struct{}{},
7068
Profiles: map[examples.ExampleMetadata]struct{}{},
7169
}
7270
if end > 0 {
@@ -88,7 +86,6 @@ func (n *Node) ShallowCopyWithoutChildren() *Node {
8886
Occurrence: n.Occurrence,
8987
SampleCount: n.SampleCount,
9088
StartNS: n.StartNS,
91-
ProfileIDs: n.ProfileIDs,
9289
Profiles: n.Profiles,
9390
DurationNS: n.DurationNS,
9491
}

internal/profile/android_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ func TestCallTrees(t *testing.T) {
708708
}
709709

710710
options := cmp.Options{
711-
cmpopts.IgnoreFields(nodetree.Node{}, "Fingerprint", "ProfileIDs", "Profiles"),
711+
cmpopts.IgnoreFields(nodetree.Node{}, "Fingerprint", "Profiles"),
712712
cmpopts.IgnoreFields(frame.Frame{}, "File"),
713713
}
714714

0 commit comments

Comments
 (0)