mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-20 10:52:25 +00:00
Revert "trie: reduce allocs in recHash (#27770)"
This reverts commit 06434771a1.
This commit is contained in:
parent
725d0336c8
commit
2aa49b926b
3 changed files with 10 additions and 14 deletions
|
|
@ -51,8 +51,9 @@ func hexToCompact(hex []byte) []byte {
|
|||
return buf
|
||||
}
|
||||
|
||||
// hexToCompactInPlace places the compact key in input buffer, returning the compacted key.
|
||||
func hexToCompactInPlace(hex []byte) []byte {
|
||||
// hexToCompactInPlace places the compact key in input buffer, returning the length
|
||||
// needed for the representation
|
||||
func hexToCompactInPlace(hex []byte) int {
|
||||
var (
|
||||
hexLen = len(hex) // length of the hex input
|
||||
firstByte = byte(0)
|
||||
|
|
@ -76,7 +77,7 @@ func hexToCompactInPlace(hex []byte) []byte {
|
|||
hex[bi] = hex[ni]<<4 | hex[ni+1]
|
||||
}
|
||||
hex[0] = firstByte
|
||||
return hex[:binLen]
|
||||
return binLen
|
||||
}
|
||||
|
||||
func compactToHex(compact []byte) []byte {
|
||||
|
|
|
|||
|
|
@ -86,7 +86,8 @@ func TestHexToCompactInPlace(t *testing.T) {
|
|||
} {
|
||||
hexBytes, _ := hex.DecodeString(key)
|
||||
exp := hexToCompact(hexBytes)
|
||||
got := hexToCompactInPlace(hexBytes)
|
||||
sz := hexToCompactInPlace(hexBytes)
|
||||
got := hexBytes[:sz]
|
||||
if !bytes.Equal(exp, got) {
|
||||
t.Fatalf("test %d: encoding err\ninp %v\ngot %x\nexp %x\n", i, key, got, exp)
|
||||
}
|
||||
|
|
@ -101,7 +102,8 @@ func TestHexToCompactInPlaceRandom(t *testing.T) {
|
|||
hexBytes := keybytesToHex(key)
|
||||
hexOrig := []byte(string(hexBytes))
|
||||
exp := hexToCompact(hexBytes)
|
||||
got := hexToCompactInPlace(hexBytes)
|
||||
sz := hexToCompactInPlace(hexBytes)
|
||||
got := hexBytes[:sz]
|
||||
|
||||
if !bytes.Equal(exp, got) {
|
||||
t.Fatalf("encoding err \ncpt %x\nhex %x\ngot %x\nexp %x\n",
|
||||
|
|
@ -117,13 +119,6 @@ func BenchmarkHexToCompact(b *testing.B) {
|
|||
}
|
||||
}
|
||||
|
||||
func BenchmarkHexToCompactInPlace(b *testing.B) {
|
||||
testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
|
||||
for i := 0; i < b.N; i++ {
|
||||
hexToCompactInPlace(testBytes)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkCompactToHex(b *testing.B) {
|
||||
testBytes := []byte{0, 15, 1, 12, 11, 8, 16 /*term*/}
|
||||
for i := 0; i < b.N; i++ {
|
||||
|
|
|
|||
|
|
@ -444,7 +444,7 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
|
|||
case extNode:
|
||||
st.children[0].hashRec(hasher, append(path, st.key...))
|
||||
|
||||
n := shortNode{Key: hexToCompactInPlace(st.key)}
|
||||
n := shortNode{Key: hexToCompact(st.key)}
|
||||
if len(st.children[0].val) < 32 {
|
||||
n.Val = rawNode(st.children[0].val)
|
||||
} else {
|
||||
|
|
@ -460,7 +460,7 @@ func (st *StackTrie) hashRec(hasher *hasher, path []byte) {
|
|||
|
||||
case leafNode:
|
||||
st.key = append(st.key, byte(16))
|
||||
n := shortNode{Key: hexToCompactInPlace(st.key), Val: valueNode(st.val)}
|
||||
n := shortNode{Key: hexToCompact(st.key), Val: valueNode(st.val)}
|
||||
|
||||
n.encode(hasher.encbuf)
|
||||
encodedNode = hasher.encodedBytes()
|
||||
|
|
|
|||
Loading…
Reference in a new issue