From e4dec03a329e3ef2fde131d421aae376cada776d Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Wed, 4 Mar 2015 13:07:11 +1100 Subject: [PATCH] Change the way readahead pieces are calculated --- client.go | 6 +++++- client_test.go | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 78269d61..9a427faf 100644 --- a/client.go +++ b/client.go @@ -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 diff --git a/client_test.go b/client_test.go index 5dc8a4d3..48eddfec 100644 --- a/client_test.go +++ b/client_test.go @@ -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_) + } + } +}