Skip to content

Commit

Permalink
raft: avoid allocation of Raft entry due to logging
Browse files Browse the repository at this point in the history
`raftpb.Entry.String` takes a pointer receiver, so calling it
on a loop variable was causing the variable to escape. Removing
the `.String()` call was enough to avoid the allocation, but
this also avoids a memory copy and prevents similar bugs.

This was responsible for 11.63% of total allocations in an
experiment with https://github.com/nvanbenschoten/raft-toy.
  • Loading branch information
nvanbenschoten committed Apr 26, 2019
1 parent cca0d5c commit 7f68744
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1005,7 +1005,8 @@ func stepLeader(r *raft, m pb.Message) error {
return ErrProposalDropped
}

for i, e := range m.Entries {
for i := range m.Entries {
e := &m.Entries[i]
if e.Type == pb.EntryConfChange {
if r.pendingConfIndex > r.raftLog.applied {
r.logger.Infof("propose conf %s ignored since pending unapplied configuration [index %d, applied %d]",
Expand Down

0 comments on commit 7f68744

Please sign in to comment.