Skip to content

Improve *deduplicatingSlice.ingest performance #4037

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions pkg/phlaredb/symdb/dedup_slice.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.

rewritingMap = make(map[int64]int64, len(elems))
missing = int64SlicePool.Get()
)
missing = missing[:0]
Expand All @@ -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)

for _, pos := range missing {
// check again if element exists
k := s.helper.key(elems[pos])
Expand All @@ -288,12 +289,13 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
}

// add element to slice/map
s.slice = append(s.slice, s.helper.clone(elems[pos]))
misSlice = append(misSlice, s.helper.clone(elems[pos]))
s.lookup[k] = posSlice
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), &elems[pos]))] = posSlice
posSlice++
s.size.Add(s.helper.size(elems[pos]))
}
s.slice = append(s.slice, misSlice...)
s.lock.Unlock()
}

Expand Down
20 changes: 20 additions & 0 deletions pkg/phlaredb/symdb/symdb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,3 +227,23 @@ func TestWritePartition(t *testing.T) {
`
require.Equal(t, expected, resolved.String())
}

func BenchmarkPartitionWriter_WriteProfileSymbols(b *testing.B) {
b.ReportAllocs()

p, err := pprof.OpenFile("testdata/profile.pb.gz")
require.NoError(b, err)
p.Normalize()
cfg := DefaultConfig().WithDirectory(b.TempDir())

db := NewSymDB(cfg)

for i := 0; i < b.N; i++ {
b.StopTimer()
newP := p.CloneVT()
pw := db.PartitionWriter(uint64(i))
b.StartTimer()

pw.WriteProfileSymbols(newP)
}
}
Loading