raft icon indicating copy to clipboard operation
raft copied to clipboard

Allow reuse of slices exposed through Ready

Open tbg opened this issue 2 years ago • 2 comments

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 avatar Dec 22 '22 16:12 tbg

@tbg @ahrtr Please see #51

CaojiamingAlan avatar May 12 '23 02:05 CaojiamingAlan

hi @tbg @ahrtr @CaojiamingAlan is this issue resolved or still looking to be picked up?

Aditya-Sood avatar Aug 11 '23 06:08 Aditya-Sood