Skip to content

Commit ddede19

Browse files
committed
parallel count leaves
1 parent a043d2f commit ddede19

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

trie/zk_trie.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,10 +239,10 @@ func (t *ZkTrie) CountLeaves() uint64 {
239239
if err != nil {
240240
panic("CountLeaves cannot get root")
241241
}
242-
return t.countLeaves(root)
242+
return t.countLeaves(root, 0)
243243
}
244244

245-
func (t *ZkTrie) countLeaves(root *zkt.Hash) uint64 {
245+
func (t *ZkTrie) countLeaves(root *zkt.Hash, depth int) uint64 {
246246
if root == nil {
247247
return 0
248248
}
@@ -255,7 +255,18 @@ func (t *ZkTrie) countLeaves(root *zkt.Hash) uint64 {
255255
if rootNode.Type == zktrie.NodeTypeLeaf_New {
256256
return 1
257257
} else {
258-
return t.countLeaves(rootNode.ChildL) + t.countLeaves(rootNode.ChildR)
258+
count := make(chan uint64)
259+
if depth < 5 {
260+
go func() {
261+
count <- t.countLeaves(rootNode.ChildL, depth+1)
262+
}()
263+
go func() {
264+
count <- t.countLeaves(rootNode.ChildR, depth+1)
265+
}()
266+
return <-count + <-count
267+
} else {
268+
return t.countLeaves(rootNode.ChildL, depth+1) + t.countLeaves(rootNode.ChildR, depth+1)
269+
}
259270
}
260271
}
261272

0 commit comments

Comments
 (0)