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 551b36b commit 66590e3
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 @@ -192,22 +192,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 @@ -222,17 +224,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 66590e3

Please sign in to comment.