Skip to content

Commit

Permalink
speedup files open (#12995)
Browse files Browse the repository at this point in the history
`KnownCfg` call is very expensive
  • Loading branch information
AskAlexSharov authored Dec 5, 2024
1 parent 8352b8c commit 234947a
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 16 deletions.
11 changes: 2 additions & 9 deletions erigon-lib/chain/snapcfg/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,16 @@ func Seedable(networkName string, info snaptype.FileInfo) bool {
return KnownCfg(networkName).Seedable(info)
}

func IsFrozen(networkName string, info snaptype.FileInfo) bool {
if networkName == "" {
return false
}
return KnownCfg(networkName).IsFrozen(info)
}

func MergeLimitFromCfg(cfg *Cfg, snapType snaptype.Enum, fromBlock uint64) uint64 {
return cfg.MergeLimit(snapType, fromBlock)
}

func MaxSeedableSegment(chain string, dir string) uint64 {
var _max uint64

segConfig := KnownCfg(chain)
if list, err := snaptype.Segments(dir); err == nil {
for _, info := range list {
if Seedable(chain, info) && info.Type.Enum() == snaptype.MinCoreEnum && info.To > _max {
if segConfig.Seedable(info) && info.Type.Enum() == snaptype.MinCoreEnum && info.To > _max {
_max = info.To
}
}
Expand Down
4 changes: 3 additions & 1 deletion erigon-lib/downloader/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ func seedableSegmentFiles(dir string, chainName string, skipSeedableCheck bool)
return nil, err
}

segConfig := snapcfg.KnownCfg(chainName)

res := make([]string, 0, len(files))
for _, fPath := range files {
_, name := filepath.Split(fPath)
Expand All @@ -97,7 +99,7 @@ func seedableSegmentFiles(dir string, chainName string, skipSeedableCheck bool)
if !skipSeedableCheck && (!ok || isStateFile) {
continue
}
if !skipSeedableCheck && !snapcfg.Seedable(chainName, ff) {
if !skipSeedableCheck && !segConfig.Seedable(ff) {
continue
}
res = append(res, name)
Expand Down
4 changes: 3 additions & 1 deletion turbo/snapshotsync/caplin_state_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,8 @@ func (s *CaplinStateSnapshots) OpenList(fileNames []string, optimistic bool) err
s.dirtySegmentsLock.Lock()
defer s.dirtySegmentsLock.Unlock()

snConfig := snapcfg.KnownCfg(s.cfg.ChainName)

s.closeWhatNotInList(fileNames)
var segmentsMax uint64
var segmentsMaxSet bool
Expand Down Expand Up @@ -303,7 +305,7 @@ Loop:
// segType: f.Type, Unsupported
version: f.Version,
Range: Range{f.From, f.To},
frozen: snapcfg.IsFrozen(s.cfg.ChainName, f),
frozen: snConfig.IsFrozen(f),
filePath: filePath,
}
}
Expand Down
6 changes: 4 additions & 2 deletions turbo/snapshotsync/freezeblocks/caplin_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ func (s *CaplinSnapshots) OpenList(fileNames []string, optimistic bool) error {
s.dirtyLock.Lock()
defer s.dirtyLock.Unlock()

snConfig := snapcfg.KnownCfg(s.cfg.ChainName)

s.closeWhatNotInList(fileNames)
var segmentsMax uint64
var segmentsMaxSet bool
Expand Down Expand Up @@ -191,7 +193,7 @@ Loop:
snaptype.BeaconBlocks,
f.Version,
f.From, f.To,
snapcfg.IsFrozen(s.cfg.ChainName, f))
snConfig.IsFrozen(f))
}
if err := sn.Open(s.dir); err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down Expand Up @@ -247,7 +249,7 @@ Loop:
snaptype.BlobSidecars,
f.Version,
f.From, f.To,
snapcfg.IsFrozen(s.cfg.ChainName, f))
snConfig.IsFrozen(f))
}
if err := sn.Open(s.dir); err != nil {
if errors.Is(err, os.ErrNotExist) {
Expand Down
5 changes: 2 additions & 3 deletions turbo/snapshotsync/snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,8 +1019,7 @@ func (s *RoSnapshots) openSegments(fileNames []string, open bool, optimistic boo
var segmentsMax uint64
var segmentsMaxSet bool

//fmt.Println("RS", s)
//defer fmt.Println("Done RS", s)
snConfig := snapcfg.KnownCfg(s.cfg.ChainName)

for _, fName := range fileNames {
f, isState, ok := snaptype.ParseFileName(s.dir, fName)
Expand Down Expand Up @@ -1054,7 +1053,7 @@ func (s *RoSnapshots) openSegments(fileNames []string, open bool, optimistic boo
})

if !exists {
sn = &DirtySegment{segType: f.Type, version: f.Version, Range: Range{f.From, f.To}, frozen: snapcfg.IsFrozen(s.cfg.ChainName, f)}
sn = &DirtySegment{segType: f.Type, version: f.Version, Range: Range{f.From, f.To}, frozen: snConfig.IsFrozen(f)}
}

if open {
Expand Down

0 comments on commit 234947a

Please sign in to comment.