Change the way readahead pieces are calculated
This commit is contained in:
parent
779f4d3b97
commit
e4dec03a32
|
@ -305,6 +305,10 @@ func dataReadAt(d Data, b []byte, off int64) (n int, err error) {
|
|||
panic(fmt.Sprintf("can't read from %T", d))
|
||||
}
|
||||
|
||||
func readaheadPieces(readahead, pieceLength int64) int {
|
||||
return int((readahead+pieceLength-1)/pieceLength - 1)
|
||||
}
|
||||
|
||||
func (cl *Client) readRaisePiecePriorities(t *torrent, off int64) {
|
||||
index := int(off / int64(t.usualPieceSize()))
|
||||
cl.raisePiecePriority(t, index, piecePriorityNow)
|
||||
|
@ -313,7 +317,7 @@ func (cl *Client) readRaisePiecePriorities(t *torrent, off int64) {
|
|||
return
|
||||
}
|
||||
cl.raisePiecePriority(t, index, piecePriorityNext)
|
||||
for i := 0; i < t.numConnsUnchoked()/2; i++ {
|
||||
for i := 0; i < readaheadPieces(5*1024*1024, t.Info.PieceLength); i++ {
|
||||
index++
|
||||
if index >= t.numPieces() {
|
||||
break
|
||||
|
|
|
@ -275,3 +275,21 @@ func TestClientTransfer(t *testing.T) {
|
|||
t.Fatal(":(")
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadaheadPieces(t *testing.T) {
|
||||
for _, case_ := range []struct {
|
||||
readaheadBytes, pieceLength int64
|
||||
readaheadPieces int
|
||||
}{
|
||||
{5 * 1024 * 1024, 256 * 1024, 19},
|
||||
{5 * 1024 * 1024, 5 * 1024 * 1024, 0},
|
||||
{5*1024*1024 - 1, 5 * 1024 * 1024, 0},
|
||||
{5 * 1024 * 1024, 5*1024*1024 - 1, 1},
|
||||
{0, 5 * 1024 * 1024, -1},
|
||||
{5 * 1024 * 1024, 1048576, 4},
|
||||
} {
|
||||
if readaheadPieces(case_.readaheadBytes, case_.pieceLength) != case_.readaheadPieces {
|
||||
t.Fatalf("case failed: %s", case_)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue