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) vset := v.ToValidators(1, 0)
// fill in the votes we want // fill in the votes we want
for i := first; i < last; i++ { for i := first; i < last && i < len(v); i++ {
if i >= len(v) {
break
}
vote := makeVote(header, vset, v[i]) vote := makeVote(header, vset, v[i])
votes[vote.ValidatorIndex] = vote 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. // GetByHeight returns the FullCommit for height h or an error if the commit is not found.
func (m *memStoreProvider) GetByHeight(h int64) (FullCommit, error) { func (m *memStoreProvider) GetByHeight(h int64) (FullCommit, error) {
m.mtx.RLock() m.mtx.Lock()
defer m.mtx.RUnlock() defer m.mtx.Unlock()
if !m.sorted { if !m.sorted {
sort.Sort(m.byHeight) sort.Sort(m.byHeight)
m.sorted = true 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. // GetByHeight returns the FullCommit for height h or an error if the commit is not found.
func (m *memStoreProvider) GetByHeightBinarySearch(h int64) (FullCommit, error) { func (m *memStoreProvider) GetByHeightBinarySearch(h int64) (FullCommit, error) {
m.mtx.RLock() m.mtx.Lock()
defer m.mtx.RUnlock() defer m.mtx.Unlock()
if !m.sorted { if !m.sorted {
sort.Sort(m.byHeight) sort.Sort(m.byHeight)
m.sorted = true m.sorted = true

View File

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