Skip to content

Commit

Permalink
raft, tests: reduced unnecessary allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
lni committed Jun 26, 2019
1 parent ad1e041 commit 90f0815
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ func BenchmarkNALookup(b *testing.B) {
func benchmarkStateMachineStep(b *testing.B, sz int, noopSession bool) {
b.ReportAllocs()
b.StopTimer()
ds := &tests.NoOP{}
ds := &tests.NoOP{NoAlloc: true}
done := make(chan struct{})
nds := rsm.NewNativeStateMachine(1, 1, rsm.NewRegularStateMachine(ds), done)
smo := rsm.NewStateMachine(nds, nil, false, &testDummyNodeProxy{})
Expand All @@ -520,7 +520,7 @@ func benchmarkStateMachineStep(b *testing.B, sz int, noopSession bool) {
Cmd: make([]byte, sz),
}
entries := make([]pb.Entry, 0)
batch := make([]rsm.Task, 0)
batch := make([]rsm.Task, 0, 100000)
smEntries := make([]sm.Entry, 0)
task := rsm.Task{
SnapshotAvailable: false,
Expand Down
2 changes: 2 additions & 0 deletions internal/raft/raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,8 @@ func (r *raft) sortMatchValues() {
r.matched[0] = r.matched[1]
r.matched[1] = v
}
} else if len(r.matched) == 1 {
return
} else {
sort.Slice(r.matched, func(i, j int) bool {
return r.matched[i] < r.matched[j]
Expand Down
4 changes: 4 additions & 0 deletions internal/tests/noop.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
// NoOP is a IStateMachine struct used for testing purpose.
type NoOP struct {
MillisecondToSleep uint64
NoAlloc bool
}

// Lookup locally looks up the data.
Expand All @@ -43,6 +44,9 @@ func (n *NoOP) Update(data []byte) (sm.Result, error) {
if n.MillisecondToSleep > 0 {
time.Sleep(time.Duration(n.MillisecondToSleep) * time.Millisecond)
}
if n.NoAlloc {
return sm.Result{Value: uint64(len(data))}, nil
}
v := make([]byte, len(data))
copy(v, data)
return sm.Result{Value: uint64(len(data)), Data: v}, nil
Expand Down

0 comments on commit 90f0815

Please sign in to comment.