]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Change the way readahead pieces are calculated
authorMatt Joiner <anacrolix@gmail.com>
Wed, 4 Mar 2015 02:07:11 +0000 (13:07 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Wed, 4 Mar 2015 02:07:11 +0000 (13:07 +1100)
client.go
client_test.go

index 78269d6196cd329b5fa459067b7f48ae558e0318..9a427faf68e9f5bff6974dae9e71c81d068dbe46 100644 (file)
--- 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
index 5dc8a4d3292b2b9cfb0200cbef4b434c363b3993..48eddfec289859fbf3dd2f366752c66d12589865 100644 (file)
@@ -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_)
+               }
+       }
+}