Skip to content

Commit

Permalink
optimize FindMergeRange (#13491)
Browse files Browse the repository at this point in the history
Now it does too much allocs because often parsing file names of
`.Preverified` immutable list:
<img width="892" alt="Screenshot 2025-01-18 at 12 15 55"
src="https://github.com/user-attachments/assets/a01f8891-7ef6-4c97-9ed6-f037b8db9cf0"
/>

I don't want to make more complex changes before `beta1` release - so
small memoization is enough.
  • Loading branch information
AskAlexSharov authored Jan 22, 2025
1 parent ffd8607 commit 6822a48
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
25 changes: 17 additions & 8 deletions erigon-lib/chain/snapcfg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,17 +370,27 @@ func doSort(in map[string]string) Preverified {

func newCfg(networkName string, preverified Preverified) *Cfg {
maxBlockNum, _ := preverified.MaxBlock(0)
return &Cfg{ExpectBlocks: maxBlockNum, Preverified: preverified, networkName: networkName}
cfg := &Cfg{ExpectBlocks: maxBlockNum, Preverified: preverified, networkName: networkName}
cfg.PreverifiedParsed = make([]*snaptype.FileInfo, len(preverified))
for i, p := range cfg.Preverified {
info, _, ok := snaptype.ParseFileName("", p.Name)
if !ok {
continue
}
cfg.PreverifiedParsed[i] = &info
}
return cfg
}

func NewNonSeededCfg(networkName string) *Cfg {
return newCfg(networkName, Preverified{})
}

type Cfg struct {
ExpectBlocks uint64
Preverified Preverified
networkName string
ExpectBlocks uint64
Preverified Preverified // immutable
PreverifiedParsed []*snaptype.FileInfo //Preverified field after `snaptype.ParseFileName("", p.Name)`
networkName string
}

// Seedable - can seed it over Bittorrent network to other nodes
Expand All @@ -398,12 +408,11 @@ func (c Cfg) IsFrozen(info snaptype.FileInfo) bool {
func (c Cfg) MergeLimit(t snaptype.Enum, fromBlock uint64) uint64 {
hasType := t == snaptype.MinCoreEnum

for _, p := range c.Preverified {
info, _, ok := snaptype.ParseFileName("", p.Name)
if !ok {
for _, info := range c.PreverifiedParsed {
if info == nil {
continue
}
if strings.Contains(p.Name, "caplin") {
if strings.Contains(info.Name(), "caplin") {
continue
}

Expand Down
2 changes: 1 addition & 1 deletion turbo/snapshotsync/merger.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func (m *Merger) FindMergeRanges(currentRanges []Range, maxBlockNum uint64) (toM
cfg := snapcfg.KnownCfg(m.chainConfig.ChainName)
for i := len(currentRanges) - 1; i > 0; i-- {
r := currentRanges[i]
mergeLimit := snapcfg.MergeLimitFromCfg(cfg, snaptype.Unknown, r.From())
mergeLimit := cfg.MergeLimit(snaptype.Unknown, r.From())
if r.To()-r.From() >= mergeLimit {
continue
}
Expand Down

0 comments on commit 6822a48

Please sign in to comment.