diff --git a/lite/helpers.go b/lite/helpers.go index fc4d697ae..d985882de 100644 --- a/lite/helpers.go +++ b/lite/helpers.go @@ -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 } diff --git a/lite/memprovider.go b/lite/memprovider.go index bfd260ce5..574aed8dd 100644 --- a/lite/memprovider.go +++ b/lite/memprovider.go @@ -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 diff --git a/lite/performance_test.go b/lite/performance_test.go index e91671292..da5ead840 100644 --- a/lite/performance_test.go +++ b/lite/performance_test.go @@ -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 {