From 6e36636c9c3dfb95a00bd624ccc57dd3e6bf9a6d Mon Sep 17 00:00:00 2001 From: CPerezz Date: Sun, 19 Apr 2026 22:15:23 +0200 Subject: [PATCH] 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. --- trie/bintrie/store_commit.go | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/trie/bintrie/store_commit.go b/trie/bintrie/store_commit.go index a60bc61697..ba53e2c530 100644 --- a/trie/bintrie/store_commit.go +++ b/trie/bintrie/store_commit.go @@ -48,26 +48,24 @@ func (s *NodeStore) computeHash(ref nodeRef) common.Hash { } } -// parallelDepth returns the tree depth below which hashInternal spawns -// goroutines for shallow-depth parallelism. -func parallelDepth() int { - return min(bits.Len(uint(runtime.NumCPU())), 8) -} +// parallelHashDepth is the tree depth below which hashInternal spawns +// goroutines for shallow-depth parallelism. Computed once at init because +// NumCPU() never changes after startup. +var parallelHashDepth = min(bits.Len(uint(runtime.NumCPU())), 8) // 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 // are combined. Below that threshold the goroutine spawn cost outweighs the -// hashing work, so deeper nodes hash both children sequentially via the -// pooled hasher. +// hashing work, so deeper nodes hash both children sequentially. func (s *NodeStore) hashInternal(idx uint32) common.Hash { node := s.getInternal(idx) if !node.mustRecompute { return node.hash } - if int(node.depth) < parallelDepth() { + if int(node.depth) < parallelHashDepth { var input [64]byte var lh common.Hash var wg sync.WaitGroup