Optimize chunk calculations in request strategy

This commit is contained in:
Matt Joiner 2021-10-10 11:32:27 +11:00
parent ced0f543d6
commit 7d0be0ac65
5 changed files with 14 additions and 11 deletions

View File

@ -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()
}

View File

@ -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 {

View File

@ -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
}

View File

@ -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 {

View File

@ -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()
}