raft: Avoid allocation when boxing slice in maybeCommit
By boxing a heap-allocated slice header instead of the slice header on the stack, we can avoid an allocation when passing through the sort.Interface interface. This was responsible for 26.61% of total allocations in an experiment with https://github.com/nvanbenschoten/raft-toy.release-3.4
parent
cca0d5c1be
commit
208b8a349c
|
@ -607,14 +607,14 @@ func (r *raft) maybeCommit() bool {
|
||||||
if cap(r.matchBuf) < len(r.prs) {
|
if cap(r.matchBuf) < len(r.prs) {
|
||||||
r.matchBuf = make(uint64Slice, len(r.prs))
|
r.matchBuf = make(uint64Slice, len(r.prs))
|
||||||
}
|
}
|
||||||
mis := r.matchBuf[:len(r.prs)]
|
r.matchBuf = r.matchBuf[:len(r.prs)]
|
||||||
idx := 0
|
idx := 0
|
||||||
for _, p := range r.prs {
|
for _, p := range r.prs {
|
||||||
mis[idx] = p.Match
|
r.matchBuf[idx] = p.Match
|
||||||
idx++
|
idx++
|
||||||
}
|
}
|
||||||
sort.Sort(mis)
|
sort.Sort(&r.matchBuf)
|
||||||
mci := mis[len(mis)-r.quorum()]
|
mci := r.matchBuf[len(r.matchBuf)-r.quorum()]
|
||||||
return r.raftLog.maybeCommit(mci, r.Term)
|
return r.raftLog.maybeCommit(mci, r.Term)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue