From 24f35a98617f3f26c6679b797e02af0240e122a1 Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Fri, 26 Apr 2019 00:15:21 -0400 Subject: [PATCH] raft: avoid allocation of Raft entry due to logging `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. --- raft/raft.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/raft/raft.go b/raft/raft.go index 61ad12ccb0a..ec7c1172b40 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -1005,11 +1005,12 @@ 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]", - e.String(), r.pendingConfIndex, r.raftLog.applied) + e, r.pendingConfIndex, r.raftLog.applied) m.Entries[i] = pb.Entry{Type: pb.EntryNormal} } else { r.pendingConfIndex = r.raftLog.lastIndex() + uint64(i) + 1