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

@ -24,20 +24,27 @@ import (
"github.com/ethereum/go-ethereum/common"
)
type weightedIterator struct {
it AccountIterator
priority int
}
// fastAccountIterator is a more optimized multi-layer iterator which maintains a
// direct mapping of all iterators leading down to the bottom layer
type fastAccountIterator struct {
iterators []AccountIterator
iterators []*weightedIterator
initiated bool
fail error
}
// The fast iterator does not query parents as much.
// newFastAccountIterator creates a new fastAccountIterator
func (dl *diffLayer) newFastAccountIterator() AccountIterator {
f := &fastAccountIterator{
iterators: dl.iterators(),
initiated: false,
}
for i, it := range dl.iterators() {
f.iterators = append(f.iterators, &weightedIterator{it, -i})
}
f.Seek(common.Hash{})
return f
}
@ -49,9 +56,17 @@ func (fi *fastAccountIterator) Len() int {
// Less implements sort.Interface
func (fi *fastAccountIterator) Less(i, j int) bool {
a := fi.iterators[i].Key()
b := fi.iterators[j].Key()
return bytes.Compare(a[:], b[:]) < 0
a := fi.iterators[i].it.Key()
b := fi.iterators[j].it.Key()
bDiff := bytes.Compare(a[:], b[:])
if bDiff < 0 {
return true
}
if bDiff > 0 {
return false
}
// keys are equal, sort by iterator priority
return fi.iterators[i].priority < fi.iterators[j].priority
}
// Swap implements sort.Interface
@ -61,23 +76,42 @@ func (fi *fastAccountIterator) Swap(i, j int) {
func (fi *fastAccountIterator) Seek(key common.Hash) {
// We need to apply this across all iterators
var seen = make(map[common.Hash]struct{})
var seen = make(map[common.Hash]int)
length := len(fi.iterators)
for i, it := range fi.iterators {
it.Seek(key)
for i := 0; i < len(fi.iterators); i++ {
//for i, it := range fi.iterators {
it := fi.iterators[i]
it.it.Seek(key)
for {
if !it.Next() {
if !it.it.Next() {
// To be removed
// swap it to the last position for now
fi.iterators[i], fi.iterators[length-1] = fi.iterators[length-1], fi.iterators[i]
length--
break
}
v := it.Key()
if _, exist := seen[v]; !exist {
seen[v] = struct{}{}
v := it.it.Key()
if other, exist := seen[v]; !exist {
seen[v] = i
break
} else {
// This whole else-block can be avoided, if we instead
// do an inital priority-sort of the iterators. If we do that,
// then we'll only wind up here if a lower-priority (preferred) iterator
// has the same value, and then we will always just continue.
// However, it costs an extra sort, so it's probably not better
// One needs to be progressed, use priority to determine which
if fi.iterators[other].priority < it.priority {
// the 'it' should be progressed
continue
} else {
// the 'other' should be progressed - swap them
it = fi.iterators[other]
fi.iterators[other], fi.iterators[i] = fi.iterators[i], fi.iterators[other]
continue
}
}
}
}
@ -110,7 +144,7 @@ func (fi *fastAccountIterator) Next() bool {
// innerNext(3), which will call Next on elem 3 (the second '5'). It will continue
// along the list and apply the same operation if needed
func (fi *fastAccountIterator) innerNext(pos int) bool {
if !fi.iterators[pos].Next() {
if !fi.iterators[pos].it.Next() {
//Exhausted, remove this iterator
fi.remove(pos)
if len(fi.iterators) == 0 {
@ -123,23 +157,23 @@ func (fi *fastAccountIterator) innerNext(pos int) bool {
return true
}
// We next:ed the elem at 'pos'. Now we may have to re-sort that elem
val, neighbour := fi.iterators[pos].Key(), fi.iterators[pos+1].Key()
diff := bytes.Compare(val[:], neighbour[:])
if diff < 0 {
var (
current, neighbour = fi.iterators[pos], fi.iterators[pos+1]
val, neighbourVal = current.it.Key(), neighbour.it.Key()
)
if diff := bytes.Compare(val[:], neighbourVal[:]); diff < 0 {
// It is still in correct place
return true
}
if diff == 0 {
// It has same value as the neighbour. So still in correct place, but
// we need to iterate on the neighbour
} else if diff == 0 && current.priority < neighbour.priority {
// So still in correct place, but we need to iterate on the neighbour
fi.innerNext(pos + 1)
return true
}
// At this point, the elem is in the wrong location, but the
// remaining list is sorted. Find out where to move the elem
iterationNeeded := false
iteratee := -1
index := sort.Search(len(fi.iterators), func(n int) bool {
if n <= pos {
if n < pos {
// No need to search 'behind' us
return false
}
@ -147,18 +181,29 @@ func (fi *fastAccountIterator) innerNext(pos int) bool {
// Can always place an elem last
return true
}
neighbour := fi.iterators[n+1].Key()
diff := bytes.Compare(val[:], neighbour[:])
if diff == 0 {
// The elem we're placing it next to has the same value,
// so it's going to need further iteration
iterationNeeded = true
neighbour := fi.iterators[n+1].it.Key()
if diff := bytes.Compare(val[:], neighbour[:]); diff < 0 {
return true
} else if diff > 0 {
return false
}
return diff < 0
// The elem we're placing it next to has the same value,
// so whichever winds up on n+1 will need further iteraton
iteratee = n + 1
if current.priority < fi.iterators[n+1].priority {
// We can drop the iterator here
return true
}
// We need to move it one step further
return false
// TODO benchmark which is best, this works too:
//iteratee = n
//return true
// Doing so should finish the current search earlier
})
fi.move(pos, index)
if iterationNeeded {
fi.innerNext(index)
if iteratee != -1 {
fi.innerNext(iteratee)
}
return true
}
@ -171,7 +216,7 @@ func (fi *fastAccountIterator) move(index, newpos int) {
var (
elem = fi.iterators[index]
middle = fi.iterators[index+1 : newpos+1]
suffix []AccountIterator
suffix []*weightedIterator
)
if newpos < len(fi.iterators)-1 {
suffix = fi.iterators[newpos+1:]
@ -194,18 +239,18 @@ func (fi *fastAccountIterator) Error() error {
// Key returns the current key
func (fi *fastAccountIterator) Key() common.Hash {
return fi.iterators[0].Key()
return fi.iterators[0].it.Key()
}
// Value returns the current key
func (fi *fastAccountIterator) Value() []byte {
panic("todo")
return fi.iterators[0].it.Value()
}
// Debug is a convencience helper during testing
func (fi *fastAccountIterator) Debug() {
for _, it := range fi.iterators {
fmt.Printf(" %v ", it.Key()[31])
fmt.Printf("[p=%v v=%v] ", it.priority, it.it.Key()[0])
}
fmt.Println()
}