]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Optimize chunk calculations in request strategy
authorMatt Joiner <anacrolix@gmail.com>
Sun, 10 Oct 2021 00:32:27 +0000 (11:32 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 19 Oct 2021 03:08:56 +0000 (14:08 +1100)
pending-requests.go
request-strategy/order.go
request-strategy/torrent.go
requesting.go
torrent.go

index 9e2d120095e855277c4769816e71519a239ad22b..dcb1faf19fb95b206aff8862b276a08b87213a7a 100644 (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()
 }
 
index 82cb504872d69596e65529c7edc7541f3c815fc6..7456649bb6c42da042f6250ff2f23a3a411fcd26 100644 (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 {
index 262ae9656682cc9f04bc14aa3c1dd0666aeddf37..ff0261a3eb6944a3c99f6fee98f74cb23a54fee0 100644 (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
 }
index f7b4f7c4e3ff4c3af91e7a38d371c186008141a5..c5cd459e50669250cf01f4613c6e9fba1d1a3b5f 100644 (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 {
index 33376f6c0bf2c880eb213a33b11e697d003f19f7..e099a10dce58e4cad9af01cb02ae404c24d62763 100644 (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()
 }