trie: thread out only on topmost fullnode

This commit is contained in:
rjl493456442 2017-10-13 18:39:53 +08:00 committed by Péter Szilágyi
parent 475aa30a47
commit b21ff51e50
No known key found for this signature in database
GPG key ID: E9AE538CEDF8293D

View file

@ -28,6 +28,7 @@ import (
type hasher struct { type hasher struct {
cachegen, cachelimit uint16 cachegen, cachelimit uint16
threaded bool
mu sync.Mutex mu sync.Mutex
} }
@ -136,22 +137,40 @@ func (h *hasher) hashChildren(original node, db DatabaseWriter) (node, node, err
// Hash the full node's children, caching the newly hashed subtrees // Hash the full node's children, caching the newly hashed subtrees
var wg sync.WaitGroup var wg sync.WaitGroup
collapsed, cached := n.copy(), n.copy() collapsed, cached := n.copy(), n.copy()
for i := 0; i < 16; i++ { if !h.threaded {
if n.Children[i] != nil { // Calculate children hash concurrently.
wg.Add(1) // Note. Avoid creating too much goroutines(which may hits GC),
go func(i int) { // we only thread out on the topmost full node.
for i := 0; i < 16; i++ {
if n.Children[i] != nil {
wg.Add(1)
go func(i int) {
var herr error
collapsed.Children[i], cached.Children[i], herr = h.hash(n.Children[i], db, false)
if herr != nil {
err = herr
}
wg.Done()
}(i)
} else {
collapsed.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
}
}
wg.Wait()
h.threaded = true
} else {
for i := 0; i < 16; i++ {
if n.Children[i] != nil {
var herr error var herr error
collapsed.Children[i], cached.Children[i], herr = h.hash(n.Children[i], db, false) collapsed.Children[i], cached.Children[i], herr = h.hash(n.Children[i], db, false)
if herr != nil { if herr != nil {
err = herr err = herr
} }
wg.Done() } else {
}(i) collapsed.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
} else { }
collapsed.Children[i] = valueNode(nil) // Ensure that nil children are encoded as empty strings.
} }
} }
wg.Wait()
if err != nil { if err != nil {
return original, original, err return original, original, err
} }