Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow reuse of slices exposed through Ready #15

Open
tbg opened this issue Dec 22, 2022 · 2 comments
Open

Allow reuse of slices exposed through Ready #15

tbg opened this issue Dec 22, 2022 · 2 comments
Labels
enhancement New feature or request

Comments

@tbg
Copy link
Collaborator

tbg commented Dec 22, 2022

There are a few allocations in the hot path, for example

raft/raft.go

Line 548 in 228ee70

r.msgsAfterAppend = append(r.msgsAfterAppend, m)

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 #14 (comment).

@CaojiamingAlan
Copy link
Contributor

@tbg @ahrtr Please see #51

@Aditya-Sood
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants