Skip to content

Commit 40663fb

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

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
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: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,8 +349,7 @@ func (sws *sessionWantSender) trackWant(c cid.Cid) {
349349
}
350350

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

355354
// For each available peer, register any information we know about
356355
// whether the peer has the block
@@ -482,8 +481,10 @@ func (sws *sessionWantSender) checkForExhaustedWants(dontHaves []cid.Cid, newlyU
482481
if len(newlyUnavailable) > 0 {
483482
// Collect all pending wants
484483
wants = make([]cid.Cid, len(sws.wants))
484+
var i int
485485
for c := range sws.wants {
486-
wants = append(wants, c)
486+
wants[i] = c
487+
i++
487488
}
488489

489490
// If the last available peer in the session has become unavailable

0 commit comments

Comments
 (0)