lite: < len(v) in for loop check, as per @melekes' recommendation

Also lazily load the commits to only be run once when
the benchmarks are activated, lest it slows down all the tests
This commit is contained in:
Emmanuel Odeke 2018-01-01 20:11:55 -08:00
parent 14eaba9ec3
commit 206da7a1b8
3 changed files with 22 additions and 15 deletions

View File

@ -77,10 +77,7 @@ func (v ValKeys) signHeader(header *types.Header, first, last int) *types.Commit
vset := v.ToValidators(1, 0)
// fill in the votes we want
for i := first; i < last; i++ {
if i >= len(v) {
break
}
for i := first; i < last && i < len(v); i++ {
vote := makeVote(header, vset, v[i])
votes[vote.ValidatorIndex] = vote
}

View File

@ -60,8 +60,8 @@ func (m *memStoreProvider) StoreCommit(fc FullCommit) error {
// GetByHeight returns the FullCommit for height h or an error if the commit is not found.
func (m *memStoreProvider) GetByHeight(h int64) (FullCommit, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()
defer m.mtx.Unlock()
if !m.sorted {
sort.Sort(m.byHeight)
m.sorted = true
@ -77,8 +77,8 @@ func (m *memStoreProvider) GetByHeight(h int64) (FullCommit, error) {
// GetByHeight returns the FullCommit for height h or an error if the commit is not found.
func (m *memStoreProvider) GetByHeightBinarySearch(h int64) (FullCommit, error) {
m.mtx.RLock()
defer m.mtx.RUnlock()
m.mtx.Lock()
defer m.mtx.Unlock()
if !m.sorted {
sort.Sort(m.byHeight)
m.sorted = true

View File

@ -3,6 +3,7 @@ package lite_test
import (
"fmt"
"math/rand"
"sync"
"testing"
"github.com/tendermint/tendermint/lite"
@ -124,13 +125,20 @@ const (
binarySearch = false
)
var (
fcs5, h5 = genFullCommits(nil, nil, 5)
fcs50, h50 = genFullCommits(fcs5, h5, 50)
fcs100, h100 = genFullCommits(fcs50, h50, 100)
fcs500, h500 = genFullCommits(fcs100, h100, 500)
fcs1000, h1000 = genFullCommits(fcs500, h500, 1000)
)
// Lazy load the commits
var fcs5, fcs50, fcs100, fcs500, fcs1000 []lite.FullCommit
var h5, h50, h100, h500, h1000 []int64
var commitsOnce sync.Once
func lazyGenerateFullCommits() {
commitsOnce.Do(func() {
fcs5, h5 = genFullCommits(nil, nil, 5)
fcs50, h50 = genFullCommits(fcs5, h5, 50)
fcs100, h100 = genFullCommits(fcs50, h50, 100)
fcs500, h500 = genFullCommits(fcs100, h100, 500)
fcs1000, h1000 = genFullCommits(fcs500, h500, 1000)
})
}
func BenchmarkMemStoreProviderGetByHeightLinearSearch5(b *testing.B) {
benchmarkMemStoreProviderGetByHeight(b, fcs5, h5, linearSearch)
@ -175,6 +183,8 @@ func BenchmarkMemStoreProviderGetByHeightBinarySearch1000(b *testing.B) {
var rng = rand.New(rand.NewSource(10))
func benchmarkMemStoreProviderGetByHeight(b *testing.B, fcs []lite.FullCommit, fHeights []int64, algo algo) {
lazyGenerateFullCommits()
b.StopTimer()
mp := lite.NewMemStoreProvider()
for i, fc := range fcs {