diff --git a/raft.go b/raft.go index ee7859456..1bb83183c 100644 --- a/raft.go +++ b/raft.go @@ -455,7 +455,7 @@ func newRaft(c *Config) *raft { func (r *raft) hasLeader() bool { return r.lead != None } -func (r *raft) softState() *SoftState { return &SoftState{Lead: r.lead, RaftState: r.state} } +func (r *raft) softState() SoftState { return SoftState{Lead: r.lead, RaftState: r.state} } func (r *raft) hardState() pb.HardState { return pb.HardState{ diff --git a/rawnode.go b/rawnode.go index b46182868..0f3807c66 100644 --- a/rawnode.go +++ b/rawnode.go @@ -54,7 +54,8 @@ func NewRawNode(config *Config) (*RawNode, error) { raft: r, } rn.asyncStorageWrites = config.AsyncStorageWrites - rn.prevSoftSt = r.softState() + ss := r.softState() + rn.prevSoftSt = &ss rn.prevHardSt = r.hardState() return rn, nil } @@ -144,7 +145,9 @@ func (rn *RawNode) readyWithoutAccept() Ready { Messages: r.msgs, } if softSt := r.softState(); !softSt.equal(rn.prevSoftSt) { - rd.SoftState = softSt + // Allocate only when SoftState changes. + escapingSoftSt := softSt + rd.SoftState = &escapingSoftSt } if hardSt := r.hardState(); !isHardStateEqual(hardSt, rn.prevHardSt) { rd.HardState = hardSt @@ -445,7 +448,7 @@ func (rn *RawNode) applyUnstableEntries() bool { func (rn *RawNode) HasReady() bool { // TODO(nvanbenschoten): order these cases in terms of cost and frequency. r := rn.raft - if !r.softState().equal(rn.prevSoftSt) { + if softSt := r.softState(); !softSt.equal(rn.prevSoftSt) { return true } if hardSt := r.hardState(); !IsEmptyHardState(hardSt) && !isHardStateEqual(hardSt, rn.prevHardSt) { diff --git a/rawnode_test.go b/rawnode_test.go index 3e146f8b4..553d74fd6 100644 --- a/rawnode_test.go +++ b/rawnode_test.go @@ -1239,6 +1239,7 @@ func benchmarkRawNodeImpl(b *testing.B, peers ...uint64) { if applied < uint64(b.N) { b.Fatalf("did not apply everything: %d < %d", applied, b.N) } + b.ReportAllocs() b.ReportMetric(float64(s.callStats.firstIndex)/float64(b.N), "firstIndex/op") b.ReportMetric(float64(s.callStats.lastIndex)/float64(b.N), "lastIndex/op") b.ReportMetric(float64(s.callStats.term)/float64(b.N), "term/op") diff --git a/status.go b/status.go index 4164b29ae..b1ffd90f1 100644 --- a/status.go +++ b/status.go @@ -59,7 +59,7 @@ func getBasicStatus(r *raft) BasicStatus { LeadTransferee: r.leadTransferee, } s.HardState = r.hardState() - s.SoftState = *r.softState() + s.SoftState = r.softState() s.Applied = r.raftLog.applied return s }