Skip to content

Commit

Permalink
perf: optimize MemoryStorage.Compact method by improving memory alloc…
Browse files Browse the repository at this point in the history
…ation
  • Loading branch information
1911860538 committed Feb 15, 2025
1 parent ef8021f commit ab3dcca
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,10 +263,9 @@ func (ms *MemoryStorage) Compact(compactIndex uint64) error {
// NB: allocate a new slice instead of reusing the old ms.ents. Entries in
// ms.ents are immutable, and can be referenced from outside MemoryStorage
// through slices returned by ms.Entries().
ents := make([]pb.Entry, 1, uint64(len(ms.ents))-i)
ents[0].Index = ms.ents[i].Index
ents[0].Term = ms.ents[i].Term
ents = append(ents, ms.ents[i+1:]...)
remainingCount := len(ms.ents) - int(i)
ents := make([]pb.Entry, remainingCount)
copy(ents, ms.ents[i:])
ms.ents = ents
return nil
}
Expand Down

0 comments on commit ab3dcca

Please sign in to comment.