Skip to content

Commit

Permalink
perf: make bootstrap saves O(N)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Sep 21, 2023
1 parent 1d14236 commit d52dc24
Showing 1 changed file with 18 additions and 21 deletions.
39 changes: 18 additions & 21 deletions core/bootstrap/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,22 +161,24 @@ func saveConnectedPeersAsTemporaryBootstrap(ctx context.Context, host host.Host,

bootstrapPeers := cfg.BootstrapPeers()
backupPeers := make([]peer.AddrInfo, 0, cfg.MaxBackupBootstrapSize)
foundPeers := make(map[peer.ID]struct{}, cfg.MaxBackupBootstrapSize+len(bootstrapPeers))

// Don't record bootstrap peers
for _, b := range bootstrapPeers {
foundPeers[b.ID] = struct{}{}
}

// Choose peers to save and filter out the ones that are already bootstrap nodes.
for _, p := range connectedPeers {
found := false
for _, bootstrapPeer := range bootstrapPeers {
if p == bootstrapPeer.ID {
found = true
break
}
}
if !found {
backupPeers = append(backupPeers, peer.AddrInfo{
ID: p,
Addrs: host.Network().Peerstore().Addrs(p),
})
if _, found := foundPeers[p]; found {
continue
}
foundPeers[p] = struct{}{}

backupPeers = append(backupPeers, peer.AddrInfo{
ID: p,
Addrs: host.Network().Peerstore().Addrs(p),
})

if len(backupPeers) >= cfg.MaxBackupBootstrapSize {
break
Expand All @@ -191,17 +193,12 @@ func saveConnectedPeersAsTemporaryBootstrap(ctx context.Context, host host.Host,

// Add some of the old saved peers. Ensure we don't duplicate them.
for _, p := range oldSavedPeers {
found := false
for _, sp := range backupPeers {
if p.ID == sp.ID {
found = true
break
}
if _, found := foundPeers[p.ID]; found {
continue
}
foundPeers[p.ID] = struct{}{}

if !found {
backupPeers = append(backupPeers, p)
}
backupPeers = append(backupPeers, p)

if len(backupPeers) >= cfg.MaxBackupBootstrapSize {
break
Expand Down

0 comments on commit d52dc24

Please sign in to comment.