Browse Source

raft: get rid of allocation

Xiang Li 11 years ago
parent
commit
96de9776b7
2 changed files with 9 additions and 5 deletions
  1. 7 2
      raft/log.go
  2. 2 3
      raft/raft.go

+ 7 - 2
raft/log.go

@@ -272,11 +272,16 @@ func (l *raftLog) slice(lo uint64, hi uint64) []pb.Entry {
 		} else if err != nil {
 		} else if err != nil {
 			panic(err) // TODO(bdarnell)
 			panic(err) // TODO(bdarnell)
 		}
 		}
-		ents = append(ents, storedEnts...)
+		ents = storedEnts
 	}
 	}
 	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)
-		ents = append(ents, unstable...)
+		if len(ents) > 0 {
+			ents = append([]pb.Entry{}, ents...)
+			ents = append(ents, unstable...)
+		} else {
+			ents = unstable
+		}
 	}
 	}
 	return ents
 	return ents
 }
 }

+ 2 - 3
raft/raft.go

@@ -407,12 +407,10 @@ func (r *raft) poll(id uint64, v bool) (granted int) {
 }
 }
 
 
 func (r *raft) Step(m pb.Message) error {
 func (r *raft) Step(m pb.Message) error {
-	// TODO(bmizerany): this likely allocs - prevent that.
-	defer func() { r.Commit = r.raftLog.committed }()
-
 	if m.Type == pb.MsgHup {
 	if m.Type == pb.MsgHup {
 		log.Printf("raft: %x is starting a new election at term %d", r.id, r.Term)
 		log.Printf("raft: %x is starting a new election at term %d", r.id, r.Term)
 		r.campaign()
 		r.campaign()
+		r.Commit = r.raftLog.committed
 		return nil
 		return nil
 	}
 	}
 
 
@@ -434,6 +432,7 @@ func (r *raft) Step(m pb.Message) error {
 		return nil
 		return nil
 	}
 	}
 	r.step(r, m)
 	r.step(r, m)
+	r.Commit = r.raftLog.committed
 	return nil
 	return nil
 }
 }