Skip to content

Commit ab3dcca

Browse files
author
1911860538
committed
perf: optimize MemoryStorage.Compact method by improving memory allocation
1 parent ef8021f commit ab3dcca

File tree

1 file changed

+3
-4
lines changed

1 file changed

+3
-4
lines changed

storage.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -263,10 +263,9 @@ func (ms *MemoryStorage) Compact(compactIndex uint64) error {
263263
// NB: allocate a new slice instead of reusing the old ms.ents. Entries in
264264
// ms.ents are immutable, and can be referenced from outside MemoryStorage
265265
// through slices returned by ms.Entries().
266-
ents := make([]pb.Entry, 1, uint64(len(ms.ents))-i)
267-
ents[0].Index = ms.ents[i].Index
268-
ents[0].Term = ms.ents[i].Term
269-
ents = append(ents, ms.ents[i+1:]...)
266+
remainingCount := len(ms.ents) - int(i)
267+
ents := make([]pb.Entry, remainingCount)
268+
copy(ents, ms.ents[i:])
270269
ms.ents = ents
271270
return nil
272271
}

0 commit comments

Comments
 (0)