trie: simplify summary generation

This commit is contained in:
lightclient 2026-02-17 21:14:52 +00:00
parent b4f7b076a5
commit 9b4495b9a8
No known key found for this signature in database
GPG key ID: 91289C7FE4ADADBD

View file

@ -19,6 +19,7 @@ package trie
import ( import (
"bufio" "bufio"
"bytes" "bytes"
"cmp"
"container/heap" "container/heap"
"encoding/binary" "encoding/binary"
"encoding/json" "encoding/json"
@ -464,64 +465,30 @@ func (t *topStorage) Sorted() []*storageSnapshot {
} }
func compareStorageByDepth(a, b *storageSnapshot) int { func compareStorageByDepth(a, b *storageSnapshot) int {
if cmp := compareInt(a.MaxDepth, b.MaxDepth); cmp != 0 { return cmp.Or(
return cmp cmp.Compare(a.MaxDepth, b.MaxDepth),
} cmp.Compare(a.TotalNodes, b.TotalNodes),
if cmp := compareUint64(a.TotalNodes, b.TotalNodes); cmp != 0 { cmp.Compare(a.Summary.Value, b.Summary.Value),
return cmp bytes.Compare(a.Owner[:], b.Owner[:]),
} )
if cmp := compareUint64(a.Summary.Value, b.Summary.Value); cmp != 0 {
return cmp
}
return bytes.Compare(a.Owner[:], b.Owner[:])
} }
func compareStorageByTotal(a, b *storageSnapshot) int { func compareStorageByTotal(a, b *storageSnapshot) int {
if cmp := compareUint64(a.TotalNodes, b.TotalNodes); cmp != 0 { return cmp.Or(
return cmp cmp.Compare(a.TotalNodes, b.TotalNodes),
} cmp.Compare(a.MaxDepth, b.MaxDepth),
if cmp := compareInt(a.MaxDepth, b.MaxDepth); cmp != 0 { cmp.Compare(a.Summary.Value, b.Summary.Value),
return cmp bytes.Compare(a.Owner[:], b.Owner[:]),
} )
if cmp := compareUint64(a.Summary.Value, b.Summary.Value); cmp != 0 {
return cmp
}
return bytes.Compare(a.Owner[:], b.Owner[:])
} }
func compareStorageByValue(a, b *storageSnapshot) int { func compareStorageByValue(a, b *storageSnapshot) int {
if cmp := compareUint64(a.Summary.Value, b.Summary.Value); cmp != 0 { return cmp.Or(
return cmp cmp.Compare(a.Summary.Value, b.Summary.Value),
} cmp.Compare(a.MaxDepth, b.MaxDepth),
if cmp := compareInt(a.MaxDepth, b.MaxDepth); cmp != 0 { cmp.Compare(a.TotalNodes, b.TotalNodes),
return cmp bytes.Compare(a.Owner[:], b.Owner[:]),
} )
if cmp := compareUint64(a.TotalNodes, b.TotalNodes); cmp != 0 {
return cmp
}
return bytes.Compare(a.Owner[:], b.Owner[:])
}
func compareInt(a, b int) int {
switch {
case a > b:
return 1
case a < b:
return -1
default:
return 0
}
}
func compareUint64(a, b uint64) int {
switch {
case a > b:
return 1
case a < b:
return -1
default:
return 0
}
} }
type inspectSummary struct { type inspectSummary struct {