raft
raft copied to clipboard
Allow reuse of slices exposed through Ready
There are a few allocations in the hot path, for example
https://github.com/etcd-io/raft/blob/228ee703e3ce57db997095ac5bbdd3d9aeb936d2/raft.go#L548
which would be nice to pool. One way we could to this is by introducing something like this:
var msgsAfterAppendPool = sync.Pool{
New: func() interface{} {
sl := make([]pb.Message, 0, 8)
return &sl
},
}
func PutMsgsAfterAppend(sl *[]pb.Message) {
for i := range *sl {
(*sl)[i] = pb.Message{}
}
msgsAfterAppendPool.Put(sl)
}
// send schedules persisting state to a stable storage and AFTER that
// sending the message (as part of next Ready message processing).
func (r *raft) send(m pb.Message) {
@@ -545,6 +559,9 @@ func (r *raft) send(m pb.Message) {
// because the safety of such behavior has not been formally verified,
// we err on the side of safety and omit a `&& !m.Reject` condition
// above.
+ if cap(r.msgsAfterAppend) == 0 {
+ r.msgsAfterAppend = *msgsAfterAppendPool.Get().(*[]pb.Message)
+ }
r.msgsAfterAppend = append(r.msgsAfterAppend, m)
} else {
if m.To == r.id {
This is discussed a little bit in https://github.com/etcd-io/raft/issues/14#issuecomment-1362973589.
@tbg @ahrtr Please see #51
hi @tbg @ahrtr @CaojiamingAlan is this issue resolved or still looking to be picked up?