Skip to content

Commit

Permalink
raft: dont allocate slice and sort on every commit
Browse files Browse the repository at this point in the history
  • Loading branch information
nvanbenschoten committed Oct 12, 2017
1 parent 764a0f7 commit b5b9d61
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ type raft struct {
maxInflight int
maxMsgSize uint64
prs map[uint64]*Progress
matchBuf uint64Slice

state StateType

Expand Down Expand Up @@ -503,13 +504,23 @@ func (r *raft) bcastHeartbeatWithCtx(ctx []byte) {
// the commit index changed (in which case the caller should call
// r.bcastAppend).
func (r *raft) maybeCommit() bool {
// TODO(bmizerany): optimize.. Currently naive
mis := make(uint64Slice, 0, len(r.prs))
if cap(r.matchBuf) < len(r.prs) {
r.matchBuf = make(uint64Slice, len(r.prs))
}
mis := r.matchBuf[:len(r.prs)]
idx := 0
sorted := true
for id := range r.prs {
mis = append(mis, r.prs[id].Match)
mis[idx] = r.prs[id].Match
if idx > 0 && mis[idx] < mis[idx-1] {
sorted = false
}
idx++
}
if !sorted {
sort.Sort(mis)
}
sort.Sort(sort.Reverse(mis))
mci := mis[r.quorum()-1]
mci := mis[len(mis)-r.quorum()]
return r.raftLog.maybeCommit(mci, r.Term)
}

Expand Down

0 comments on commit b5b9d61

Please sign in to comment.