go-ethereum/trie/bintrie
CPerezz 6138a11c39
trie/bintrie: parallelize InternalNode.Hash at shallow tree depths (#34032)
## Summary

At tree depths below `log2(NumCPU)` (clamped to [2, 8]), hash the left
subtree in a goroutine while hashing the right subtree inline. This
exploits available CPU cores for the top levels of the tree where
subtree hashing is most expensive. On single-core machines, the parallel
path is disabled entirely.

Deeper nodes use sequential hashing with the existing `sync.Pool` hasher
where goroutine overhead would exceed the hash computation cost. The
parallel path uses `sha256.Sum256` with a stack-allocated buffer to
avoid pool contention across goroutines.

**Safety:**
- Left/right subtrees are disjoint — no shared mutable state
- `sync.WaitGroup` provides happens-before guarantee for the result
- `defer wg.Done()` + `recover()` prevents goroutine panics from
crashing the process
- `!bt.mustRecompute` early return means clean nodes never enter the
parallel path
- Hash results are deterministic regardless of computation order — no
consensus risk

## Benchmark (AMD EPYC 48-core, 500K entries, `--benchtime=10s
--count=3`, post-H01 baseline)

| Metric | Baseline | Parallel | Delta |
|--------|----------|----------|-------|
| Approve (Mgas/s) | 224.5 ± 7.1 | **259.6 ± 2.4** | **+15.6%** |
| BalanceOf (Mgas/s) | 982.9 ± 5.1 | 954.3 ± 10.8 | -2.9% (noise, clean
nodes skip parallel path) |
| Allocs/op (approve) | ~810K | ~700K | -13.6% |
2026-03-18 13:54:23 +01:00
..
binary_node.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
binary_node_test.go cmd/evm/internal/t8ntool, trie: support for verkle-at-genesis, use UBT, and move the transition tree to its own package (#32445) 2025-11-14 15:25:30 +01:00
empty.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
empty_test.go trie/bintrie: add eip7864 binary trees and run its tests (#32365) 2025-09-01 21:06:51 +08:00
hashed_node.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
hashed_node_test.go cmd/evm/internal/t8ntool, trie: support for verkle-at-genesis, use UBT, and move the transition tree to its own package (#32445) 2025-11-14 15:25:30 +01:00
hasher.go trie/bintrie: use a sync.Pool when hashing binary tree nodes (#33989) 2026-03-12 10:20:12 +01:00
internal_node.go trie/bintrie: parallelize InternalNode.Hash at shallow tree depths (#34032) 2026-03-18 13:54:23 +01:00
internal_node_test.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
iterator.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
key_encoding.go trie/bintrie: use a sync.Pool when hashing binary tree nodes (#33989) 2026-03-12 10:20:12 +01:00
stem_node.go trie/bintrie: use a sync.Pool when hashing binary tree nodes (#33989) 2026-03-12 10:20:12 +01:00
stem_node_test.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
trie.go trie/bintrie: cache hashes of clean nodes so as not to rehash the whole tree (#33961) 2026-03-06 18:06:24 +01:00
trie_test.go trie/bintrie: use correct key mapping in GetStorage and DeleteStorage (#33807) 2026-02-11 11:42:17 +01:00