raft: get rid of allocation

This commit is contained in:
Xiang Li 2014-12-10 13:41:04 -08:00
parent e4c0f5c1a8
commit 96de9776b7
2 changed files with 9 additions and 5 deletions

View File

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

View File

@ -407,12 +407,10 @@ func (r *raft) poll(id uint64, v bool) (granted int) {
}
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 {
log.Printf("raft: %x is starting a new election at term %d", r.id, r.Term)
r.campaign()
r.Commit = r.raftLog.committed
return nil
}
@ -434,6 +432,7 @@ func (r *raft) Step(m pb.Message) error {
return nil
}
r.step(r, m)
r.Commit = r.raftLog.committed
return nil
}