Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve *deduplicatingSlice.ingest performance #4037

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

inkel
Copy link
Contributor

@inkel inkel commented Mar 21, 2025

As in #4033 in the past hackathon I checked optimizations on Pyroscope ingester, and found a couple low hanging fruits that could speed up *deduplicatingSlice.ingest method.

The first thing I did was adding a benchmark for *PartitionWriter.WriteProfileSymbols which was showing as one of the methods consuming most CPU and memory. Armed with this, I eventually found out that *deduplicatingSlice.ingest stand out as one of the methods that could be improved in a relatively easy way.

The first was creating the rewritingMap with the most capacity it could hold. This already showed some gains in ~7% CPU and ~8% bytes/op reduction.

 benchstat bench.pw-writeprofilesymbols-refactorbench.log bench.pw-writeprofilesymbols-rewritemapinitcap.log
goos: darwin
goarch: arm64
pkg: github.com/grafana/pyroscope/pkg/phlaredb/symdb
cpu: Apple M1 Pro
                                       │ bench.pw-writeprofilesymbols-refactorbench.log │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │
                                       │                     sec/op                     │           sec/op             vs base               │
PartitionWriter_WriteProfileSymbols-10                                      548.2µ ± 1%                   509.8µ ± 1%  -7.01% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols-refactorbench.log │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │
                                       │                      B/op                      │            B/op              vs base               │
PartitionWriter_WriteProfileSymbols-10                                     706.2Ki ± 0%                  650.5Ki ± 0%  -7.88% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols-refactorbench.log │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │
                                       │                   allocs/op                    │          allocs/op           vs base               │
PartitionWriter_WriteProfileSymbols-10                                      2.241k ± 0%                   2.202k ± 0%  -1.76% (p=0.000 n=20)

Next thing I did was noticing that when iterating over missing elements we were calling append on s.slice every time there was an element to add. By creating a temporary slice with an initial capacity of len(missing) and size 0 and appending to that one, the runtime doesn't have to regrow the slice (if needed) on every iteration, and it only checks capacity and regrows s.slice if needed outside the loop. This results in additional CPU and memory savings:

$ benchstat bench.pw-writeprofilesymbols-rewritemapinitcap.log bench.pw-writeprofilesymbols-misslice.log
goos: darwin
goarch: arm64
pkg: github.com/grafana/pyroscope/pkg/phlaredb/symdb
cpu: Apple M1 Pro
                                       │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │                       sec/op                       │       sec/op        vs base               │
PartitionWriter_WriteProfileSymbols-10                                         569.9µ ± 15%          545.6µ ± 1%  -4.28% (p=0.013 n=20)

                                       │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │                        B/op                        │        B/op         vs base               │
PartitionWriter_WriteProfileSymbols-10                                         650.6Ki ± 0%         600.9Ki ± 0%  -7.64% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols-rewritemapinitcap.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │                     allocs/op                      │     allocs/op       vs base               │
PartitionWriter_WriteProfileSymbols-10                                          2.202k ± 0%          2.176k ± 0%  -1.18% (p=0.000 n=20)

Overall, with all these changes applied, the benchmark comparison is close to 20% CPU and 15% memory:

$ benchstat bench.pw-writeprofilesymbols.log bench.pw-writeprofilesymbols-misslice.log
goos: darwin
goarch: arm64
pkg: github.com/grafana/pyroscope/pkg/phlaredb/symdb
cpu: Apple M1 Pro
                                       │ bench.pw-writeprofilesymbols.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │              sec/op              │      sec/op        vs base                │
PartitionWriter_WriteProfileSymbols-10                        629.1µ ± 9%         504.0µ ± 1%  -19.89% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │               B/op               │       B/op         vs base                │
PartitionWriter_WriteProfileSymbols-10                       706.2Ki ± 0%        600.8Ki ± 0%  -14.92% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols.log │ bench.pw-writeprofilesymbols-misslice.log │
                                       │            allocs/op             │     allocs/op       vs base               │
PartitionWriter_WriteProfileSymbols-10                        2.241k ± 0%          2.176k ± 0%  -2.90% (p=0.000 n=20)

I had to perform the initialization within the for loop as otherwise
it always panics.
@inkel inkel force-pushed the inkel/deduplicating-slice-ingest-improvements branch from f0c1d89 to e7472e0 Compare March 21, 2025 16:10
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.

Thanks for sharing your work!

Not too sure if you are aware, but using -memprofile, you can also generate a profile of your micro benchmark to see how it performs line by line: (left is before this PR right is after)
image.

I think the best benchmark test would, be to run pyroscope for 30 min with ingestion real profiles and compare its memory profiles before and after.

@@ -279,6 +279,7 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
if len(missing) > 0 {
s.lock.Lock()
posSlice := int64(len(s.slice))
misSlice := make([]M, 0, len(missing))
Copy link
Contributor

@simonswine simonswine Mar 21, 2025

Choose a reason for hiding this comment

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

Rather than using the extra slice you can also just grow the underlying slice, you might overgrow it but you can get rid of misSlice entirely.

Suggested change
misSlice := make([]M, 0, len(missing))
s.slice = slices.Grow(s.slice, len(missing))

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, that's probably even better! I'll refactor to use this and see what the benchmark says.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Did you mean slices.GrowLen instead? Anyway, I've tested calling this function and then comparing both results and got this:

$ benchstat bench.pw-writeprofilesymbols-misslice.log bench.pw-writeprofilesymbols-slicesgrowlen.log
goos: darwin
goarch: arm64
pkg: github.com/grafana/pyroscope/pkg/phlaredb/symdb
cpu: Apple M1 Pro
                                       │ bench.pw-writeprofilesymbols-misslice.log │ bench.pw-writeprofilesymbols-slicesgrowlen.log │
                                       │                  sec/op                   │         sec/op           vs base               │
PartitionWriter_WriteProfileSymbols-10                                 504.0µ ± 1%               515.7µ ± 1%  +2.34% (p=0.001 n=20)

                                       │ bench.pw-writeprofilesymbols-misslice.log │ bench.pw-writeprofilesymbols-slicesgrowlen.log │
                                       │                   B/op                    │          B/op           vs base                │
PartitionWriter_WriteProfileSymbols-10                                600.8Ki ± 0%             714.7Ki ± 0%  +18.95% (p=0.000 n=20)

                                       │ bench.pw-writeprofilesymbols-misslice.log │ bench.pw-writeprofilesymbols-slicesgrowlen.log │
                                       │                 allocs/op                 │        allocs/op         vs base               │
PartitionWriter_WriteProfileSymbols-10                                 2.176k ± 0%               2.206k ± 0%  +1.38% (p=0.000 n=20)

Surprisingly, growing s.slice is actually slower and consumes more memory 🤔

Copy link
Contributor

@simonswine simonswine Mar 25, 2025

Choose a reason for hiding this comment

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

No GrowLen is not the same function, as it will take the total length of slices, rather than how much more space should be available, you either need to adapt the size of that or import origslices "slices" and use slices.Grow(xx)

@@ -254,7 +254,7 @@ func (s *deduplicatingSlice[M, K, H]) Size() uint64 {

func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
var (
rewritingMap = make(map[int64]int64)
Copy link
Contributor

Choose a reason for hiding this comment

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

Good find, this will give us a right sized map every time.

@inkel
Copy link
Contributor Author

inkel commented Mar 21, 2025

Not too sure if you are aware, but using -memprofile, you can also generate a profile of your micro benchmark to see how it performs line by line: (left is before this PR right is after)

Ah, yes! I used it locally and that's how I arrived to the ingest method.

I think the best benchmark test would, be to run pyroscope for 30 min with ingestion real profiles and compare its memory profiles before and after.

Ooh this would definitely be The Dream™. I'm going to apply your suggestion for refactoring the benchmark and then see how it affects the results I have.

Thanks for the review so far! 🫶

inkel and others added 3 commits March 21, 2025 13:46
Thank you @simonswine for the suggestion!

Co-authored-by: Christian Simon <simon@swine.de>
By doing this we avoid having to regrow the map, which improves CPU
and memory consumption.
Instead of checking `s.slice` on each call to append and regrow if
necessary, create a temporary slice with the maximum possible
capacity, and append that slice to `s.slice` outside the for loop,
effectively checking and regrowing `s.slice` only once.

This further improves CPU and memory consumption.
@inkel
Copy link
Contributor Author

inkel commented Mar 21, 2025

@simonswine applying your changes to the benchmark it runs ~13% faster with the original code, so I'm going to reorder the commits and run the benchmarks again to get more realistic results. I'll update the PR accordingly. Thanks!

@inkel inkel force-pushed the inkel/deduplicating-slice-ingest-improvements branch from 95b0bec to 44cf7d9 Compare March 21, 2025 17:26
@inkel
Copy link
Contributor Author

inkel commented Mar 21, 2025

I've found another thing that could be pre-initialized with a size that greatly affects the performance, and is the *deduplicatingSlice.lookup map (see here), however, that one is trickier to address because each usage of *deduplicatingSlice benefits better from different initial sizes of the lookup map, so I'm leaving out. Nevertheless, I thought it was worth mentioning.

@inkel inkel marked this pull request as ready for review March 21, 2025 18:08
@inkel inkel requested a review from a team as a code owner March 21, 2025 18:08
@inkel
Copy link
Contributor Author

inkel commented Mar 21, 2025

🤔 there's a failing test on CI, IIUC due to a context cancelled. I've run make go/test locally and it's working, maybe it needs to re-run?

@simonswine
Copy link
Contributor

maybe it needs to re-run?

Yes some tests are flaky right now, sorry about that

@inkel
Copy link
Contributor Author

inkel commented Mar 21, 2025

No worries!

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