Skip to content

Commit

Permalink
bitswap/client: fewer wantlist iterations in sendCancels (#819)
Browse files Browse the repository at this point in the history
Combine loops to remove redundant wantlist iteration.
  • Loading branch information
gammazero authored Jan 31, 2025
1 parent b0125d1 commit 3c70cf3
Showing 1 changed file with 31 additions and 38 deletions.
69 changes: 31 additions & 38 deletions bitswap/client/internal/peermanager/peerwantmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,58 +283,51 @@ func (pwm *peerWantManager) sendCancels(cancelKs []cid.Cid) {
}
}

if len(broadcastCancels) > 0 {
// If a broadcast want is being cancelled, send the cancel to all
// peers
for p, pws := range pwm.peerWants {
send(p, pws)
}
} else {
// Only send cancels to peers that received a corresponding want
cancelPeers := make(map[peer.ID]struct{}, len(pwm.wantPeers[cancelKs[0]]))
for _, c := range cancelKs {
for p := range pwm.wantPeers[c] {
cancelPeers[p] = struct{}{}
}
}
for p := range cancelPeers {
pws, ok := pwm.peerWants[p]
if !ok {
// Should never happen but check just in case
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
continue
}

send(p, pws)
}
}

// Decrement the wants gauges
for _, c := range cancelKs {
clearWantsForCID := func(c cid.Cid) {
peerCnts := peerCounts[c]

// If there were any peers that had a pending want-block for the key
if peerCnts.wantBlock > 0 {
// Decrement the want-block gauge
pwm.wantBlockGauge.Dec()
}

// If there was a peer that had a pending want or it was a broadcast want
if peerCnts.wanted() {
// Decrement the total wants gauge
pwm.wantGauge.Dec()
}
delete(pwm.wantPeers, c)
}

// Remove cancelled broadcast wants
for _, c := range broadcastCancels {
pwm.broadcastWants.Remove(c)
}
if len(broadcastCancels) > 0 {
// If a broadcast want is being cancelled, send the cancel to all
// peers
for p, pws := range pwm.peerWants {
send(p, pws)
}

// Batch-remove the reverse-index. There's no need to clear this index
// peer-by-peer.
for _, c := range cancelKs {
delete(pwm.wantPeers, c)
// Remove cancelled broadcast wants
for _, c := range broadcastCancels {
pwm.broadcastWants.Remove(c)
}

for _, c := range cancelKs {
clearWantsForCID(c)
}
} else {
// Only send cancels to peers that received a corresponding want
for _, c := range cancelKs {
for p := range pwm.wantPeers[c] {
pws, ok := pwm.peerWants[p]
if !ok {
// Should never happen but check just in case
log.Errorf("sendCancels - peerWantManager index missing peer %s", p)
continue
}
send(p, pws)
}

clearWantsForCID(c)
}
}
}

Expand Down

0 comments on commit 3c70cf3

Please sign in to comment.