From 7d0be0ac6529be0f9a71b537d0f258898f4c3aba Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Sun, 10 Oct 2021 11:32:27 +1100 Subject: [PATCH] Optimize chunk calculations in request strategy --- pending-requests.go | 2 +- request-strategy/order.go | 2 +- request-strategy/torrent.go | 2 +- requesting.go | 6 +++--- torrent.go | 13 ++++++++----- 5 files changed, 14 insertions(+), 11 deletions(-) diff --git a/pending-requests.go b/pending-requests.go index 9e2d1200..dcb1faf1 100644 --- a/pending-requests.go +++ b/pending-requests.go @@ -24,7 +24,7 @@ func (p *pendingRequests) Inc(r RequestIndex) { p.m.SetValue(_r, prev+1) } -func (p *pendingRequests) Init() { +func (p *pendingRequests) Init(maxIndex RequestIndex) { p.m = roaring.NewDefaultBSI() } diff --git a/request-strategy/order.go b/request-strategy/order.go index 82cb5048..7456649b 100644 --- a/request-strategy/order.go +++ b/request-strategy/order.go @@ -88,7 +88,7 @@ type requestablePiece struct { } func (p *requestablePiece) chunkIndexToRequestIndex(c ChunkIndex) RequestIndex { - return RequestIndex(p.t.ChunksPerPiece*p.index) + RequestIndex(c) + return p.t.ChunksPerPiece*uint32(p.index) + c } type filterPiece struct { diff --git a/request-strategy/torrent.go b/request-strategy/torrent.go index 262ae965..ff0261a3 100644 --- a/request-strategy/torrent.go +++ b/request-strategy/torrent.go @@ -12,7 +12,7 @@ type Torrent struct { Peers []Peer // Some value that's unique and stable between runs. Could even use the infohash? InfoHash metainfo.Hash - ChunksPerPiece int + ChunksPerPiece uint32 MaxUnverifiedBytes int64 } diff --git a/requesting.go b/requesting.go index f7b4f7c4..c5cd459e 100644 --- a/requesting.go +++ b/requesting.go @@ -32,7 +32,7 @@ func (cl *Client) getRequestStrategyInput() request_strategy.Input { } rst := request_strategy.Torrent{ InfoHash: t.infoHash, - ChunksPerPiece: (t.usualPieceSize() + int(t.chunkSize) - 1) / int(t.chunkSize), + ChunksPerPiece: t.chunksPerRegularPiece(), } if t.storage != nil { rst.Capacity = t.storage.Capacity @@ -135,8 +135,8 @@ func (p peerRequests) Less(i, j int) bool { leftRequest := p.requestIndexes[i] rightRequest := p.requestIndexes[j] t := p.peer.t - leftPieceIndex := leftRequest / t.chunksPerRegularPiece() - rightPieceIndex := rightRequest / t.chunksPerRegularPiece() + leftPieceIndex := leftRequest / p.torrentStrategyInput.ChunksPerPiece + rightPieceIndex := rightRequest / p.torrentStrategyInput.ChunksPerPiece leftCurrent := p.peer.actualRequestState.Requests.Contains(leftRequest) rightCurrent := p.peer.actualRequestState.Requests.Contains(rightRequest) pending := func(index RequestIndex, current bool) int { diff --git a/torrent.go b/torrent.go index 33376f6c..e099a10d 100644 --- a/torrent.go +++ b/torrent.go @@ -445,7 +445,7 @@ func (t *Torrent) onSetInfo() { t.cl.event.Broadcast() close(t.gotMetainfoC) t.updateWantPeersEvent() - t.pendingRequests.Init() + t.pendingRequests.Init(t.numRequests()) t.tryCreateMorePieceHashers() t.iterPeers(func(p *Peer) { p.onGotInfo(t.info) @@ -861,6 +861,13 @@ func (t *Torrent) chunksPerRegularPiece() uint32 { return uint32((pp.Integer(t.usualPieceSize()) + t.chunkSize - 1) / t.chunkSize) } +func (t *Torrent) numRequests() RequestIndex { + if t.numPieces() == 0 { + return 0 + } + return uint32(t.numPieces()-1)*t.chunksPerRegularPiece() + t.pieceNumChunks(t.numPieces()-1) +} + func (t *Torrent) pendAllChunkSpecs(pieceIndex pieceIndex) { t.dirtyChunks.RemoveRange( uint64(t.pieceRequestIndexOffset(pieceIndex)), @@ -2283,10 +2290,6 @@ func (t *Torrent) requestIndexFromRequest(r Request) RequestIndex { return t.pieceRequestIndexOffset(pieceIndex(r.Index)) + uint32(r.Begin/t.chunkSize) } -func (t *Torrent) numChunks() RequestIndex { - return RequestIndex((t.Length() + int64(t.chunkSize) - 1) / int64(t.chunkSize)) -} - func (t *Torrent) pieceRequestIndexOffset(piece pieceIndex) RequestIndex { return RequestIndex(piece) * t.chunksPerRegularPiece() }