Skip to content

Commit

Permalink
fix: performance improvements
Browse files Browse the repository at this point in the history
- Fix exhausted wants problem resulting in possible performance issue
- Minor improvements for GC.
  • Loading branch information
gammazero committed Oct 17, 2024
1 parent c2487a2 commit 40663fb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
19 changes: 13 additions & 6 deletions bitswap/client/internal/session/sessionwants.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,13 @@ func (sw *sessionWants) GetNextWants() []cid.Cid {
// limit)
currentLiveCount := len(sw.liveWants)
toAdd := sw.broadcastLimit - currentLiveCount
liveSize := min(toAdd, sw.toFetch.Len())
if liveSize == 0 {
return nil
}

Check warning on line 62 in bitswap/client/internal/session/sessionwants.go

View check run for this annotation

Codecov / codecov/patch

bitswap/client/internal/session/sessionwants.go#L61-L62

Added lines #L61 - L62 were not covered by tests

var live []cid.Cid
for ; toAdd > 0 && sw.toFetch.Len() > 0; toAdd-- {
live := make([]cid.Cid, 0, liveSize)
for ; toAdd != 0 && sw.toFetch.Len() != 0; toAdd-- {
c := sw.toFetch.Pop()
live = append(live, c)
sw.liveWantsOrder = append(sw.liveWantsOrder, c)
Expand Down Expand Up @@ -117,6 +121,7 @@ func (sw *sessionWants) BlocksReceived(ks []cid.Cid) ([]cid.Cid, time.Duration)
cleaned = append(cleaned, c)
}
}
clear(sw.liveWantsOrder[len(cleaned):]) // GC cleared items
sw.liveWantsOrder = cleaned
}

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

// LiveWants returns a list of live wants
func (sw *sessionWants) LiveWants() []cid.Cid {
live := make([]cid.Cid, 0, len(sw.liveWants))
live := make([]cid.Cid, len(sw.liveWants))
var i int
for c := range sw.liveWants {
live = append(live, c)
live[i] = c
i++
}

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

// Has live wants indicates if there are any live wants
func (sw *sessionWants) HasLiveWants() bool {
return len(sw.liveWants) > 0
return len(sw.liveWants) != 0
}

// Indicates whether the want is in either of the fetch or live queues
Expand Down
7 changes: 4 additions & 3 deletions bitswap/client/internal/session/sessionwantsender.go
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,7 @@ func (sws *sessionWantSender) trackWant(c cid.Cid) {
}

// Create the want info
wi := newWantInfo(sws.peerRspTrkr)
sws.wants[c] = wi
sws.wants[c] = newWantInfo(sws.peerRspTrkr)

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

// If the last available peer in the session has become unavailable
Expand Down

0 comments on commit 40663fb

Please sign in to comment.