t *Torrent
}
-func (r requestStrategyTorrent) IgnorePiece(i int) bool {
- if r.t.ignorePieceForRequests(i) {
- return true
- }
- if r.t.pieceNumPendingChunks(i) == 0 {
- return true
- }
-
- return false
+func (r requestStrategyTorrent) Piece(i int) request_strategy.Piece {
+ return (*requestStrategyPiece)(r.t.piece(i))
}
func (r requestStrategyTorrent) PieceLength() int64 {
var _ request_strategy.Torrent = requestStrategyTorrent{}
-type requestStrategyPiece struct {
- t *Torrent
- i pieceIndex
-}
+type requestStrategyPiece Piece
-func (r requestStrategyPiece) Request() bool {
- return !r.t.ignorePieceForRequests(r.i)
+func (r *requestStrategyPiece) Request() bool {
+ return !r.t.ignorePieceForRequests(r.index)
}
-func (r requestStrategyPiece) NumPendingChunks() int {
- return int(r.t.pieceNumPendingChunks(r.i))
+func (r *requestStrategyPiece) NumPendingChunks() int {
+ return int(r.t.pieceNumPendingChunks(r.index))
}
-var _ request_strategy.Piece = requestStrategyPiece{}
+var _ request_strategy.Piece = (*requestStrategyPiece)(nil)
--- /dev/null
+package torrent
+
+import (
+ "io"
+ "runtime"
+ "testing"
+
+ "github.com/davecgh/go-spew/spew"
+ qt "github.com/frankban/quicktest"
+
+ request_strategy "github.com/anacrolix/torrent/request-strategy"
+)
+
+func makeRequestStrategyPiece(t request_strategy.Torrent) request_strategy.Piece {
+ return t.Piece(0)
+}
+
+func TestRequestStrategyPieceDoesntAlloc(t *testing.T) {
+ c := qt.New(t)
+ akshalTorrent := &Torrent{pieces: make([]Piece, 1)}
+ rst := requestStrategyTorrent{akshalTorrent}
+ var before, after runtime.MemStats
+ runtime.ReadMemStats(&before)
+ p := makeRequestStrategyPiece(rst)
+ runtime.ReadMemStats(&after)
+ c.Assert(before.HeapAlloc, qt.Equals, after.HeapAlloc)
+ // We have to use p, or it gets optimized away.
+ spew.Fdump(io.Discard, p)
+}
lastItem.Set(_i)
ih := _i.key.InfoHash
- t := input.Torrent(ih)
+ var t Torrent = input.Torrent(ih)
+ var piece Piece = t.Piece(_i.key.Index)
pieceLength := t.PieceLength()
if storageLeft != nil {
if *storageLeft < pieceLength {
}
*storageLeft -= pieceLength
}
- if t.IgnorePiece(_i.key.Index) {
+ if !piece.Request() || piece.NumPendingChunks() == 0 {
// TODO: Clarify exactly what is verified. Stuff that's being hashed should be
// considered unverified and hold up further requests.
return true
package requestStrategy
type Torrent interface {
- IgnorePiece(int) bool
+ Piece(int) Piece
PieceLength() int64
}