Преглед изворни кода

Merge pull request #10684 from nvanbenschoten/nvanbenschoten/appendAndCopy

raft: Avoid multiple allocs when merging stable and unstable log
Xiang Li пре 6 година
родитељ
комит
e3f37534e1
1 измењених фајлова са 4 додато и 2 уклоњено
  1. 4 2
      raft/log.go

+ 4 - 2
raft/log.go

@@ -332,8 +332,10 @@ func (l *raftLog) slice(lo, hi, maxSize uint64) ([]pb.Entry, error) {
 	if hi > l.unstable.offset {
 	if hi > l.unstable.offset {
 		unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
 		unstable := l.unstable.slice(max(lo, l.unstable.offset), hi)
 		if len(ents) > 0 {
 		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 {
 		} else {
 			ents = unstable
 			ents = unstable
 		}
 		}