@@ -56,9 +56,13 @@ func (sw *sessionWants) GetNextWants() []cid.Cid {
56
56
// limit)
57
57
currentLiveCount := len (sw .liveWants )
58
58
toAdd := sw .broadcastLimit - currentLiveCount
59
+ liveSize := min (toAdd , sw .toFetch .Len ())
60
+ if liveSize == 0 {
61
+ return nil
62
+ }
59
63
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 -- {
62
66
c := sw .toFetch .Pop ()
63
67
live = append (live , c )
64
68
sw .liveWantsOrder = append (sw .liveWantsOrder , c )
@@ -117,6 +121,7 @@ func (sw *sessionWants) BlocksReceived(ks []cid.Cid) ([]cid.Cid, time.Duration)
117
121
cleaned = append (cleaned , c )
118
122
}
119
123
}
124
+ clear (sw .liveWantsOrder [len (cleaned ):]) // GC cleared items
120
125
sw .liveWantsOrder = cleaned
121
126
}
122
127
@@ -127,7 +132,7 @@ func (sw *sessionWants) BlocksReceived(ks []cid.Cid) ([]cid.Cid, time.Duration)
127
132
// live want CIDs up to the broadcast limit.
128
133
func (sw * sessionWants ) PrepareBroadcast () []cid.Cid {
129
134
now := time .Now ()
130
- live := make ([]cid.Cid , 0 , len (sw .liveWants ))
135
+ live := make ([]cid.Cid , 0 , min ( len (sw .liveWants ), sw . broadcastLimit ))
131
136
for _ , c := range sw .liveWantsOrder {
132
137
if _ , ok := sw .liveWants [c ]; ok {
133
138
// No response was received for the want, so reset the sent time
@@ -153,9 +158,11 @@ func (sw *sessionWants) CancelPending(keys []cid.Cid) {
153
158
154
159
// LiveWants returns a list of live wants
155
160
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
157
163
for c := range sw .liveWants {
158
- live = append (live , c )
164
+ live [i ] = c
165
+ i ++
159
166
}
160
167
161
168
return live
@@ -180,7 +187,7 @@ func (sw *sessionWants) RandomLiveWant() cid.Cid {
180
187
181
188
// Has live wants indicates if there are any live wants
182
189
func (sw * sessionWants ) HasLiveWants () bool {
183
- return len (sw .liveWants ) > 0
190
+ return len (sw .liveWants ) != 0
184
191
}
185
192
186
193
// Indicates whether the want is in either of the fetch or live queues
0 commit comments