trie/bintrie: compute parallelHashDepth once at init

runtime.NumCPU() + bits.Len were recomputed on every hashInternal
call. NumCPU is immutable after startup; hoist to a package var
computed once. Also fixes a minor style wart: the constant is now a
value, not a zero-arg function.
This commit is contained in:
CPerezz 2026-04-19 22:15:23 +02:00
parent f8c283410e
commit 6e36636c9c
No known key found for this signature in database
GPG key ID: 62045F34B97177DD

View file

@ -48,26 +48,24 @@ func (s *NodeStore) computeHash(ref nodeRef) common.Hash {
} }
} }
// parallelDepth returns the tree depth below which hashInternal spawns // parallelHashDepth is the tree depth below which hashInternal spawns
// goroutines for shallow-depth parallelism. // goroutines for shallow-depth parallelism. Computed once at init because
func parallelDepth() int { // NumCPU() never changes after startup.
return min(bits.Len(uint(runtime.NumCPU())), 8) var parallelHashDepth = min(bits.Len(uint(runtime.NumCPU())), 8)
}
// hashInternal hashes an InternalNode and caches the result. // hashInternal hashes an InternalNode and caches the result.
// //
// At shallow depths (< parallelDepth()) the left subtree is hashed in a // At shallow depths (< parallelHashDepth) the left subtree is hashed in a
// goroutine while the right subtree is hashed inline, then the two digests // goroutine while the right subtree is hashed inline, then the two digests
// are combined. Below that threshold the goroutine spawn cost outweighs the // are combined. Below that threshold the goroutine spawn cost outweighs the
// hashing work, so deeper nodes hash both children sequentially via the // hashing work, so deeper nodes hash both children sequentially.
// pooled hasher.
func (s *NodeStore) hashInternal(idx uint32) common.Hash { func (s *NodeStore) hashInternal(idx uint32) common.Hash {
node := s.getInternal(idx) node := s.getInternal(idx)
if !node.mustRecompute { if !node.mustRecompute {
return node.hash return node.hash
} }
if int(node.depth) < parallelDepth() { if int(node.depth) < parallelHashDepth {
var input [64]byte var input [64]byte
var lh common.Hash var lh common.Hash
var wg sync.WaitGroup var wg sync.WaitGroup