Make piece states a slice and reuse it and request indexes between runs

This commit is contained in:
Matt Joiner 2022-05-11 14:15:33 +10:00
parent 4eb6a49f27
commit 590d1ac265
No known key found for this signature in database
GPG Key ID: 6B990B8185E7F782
2 changed files with 13 additions and 6 deletions

View File

@ -70,7 +70,7 @@ type (
type desiredPeerRequests struct {
requestIndexes []RequestIndex
peer *Peer
pieceStates map[pieceIndex]request_strategy.PieceRequestOrderState
pieceStates []request_strategy.PieceRequestOrderState
}
func (p *desiredPeerRequests) Len() int {
@ -95,8 +95,8 @@ func (p *desiredPeerRequests) lessByValue(leftRequest, rightRequest RequestIndex
!p.peer.peerAllowedFast.Contains(rightPieceIndex),
)
}
leftPiece := p.pieceStates[leftPieceIndex]
rightPiece := p.pieceStates[rightPieceIndex]
leftPiece := &p.pieceStates[leftPieceIndex]
rightPiece := &p.pieceStates[rightPieceIndex]
// Putting this first means we can steal requests from lesser-performing peers for our first few
// new requests.
priority := func() piecePriority {
@ -194,7 +194,8 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
input := t.getRequestStrategyInput()
requestHeap := desiredPeerRequests{
peer: p,
pieceStates: make(map[pieceIndex]request_strategy.PieceRequestOrderState),
pieceStates: t.requestPieceStates,
requestIndexes: t.requestIndexes,
}
request_strategy.GetRequestablePieces(
input,
@ -206,6 +207,7 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
if !p.peerHasPiece(pieceIndex) {
return
}
requestHeap.pieceStates[pieceIndex] = pieceExtra
allowedFast := p.peerAllowedFast.Contains(pieceIndex)
p.t.piece(pieceIndex).undirtiedChunksIter.Iter(func(ci request_strategy.ChunkIndex) {
r := p.t.pieceRequestIndexOffset(pieceIndex) + ci
@ -228,7 +230,6 @@ func (p *Peer) getDesiredRequestState() (desired desiredRequestState) {
return
}
requestHeap.requestIndexes = append(requestHeap.requestIndexes, r)
requestHeap.pieceStates[pieceIndex] = pieceExtra
})
},
)
@ -256,6 +257,7 @@ func (p *Peer) maybeUpdateActualRequestState() {
func(_ context.Context) {
next := p.getDesiredRequestState()
p.applyRequestState(next)
p.t.requestIndexes = next.Requests.requestIndexes[:0]
},
)
}

View File

@ -157,6 +157,10 @@ type Torrent struct {
sourcesLogger log.Logger
smartBanCache smartBanCache
// Large allocations reused between request state updates.
requestPieceStates []request_strategy.PieceRequestOrderState
requestIndexes []RequestIndex
}
func (t *Torrent) selectivePieceAvailabilityFromPeers(i pieceIndex) (count int) {
@ -457,6 +461,7 @@ func (t *Torrent) pieceRequestOrderKey(i int) request_strategy.PieceRequestOrder
func (t *Torrent) onSetInfo() {
t.initPieceRequestOrder()
MakeSliceWithLength(&t.requestState, t.numChunks())
MakeSliceWithLength(&t.requestPieceStates, t.numPieces())
for i := range t.pieces {
p := &t.pieces[i]
// Need to add relativeAvailability before updating piece completion, as that may result in conns