Skip to content

Commit fae1aa7

Browse files
inkelsimonswine
andauthored
Improve *deduplicatingSlice.ingest performance (#4037)
* Add benchmark for `*PartitionWriter.WriteProfileSymbols` I had to perform the initialization within the for loop as otherwise it always panics. * Refactor `*PartitionWriter.WriteProfileSymbols` for DRY Thank you @simonswine for the suggestion! Co-authored-by: Christian Simon <[email protected]> * Create rewriting map with maximum capacity By doing this we avoid having to regrow the map, which improves CPU and memory consumption. * Use temp slice to append only once 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. * Use stdlib slices.Grow to grow the slice --------- Co-authored-by: Christian Simon <[email protected]>
1 parent 6cdda61 commit fae1aa7

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

pkg/phlaredb/symdb/dedup_slice.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package symdb
44
import (
55
"fmt"
66
"hash/maphash"
7+
stdslices "slices"
78
"sort"
89
"sync"
910
"unsafe"
@@ -254,7 +255,7 @@ func (s *deduplicatingSlice[M, K, H]) Size() uint64 {
254255

255256
func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
256257
var (
257-
rewritingMap = make(map[int64]int64)
258+
rewritingMap = make(map[int64]int64, len(elems))
258259
missing = int64SlicePool.Get()
259260
)
260261
missing = missing[:0]
@@ -279,6 +280,7 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
279280
if len(missing) > 0 {
280281
s.lock.Lock()
281282
posSlice := int64(len(s.slice))
283+
s.slice = stdslices.Grow(s.slice, len(missing))
282284
for _, pos := range missing {
283285
// check again if element exists
284286
k := s.helper.key(elems[pos])

pkg/phlaredb/symdb/symdb_test.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,3 +227,23 @@ func TestWritePartition(t *testing.T) {
227227
`
228228
require.Equal(t, expected, resolved.String())
229229
}
230+
231+
func BenchmarkPartitionWriter_WriteProfileSymbols(b *testing.B) {
232+
b.ReportAllocs()
233+
234+
p, err := pprof.OpenFile("testdata/profile.pb.gz")
235+
require.NoError(b, err)
236+
p.Normalize()
237+
cfg := DefaultConfig().WithDirectory(b.TempDir())
238+
239+
db := NewSymDB(cfg)
240+
241+
for i := 0; i < b.N; i++ {
242+
b.StopTimer()
243+
newP := p.CloneVT()
244+
pw := db.PartitionWriter(uint64(i))
245+
b.StartTimer()
246+
247+
pw.WriteProfileSymbols(newP)
248+
}
249+
}

0 commit comments

Comments
 (0)