core/state/snapshot: implement iterator priority for fast direct data lookup

This commit is contained in:
Martin Holst Swende
2019-12-06 23:27:18 +01:00
committed by Péter Szilágyi
parent e567675473
commit e570835356
2 changed files with 250 additions and 47 deletions

View File

@ -19,6 +19,7 @@ package snapshot
import (
"bytes"
"encoding/binary"
"fmt"
"math/rand"
"testing"
@ -95,9 +96,10 @@ func TestFastIteratorBasics(t *testing.T) {
{9, 10}, {10, 13, 15, 16}},
expKeys: []byte{0, 1, 2, 7, 8, 9, 10, 13, 14, 15, 16}},
} {
var iterators []AccountIterator
for _, data := range tc.lists {
iterators = append(iterators, newTestIterator(data...))
var iterators []*weightedIterator
for i, data := range tc.lists {
it := newTestIterator(data...)
iterators = append(iterators, &weightedIterator{it, i})
}
fi := &fastAccountIterator{
@ -162,6 +164,69 @@ func TestIteratorTraversal(t *testing.T) {
verifyIterator(t, 7, child.newFastAccountIterator())
}
// TestIteratorTraversalValues tests some multi-layer iteration, where we
// also expect the correct values to show up
func TestIteratorTraversalValues(t *testing.T) {
var (
storage = make(map[common.Hash]map[common.Hash][]byte)
a = make(map[common.Hash][]byte)
b = make(map[common.Hash][]byte)
c = make(map[common.Hash][]byte)
d = make(map[common.Hash][]byte)
e = make(map[common.Hash][]byte)
f = make(map[common.Hash][]byte)
g = make(map[common.Hash][]byte)
h = make(map[common.Hash][]byte)
)
// entries in multiple layers should only become output once
for i := byte(2); i < 0xff; i++ {
a[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 0, i))
if i > 20 && i%2 == 0 {
b[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 1, i))
}
if i%4 == 0 {
c[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 2, i))
}
if i%7 == 0 {
d[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 3, i))
}
if i%8 == 0 {
e[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 4, i))
}
if i > 50 || i < 85 {
f[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 5, i))
}
if i%64 == 0 {
g[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 6, i))
}
if i%128 == 0 {
h[common.Hash{i}] = []byte(fmt.Sprintf("layer-%d, key %d", 7, i))
}
}
child := newDiffLayer(emptyLayer(), common.Hash{}, a, storage).
Update(common.Hash{}, b, storage).
Update(common.Hash{}, c, storage).
Update(common.Hash{}, d, storage).
Update(common.Hash{}, e, storage).
Update(common.Hash{}, f, storage).
Update(common.Hash{}, g, storage).
Update(common.Hash{}, h, storage)
it := child.newFastAccountIterator()
for it.Next() {
key := it.Key()
exp, err := child.accountRLP(key, 0)
if err != nil {
t.Fatal(err)
}
got := it.Value()
if !bytes.Equal(exp, got) {
t.Fatalf("Error on key %x, got %v exp %v", key, string(got), string(exp))
}
//fmt.Printf("val: %v\n", string(it.Value()))
}
}
func TestIteratorLargeTraversal(t *testing.T) {
// This testcase is a bit notorious -- all layers contain the exact
// same 200 accounts.
@ -195,8 +260,14 @@ func TestIteratorLargeTraversal(t *testing.T) {
// same 200 accounts. That means that we need to process 2000 items, but only
// spit out 200 values eventually.
//
//BenchmarkIteratorTraversal/binary_iterator-6 2008 573290 ns/op 9520 B/op 199 allocs/op
//BenchmarkIteratorTraversal/fast_iterator-6 1946 575596 ns/op 20146 B/op 134 allocs/op
// The value-fetching benchmark is easy on the binary iterator, since it never has to reach
// down at any depth for retrieving the values -- all are on the toppmost layer
//
// BenchmarkIteratorTraversal/binary_iterator_keys-6 2239 483674 ns/op
// BenchmarkIteratorTraversal/binary_iterator_values-6 2403 501810 ns/op
// BenchmarkIteratorTraversal/fast_iterator_keys-6 1923 677966 ns/op
// BenchmarkIteratorTraversal/fast_iterator_values-6 1741 649967 ns/op
//
func BenchmarkIteratorTraversal(b *testing.B) {
var storage = make(map[common.Hash]map[common.Hash][]byte)
@ -224,7 +295,7 @@ func BenchmarkIteratorTraversal(b *testing.B) {
// We call this once before the benchmark, so the creation of
// sorted accountlists are not included in the results.
child.newBinaryAccountIterator()
b.Run("binary iterator", func(b *testing.B) {
b.Run("binary iterator keys", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newBinaryAccountIterator()
@ -236,7 +307,20 @@ func BenchmarkIteratorTraversal(b *testing.B) {
}
}
})
b.Run("fast iterator", func(b *testing.B) {
b.Run("binary iterator values", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newBinaryAccountIterator()
for it.Next() {
got++
child.accountRLP(it.Key(), 0)
}
if exp := 200; got != exp {
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
}
}
})
b.Run("fast iterator keys", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newFastAccountIterator()
@ -248,6 +332,19 @@ func BenchmarkIteratorTraversal(b *testing.B) {
}
}
})
b.Run("fast iterator values", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newFastAccountIterator()
for it.Next() {
got++
it.Value()
}
if exp := 200; got != exp {
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
}
}
})
}
// BenchmarkIteratorLargeBaselayer is a pretty realistic benchmark, where
@ -256,8 +353,10 @@ func BenchmarkIteratorTraversal(b *testing.B) {
// This is heavy on the binary iterator, which in most cases will have to
// call recursively 100 times for the majority of the values
//
// BenchmarkIteratorLargeBaselayer/binary_iterator-6 585 2067377 ns/op 9520 B/op 199 allocs/op
// BenchmarkIteratorLargeBaselayer/fast_iterator-6 13198 91043 ns/op 8601 B/op 118 allocs/op
// BenchmarkIteratorLargeBaselayer/binary_iterator_(keys)-6 514 1971999 ns/op
// BenchmarkIteratorLargeBaselayer/fast_iterator_(keys)-6 10000 114385 ns/op
// BenchmarkIteratorLargeBaselayer/binary_iterator_(values)-6 61 18997492 ns/op
// BenchmarkIteratorLargeBaselayer/fast_iterator_(values)-6 4047 296823 ns/op
func BenchmarkIteratorLargeBaselayer(b *testing.B) {
var storage = make(map[common.Hash]map[common.Hash][]byte)
@ -285,7 +384,7 @@ func BenchmarkIteratorLargeBaselayer(b *testing.B) {
// We call this once before the benchmark, so the creation of
// sorted accountlists are not included in the results.
child.newBinaryAccountIterator()
b.Run("binary iterator", func(b *testing.B) {
b.Run("binary iterator (keys)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newBinaryAccountIterator()
@ -297,7 +396,7 @@ func BenchmarkIteratorLargeBaselayer(b *testing.B) {
}
}
})
b.Run("fast iterator", func(b *testing.B) {
b.Run("fast iterator (keys)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newFastAccountIterator()
@ -309,6 +408,34 @@ func BenchmarkIteratorLargeBaselayer(b *testing.B) {
}
}
})
b.Run("binary iterator (values)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newBinaryAccountIterator()
for it.Next() {
got++
v := it.Key()
child.accountRLP(v, -0)
}
if exp := 2000; got != exp {
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
}
}
})
b.Run("fast iterator (values)", func(b *testing.B) {
for i := 0; i < b.N; i++ {
got := 0
it := child.newFastAccountIterator()
for it.Next() {
it.Value()
got++
}
if exp := 2000; got != exp {
b.Errorf("iterator len wrong, expected %d, got %d", exp, got)
}
}
})
}
// TestIteratorFlatting tests what happens when we
@ -394,3 +521,34 @@ func TestIteratorSeek(t *testing.T) {
it.Seek(common.HexToHash("0xff"))
verifyIterator(t, 0, it)
}
//BenchmarkIteratorSeek/init+seek-6 4328 245477 ns/op
func BenchmarkIteratorSeek(b *testing.B) {
var storage = make(map[common.Hash]map[common.Hash][]byte)
mkAccounts := func(num int) map[common.Hash][]byte {
accounts := make(map[common.Hash][]byte)
for i := 0; i < num; i++ {
h := common.Hash{}
binary.BigEndian.PutUint64(h[:], uint64(i+1))
accounts[h] = randomAccount()
}
return accounts
}
layer := newDiffLayer(emptyLayer(), common.Hash{}, mkAccounts(200), storage)
for i := 1; i < 100; i++ {
layer = layer.Update(common.Hash{},
mkAccounts(200), storage)
}
b.Run("init+seek", func(b *testing.B) {
b.ResetTimer()
seekpos := make([]byte, 20)
for i := 0; i < b.N; i++ {
b.StopTimer()
rand.Read(seekpos)
it := layer.newFastAccountIterator()
b.StartTimer()
it.Seek(common.BytesToHash(seekpos))
}
})
}