Skip to content

Conversation

aleks-p
Copy link
Contributor

@aleks-p aleks-p commented Sep 11, 2025

When we render large diff flamegraphs, we create quite a bit of allocations due to prepending to a slice instead of appending. We also use an older and less efficient way of truncating the diff tree, I've replaced that with the one used for truncating regular (non-diff) trees

Screenshot 2025-09-11 at 18 07 44 Screenshot 2025-09-11 at 18 07 55
goos: darwin
goarch: arm64
pkg: github.com/grafana/pyroscope/pkg/model
cpu: Apple M2 Pro
                      │ reference-diff.txt │         optimized-diff.txt          │
                      │       sec/op       │   sec/op     vs base                │
_NewFlamegraphDiff-10         3956.7m ± 3%   252.0m ± 7%  -93.63% (p=0.000 n=10)

                      │ reference-diff.txt │          optimized-diff.txt          │
                      │        B/op        │     B/op      vs base                │
_NewFlamegraphDiff-10       18313.8Mi ± 0%   749.1Mi ± 0%  -95.91% (p=0.000 n=10)

                      │ reference-diff.txt │         optimized-diff.txt          │
                      │     allocs/op      │  allocs/op   vs base                │
_NewFlamegraphDiff-10          1.489M ± 0%   1.053M ± 0%  -29.33% (p=0.000 n=10)

@aleks-p aleks-p requested a review from bryanhuhta as a code owner September 11, 2025 21:24
Comment on lines +272 to +290

h := make([]int64, 0, maxNodes)
combineIterateWithTotal(leftTree, rightTree, func(left uint64, right uint64) bool {
return c.Push(max(left, right))
maxVal := int64(max(left, right))
if len(h) >= maxNodes {
if maxVal > h[0] {
h = minheap.Pop(h)
h = minheap.Push(h, maxVal)
}
} else {
h = minheap.Push(h, maxVal)
}
return true
})
return c.MinValue()

if len(h) < maxNodes {
return 0
}
return uint64(h[0])
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is based on model.Tree.minValue(), adapted for 2 trees

Copy link
Contributor

@simonswine simonswine left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Thanks for spotting this. Wonder how "good" your benchmarks were, but might make sense to add them.

@aleks-p
Copy link
Contributor Author

aleks-p commented Sep 12, 2025

Wonder how "good" your benchmarks were, but might make sense to add them.

I can't add the data I used for the benchmark, but I can likely capture the improvement with synthetic data as well. I will add the benchmark code before merging.

Edit: done

@aleks-p aleks-p merged commit 3b1c0ba into main Sep 12, 2025
20 checks passed
@aleks-p aleks-p deleted the feat/improve-diff branch September 12, 2025 12:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants