triedb/pathdb: optimize allocation

This commit is contained in:
Gary Rong 2026-01-26 16:30:09 +08:00
parent 990a71c952
commit a5500fb0e2

View file

@ -140,9 +140,12 @@ type trienodeHistory struct {
// newTrienodeHistory constructs a trienode history with the provided trie nodes. // newTrienodeHistory constructs a trienode history with the provided trie nodes.
func newTrienodeHistory(root common.Hash, parent common.Hash, block uint64, nodes map[common.Hash]map[string][]byte) *trienodeHistory { func newTrienodeHistory(root common.Hash, parent common.Hash, block uint64, nodes map[common.Hash]map[string][]byte) *trienodeHistory {
nodeList := make(map[common.Hash][]string) nodeList := make(map[common.Hash][]string, len(nodes))
for owner, subset := range nodes { for owner, subset := range nodes {
keys := sort.StringSlice(slices.Collect(maps.Keys(subset))) keys := make(sort.StringSlice, 0, len(subset))
for k := range subset {
keys = append(keys, k)
}
keys.Sort() keys.Sort()
nodeList[owner] = keys nodeList[owner] = keys
} }
@ -518,11 +521,12 @@ func decodeSingle(keySection []byte, onValue func([]byte, int, int) error) error
return nil return nil
} }
func decodeSingleWithValue(keySection []byte, valueSection []byte, valStart uint32) ([]string, map[string][]byte, error) { func decodeSingleWithValue(keySection []byte, valueSection []byte) ([]string, map[string][]byte, error) {
var ( var (
offset int offset int
nodes = make(map[string][]byte) estimated = len(keySection) / 8
paths []string nodes = make(map[string][]byte, estimated)
paths = make([]string, 0, estimated)
) )
err := decodeSingle(keySection, func(key []byte, start int, limit int) error { err := decodeSingle(keySection, func(key []byte, start int, limit int) error {
if start != offset { if start != offset {
@ -539,7 +543,6 @@ func decodeSingleWithValue(keySection []byte, valueSection []byte, valStart uint
paths = append(paths, strkey) paths = append(paths, strkey)
nodes[strkey] = valueSection[start:limit] nodes[strkey] = valueSection[start:limit]
fmt.Println("key", key, "baseoff", valStart, "start in section", start, "limit in section", limit, "value", valueSection[start:limit])
offset = limit offset = limit
return nil return nil
}) })
@ -587,7 +590,7 @@ func (h *trienodeHistory) decode(header []byte, keySection []byte, valueSection
} }
// Decode the key and values for this specific trie // Decode the key and values for this specific trie
paths, nodes, err := decodeSingleWithValue(keySection[keyStart:keyLimit], valueSection[valStart:valLimit], valStart) paths, nodes, err := decodeSingleWithValue(keySection[keyStart:keyLimit], valueSection[valStart:valLimit])
if err != nil { if err != nil {
return err return err
} }