From 610f8e01858b330801ea75e5202e5193252638d1 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Mon, 11 Oct 2021 16:21:26 +1100 Subject: [PATCH] Don't put requests we can't make into the request heap In particular this should save a lot of overhead if we're choked. --- requesting.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/requesting.go b/requesting.go index c5cd459e..737771fe 100644 --- a/requesting.go +++ b/requesting.go @@ -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 } -- 2.48.1