From dfc7439cf558baef75ab6b9658c76269dd621060 Mon Sep 17 00:00:00 2001 From: CPerezz Date: Mon, 16 Mar 2026 23:56:04 +0100 Subject: [PATCH] trie/bintrie: use sha256.Sum256 with stack buffer in InternalNode.Hash Replace the sync.Pool-based sha256 hashing with a single sha256.Sum256 call using a stack-allocated [64]byte buffer. This eliminates pool Get/Put overhead, two interface-dispatched Write() calls, the Sum(nil) heap allocation, and the BytesToHash conversion. --- trie/bintrie/internal_node.go | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/trie/bintrie/internal_node.go b/trie/bintrie/internal_node.go index 7ad76aa9db..e1d3f35eb3 100644 --- a/trie/bintrie/internal_node.go +++ b/trie/bintrie/internal_node.go @@ -17,6 +17,7 @@ package bintrie import ( + "crypto/sha256" "errors" "fmt" @@ -124,19 +125,14 @@ func (bt *InternalNode) Hash() common.Hash { return bt.hash } - h := newSha256() - defer returnSha256(h) + var buf [64]byte if bt.left != nil { - h.Write(bt.left.Hash().Bytes()) - } else { - h.Write(zero[:]) + copy(buf[:32], bt.left.Hash().Bytes()) } if bt.right != nil { - h.Write(bt.right.Hash().Bytes()) - } else { - h.Write(zero[:]) + copy(buf[32:], bt.right.Hash().Bytes()) } - bt.hash = common.BytesToHash(h.Sum(nil)) + bt.hash = sha256.Sum256(buf[:]) bt.mustRecompute = false return bt.hash }