mirror of
https://github.com/etcd-io/etcd.git
synced 2024-09-27 06:25:44 +00:00
raft: Avoid multiple allocs when merging stable and unstable log
Appending to an empty slice twice could (and often did) result in multiple allocations. This was wasteful. We can avoid this by performing a single allocation with the correct size and copying into it.
This commit is contained in:
parent
cca0d5c1be
commit
b5593de806
@ -332,8 +332,10 @@ func (l *raftLog) slice(lo, hi, maxSize uint64) ([]pb.Entry, error) {
|
||||
if hi > l.unstable.offset {
|
||||
unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
|
||||
if len(ents) > 0 {
|
||||
ents = append([]pb.Entry{}, ents...)
|
||||
ents = append(ents, unstable...)
|
||||
combined := make([]pb.Entry, len(ents)+len(unstable))
|
||||
n := copy(combined, ents)
|
||||
copy(combined[n:], unstable)
|
||||
ents = combined
|
||||
} else {
|
||||
ents = unstable
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user