Skip to content

Commit e7472e0

Browse files
committed
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.
1 parent e619e5a commit e7472e0

File tree

1 file changed

+3
-1
lines changed

1 file changed

+3
-1
lines changed

pkg/phlaredb/symdb/dedup_slice.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
279279
if len(missing) > 0 {
280280
s.lock.Lock()
281281
posSlice := int64(len(s.slice))
282+
misSlice := make([]M, 0, len(missing))
282283
for _, pos := range missing {
283284
// check again if element exists
284285
k := s.helper.key(elems[pos])
@@ -288,12 +289,13 @@ func (s *deduplicatingSlice[M, K, H]) ingest(elems []M, rewriter *rewriter) {
288289
}
289290

290291
// add element to slice/map
291-
s.slice = append(s.slice, s.helper.clone(elems[pos]))
292+
misSlice = append(misSlice, s.helper.clone(elems[pos]))
292293
s.lookup[k] = posSlice
293294
rewritingMap[int64(s.helper.setID(uint64(pos), uint64(posSlice), &elems[pos]))] = posSlice
294295
posSlice++
295296
s.size.Add(s.helper.size(elems[pos]))
296297
}
298+
s.slice = append(s.slice, misSlice...)
297299
s.lock.Unlock()
298300
}
299301

0 commit comments

Comments
 (0)