Skip to content

Commit ac4c801

Browse files
committed
fix: performance improvements
- Fix exhausted wants problem resulting in possible performance issue - Minor improvements for GC.
1 parent c2487a2 commit ac4c801

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

bitswap/client/internal/session/sessionwants.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,13 @@ func (sw *sessionWants) GetNextWants() []cid.Cid {
5656
// limit)
5757
currentLiveCount := len(sw.liveWants)
5858
toAdd := sw.broadcastLimit - currentLiveCount
59+
liveSize := min(toAdd, sw.toFetch.Len())
60+
if liveSize == 0 {
61+
return nil
62+
}
5963

60-
var live []cid.Cid
61-
for ; toAdd > 0 && sw.toFetch.Len() > 0; toAdd-- {
64+
live := make([]cid.Cid, 0, liveSize)
65+
for ; toAdd != 0 && sw.toFetch.Len() != 0; toAdd-- {
6266
c := sw.toFetch.Pop()
6367
live = append(live, c)
6468
sw.liveWantsOrder = append(sw.liveWantsOrder, c)
@@ -117,6 +121,7 @@ func (sw *sessionWants) BlocksReceived(ks []cid.Cid) ([]cid.Cid, time.Duration)
117121
cleaned = append(cleaned, c)
118122
}
119123
}
124+
clear(sw.liveWantsOrder[len(cleaned):]) // GC cleared items
120125
sw.liveWantsOrder = cleaned
121126
}
122127

@@ -127,7 +132,7 @@ func (sw *sessionWants) BlocksReceived(ks []cid.Cid) ([]cid.Cid, time.Duration)
127132
// live want CIDs up to the broadcast limit.
128133
func (sw *sessionWants) PrepareBroadcast() []cid.Cid {
129134
now := time.Now()
130-
live := make([]cid.Cid, 0, len(sw.liveWants))
135+
live := make([]cid.Cid, 0, min(len(sw.liveWants), sw.broadcastLimit))
131136
for _, c := range sw.liveWantsOrder {
132137
if _, ok := sw.liveWants[c]; ok {
133138
// No response was received for the want, so reset the sent time
@@ -153,9 +158,11 @@ func (sw *sessionWants) CancelPending(keys []cid.Cid) {
153158

154159
// LiveWants returns a list of live wants
155160
func (sw *sessionWants) LiveWants() []cid.Cid {
156-
live := make([]cid.Cid, 0, len(sw.liveWants))
161+
live := make([]cid.Cid, len(sw.liveWants))
162+
var i int
157163
for c := range sw.liveWants {
158-
live = append(live, c)
164+
live[i] = c
165+
i++
159166
}
160167

161168
return live
@@ -180,7 +187,7 @@ func (sw *sessionWants) RandomLiveWant() cid.Cid {
180187

181188
// Has live wants indicates if there are any live wants
182189
func (sw *sessionWants) HasLiveWants() bool {
183-
return len(sw.liveWants) > 0
190+
return len(sw.liveWants) != 0
184191
}
185192

186193
// Indicates whether the want is in either of the fetch or live queues

bitswap/client/internal/session/sessionwantsender.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,7 @@ func (sws *sessionWantSender) Cancel(ks []cid.Cid) {
161161
// Update is called when the session receives a message with incoming blocks
162162
// or HAVE / DONT_HAVE
163163
func (sws *sessionWantSender) Update(from peer.ID, ks []cid.Cid, haves []cid.Cid, dontHaves []cid.Cid) {
164-
hasUpdate := len(ks) > 0 || len(haves) > 0 || len(dontHaves) > 0
165-
if !hasUpdate {
164+
if len(ks) == 0 && len(haves) == 0 && len(dontHaves) == 0 {
166165
return
167166
}
168167

@@ -270,7 +269,7 @@ func (sws *sessionWantSender) onChange(changes []change) {
270269
if chng.update.from != "" {
271270
// If the update includes blocks or haves, treat it as signaling that
272271
// the peer is available
273-
if len(chng.update.ks) > 0 || len(chng.update.haves) > 0 {
272+
if len(chng.update.ks) != 0 || len(chng.update.haves) != 0 {
274273
p := chng.update.from
275274
availability[p] = true
276275

@@ -296,7 +295,7 @@ func (sws *sessionWantSender) onChange(changes []change) {
296295
sws.checkForExhaustedWants(dontHaves, newlyUnavailable)
297296

298297
// If there are any cancels, send them
299-
if len(cancels) > 0 {
298+
if len(cancels) != 0 {
300299
sws.canceller.CancelSessionWants(sws.sessionID, cancels)
301300
}
302301

@@ -349,8 +348,7 @@ func (sws *sessionWantSender) trackWant(c cid.Cid) {
349348
}
350349

351350
// Create the want info
352-
wi := newWantInfo(sws.peerRspTrkr)
353-
sws.wants[c] = wi
351+
sws.wants[c] = newWantInfo(sws.peerRspTrkr)
354352

355353
// For each available peer, register any information we know about
356354
// whether the peer has the block
@@ -451,7 +449,7 @@ func (sws *sessionWantSender) processUpdates(updates []update) []cid.Cid {
451449
}
452450
}
453451
}
454-
if len(prunePeers) > 0 {
452+
if len(prunePeers) != 0 {
455453
go func() {
456454
for p := range prunePeers {
457455
// Peer doesn't have anything we want, so remove it
@@ -479,11 +477,13 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU
479477

480478
// If a peer just became unavailable, then we need to check all wants
481479
// (because it may be the last peer who hadn't sent a DONT_HAVE for a CID)
482-
if len(newlyUnavailable) > 0 {
480+
if len(newlyUnavailable) != 0 {
483481
// Collect all pending wants
484482
wants = make([]cid.Cid, len(sws.wants))
483+
var i int
485484
for c := range sws.wants {
486-
wants = append(wants, c)
485+
wants[i] = c
486+
i++
487487
}
488488

489489
// If the last available peer in the session has become unavailable
@@ -496,7 +496,7 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU
496496

497497
// If all available peers for a cid sent a DONT_HAVE, signal to the session
498498
// that we've exhausted available peers
499-
if len(wants) > 0 {
499+
if len(wants) != 0 {
500500
exhausted := sws.bpm.AllPeersDoNotHaveBlock(sws.spm.Peers(), wants)
501501
sws.processExhaustedWants(exhausted)
502502
}
@@ -506,7 +506,7 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU
506506
// already been marked as exhausted are passed to onPeersExhausted()
507507
func (sws *sessionWantSender) processExhaustedWants(exhausted []cid.Cid) {
508508
newlyExhausted := sws.newlyExhausted(exhausted)
509-
if len(newlyExhausted) > 0 {
509+
if len(newlyExhausted) != 0 {
510510
sws.onPeersExhausted(newlyExhausted)
511511
}
512512
}

0 commit comments

Comments
 (0)