mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-05-10 01:56:37 +00:00
This PR addresses one of the biggest performance issue with binary tries: storing each internal node individually bloats the index, the disk, and triggers a lot of write amplifications. To fix this issue, this PR serializes groups of nodes together. Because we are still looking for the ideal group size, the "depth" of the group tree is made a parameter, but that will be removed in the future, once the perfect size is known. This is a rebase of #33658 --------- Co-authored-by: Copilot <copilot@github.com>
58 lines
2 KiB
Go
58 lines
2 KiB
Go
// Copyright 2025 go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
package bintrie
|
|
|
|
import "github.com/ethereum/go-ethereum/common"
|
|
|
|
// zero is the zero value for a 32-byte array.
|
|
var zero [32]byte
|
|
|
|
const (
|
|
StemNodeWidth = 256 // Number of children per leaf node
|
|
StemSize = 31 // Number of bytes to travel before reaching a group of leaves
|
|
NodeTypeBytes = 1 // Size of node type prefix in serialization
|
|
HashSize = 32 // Size of a hash in bytes
|
|
StemBitmapSize = 32 // Size of the bitmap in a stem node (256 values = 32 bytes)
|
|
|
|
MaxGroupDepth = 8
|
|
)
|
|
|
|
// bitmapSizeForDepth returns the bitmap size in bytes for a given group depth.
|
|
// For depths 1-3, returns 1 byte. For depths 4-8, returns 2^(depth-3) bytes.
|
|
func bitmapSizeForDepth(groupDepth int) int {
|
|
if groupDepth <= 3 {
|
|
return 1
|
|
}
|
|
return 1 << (groupDepth - 3)
|
|
}
|
|
|
|
const (
|
|
nodeTypeStem = iota + 1
|
|
nodeTypeInternal
|
|
)
|
|
|
|
// DeserializeAndHash deserializes a node from bytes and returns its hash.
|
|
// This is a convenience function for external callers that need to compute
|
|
// the hash of a serialized node without maintaining a nodeStore.
|
|
func DeserializeAndHash(blob []byte, depth int) (common.Hash, error) {
|
|
s := newNodeStore()
|
|
ref, err := s.deserializeNode(blob, depth)
|
|
if err != nil {
|
|
return common.Hash{}, err
|
|
}
|
|
return s.computeHash(ref), nil
|
|
}
|