]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Don't put requests we can't make into the request heap
authorMatt Joiner <anacrolix@gmail.com>
Mon, 11 Oct 2021 05:21:26 +0000 (16:21 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 19 Oct 2021 03:08:56 +0000 (14:08 +1100)
In particular this should save a lot of overhead if we're choked.

requesting.go

index c5cd459e50669250cf01f4613c6e9fba1d1a3b5f..737771fec97a64ee0dda497b832c0a05c579ada0 100644 (file)
@@ -180,8 +180,7 @@ func (p *peerRequests) Pop() interface{} {
 func (p *Peer) getDesiredRequestState() (desired requestState) {
        input := p.t.cl.getRequestStrategyInput()
        requestHeap := peerRequests{
-               requestIndexes: nil,
-               peer:           p,
+               peer: p,
        }
        for _, t := range input.Torrents {
                if t.InfoHash == p.t.infoHash {
@@ -198,6 +197,15 @@ func (p *Peer) getDesiredRequestState() (desired requestState) {
                        if !p.peerHasPiece(pieceIndex) {
                                return
                        }
+                       allowedFast := p.peerAllowedFast.ContainsInt(pieceIndex)
+                       if !allowedFast {
+                               // We must signal interest to request this piece.
+                               desired.Interested = true
+                               if p.peerChoking {
+                                       // We can't request from this piece right now then.
+                                       return
+                               }
+                       }
                        rsp.IterPendingChunks.Iter(func(ci request_strategy.ChunkIndex) {
                                requestHeap.requestIndexes = append(
                                        requestHeap.requestIndexes,
@@ -208,14 +216,7 @@ func (p *Peer) getDesiredRequestState() (desired requestState) {
        heap.Init(&requestHeap)
        for requestHeap.Len() != 0 && desired.Requests.GetCardinality() < uint64(p.nominalMaxRequests()) {
                requestIndex := heap.Pop(&requestHeap).(RequestIndex)
-               pieceIndex := requestIndex / p.t.chunksPerRegularPiece()
-               allowedFast := p.peerAllowedFast.Contains(pieceIndex)
-               if !allowedFast {
-                       desired.Interested = true
-               }
-               if allowedFast || !p.peerChoking {
-                       desired.Requests.Add(requestIndex)
-               }
+               desired.Requests.Add(requestIndex)
        }
        return
 }