]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Switch pieceIndex back to an int
authorMatt Joiner <anacrolix@gmail.com>
Tue, 17 Jul 2018 11:28:01 +0000 (21:28 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 17 Jul 2018 11:28:01 +0000 (21:28 +1000)
I suspect that interface conversions using packet iter are causing a lot of allocation. Either way, with the casting this adds, we should be able to change pieceIndex's type alias now with minimal code change.

connection.go
file.go
misc.go
reader.go
torrent.go

index 56a0ee5b9c72bbbb0c91b56bf4eb10426ee7f7ce..0393497c5cec07d4523fde04c9aa741a9f1a85b6 100644 (file)
@@ -486,7 +486,7 @@ func (cn *connection) request(r request, mw messageWriter) bool {
        if _, ok := cn.requests[r]; ok {
                panic("chunk already requested")
        }
-       if !cn.PeerHasPiece(r.Index) {
+       if !cn.PeerHasPiece(pieceIndex(r.Index)) {
                panic("requesting piece peer doesn't have")
        }
        if _, ok := cn.t.conns[cn]; !ok {
@@ -803,7 +803,7 @@ func iterUndirtiedChunks(piece pieceIndex, t *Torrent, f func(chunkSpec) bool) b
        chunkIndices := t.pieces[piece].undirtiedChunkIndices().ToSortedSlice()
        // TODO: Use "math/rand".Shuffle >= Go 1.10
        return iter.ForPerm(len(chunkIndices), func(i int) bool {
-               return f(t.chunkIndexSpec(pieceIndex(chunkIndices[i]), piece))
+               return f(t.chunkIndexSpec(pp.Integer(chunkIndices[i]), piece))
        })
 }
 
@@ -1024,7 +1024,7 @@ func (c *connection) reject(r request) {
 
 func (c *connection) onReadRequest(r request) error {
        requestedChunkLengths.Add(strconv.FormatUint(r.Length.Uint64(), 10), 1)
-       if r.Begin+r.Length > c.t.pieceLength(r.Index) {
+       if r.Begin+r.Length > c.t.pieceLength(pieceIndex(r.Index)) {
                torrent.Add("bad requests received", 1)
                return errors.New("bad request")
        }
@@ -1048,7 +1048,7 @@ func (c *connection) onReadRequest(r request) error {
                // BEP 6 says we may close here if we choose.
                return nil
        }
-       if !c.t.havePiece(r.Index) {
+       if !c.t.havePiece(pieceIndex(r.Index)) {
                // This isn't necessarily them screwing up. We can drop pieces
                // from our storage, and can't communicate this to peers
                // except by reconnecting.
@@ -1127,7 +1127,7 @@ func (c *connection) mainReadLoop() (err error) {
                        // We'll probably choke them for this, which will clear them if
                        // appropriate, and is clearly specified.
                case pp.Have:
-                       err = c.peerSentHave(msg.Index)
+                       err = c.peerSentHave(pieceIndex(msg.Index))
                case pp.Request:
                        r := newRequestFromMessage(&msg)
                        err = c.onReadRequest(r)
@@ -1340,21 +1340,21 @@ func (c *connection) receiveChunk(msg *pp.Message) error {
        if err != nil {
                log.Printf("%s (%s): error writing chunk %v: %s", t, t.infoHash, req, err)
                t.pendRequest(req)
-               t.updatePieceCompletion(msg.Index)
+               t.updatePieceCompletion(pieceIndex(msg.Index))
                return nil
        }
 
        // It's important that the piece is potentially queued before we check if
        // the piece is still wanted, because if it is queued, it won't be wanted.
-       if t.pieceAllDirty(req.Index) {
-               t.queuePieceCheck(req.Index)
-               t.pendAllChunkSpecs(req.Index)
+       if t.pieceAllDirty(pieceIndex(req.Index)) {
+               t.queuePieceCheck(pieceIndex(req.Index))
+               t.pendAllChunkSpecs(pieceIndex(req.Index))
        }
 
-       c.onDirtiedPiece(req.Index)
+       c.onDirtiedPiece(pieceIndex(req.Index))
 
        cl.event.Broadcast()
-       t.publishPieceChange(req.Index)
+       t.publishPieceChange(pieceIndex(req.Index))
 
        return nil
 }
@@ -1420,7 +1420,7 @@ another:
                        }
                        more, err := c.sendChunk(r, msg)
                        if err != nil {
-                               i := r.Index
+                               i := pieceIndex(r.Index)
                                if c.t.pieceComplete(i) {
                                        c.t.updatePieceCompletion(i)
                                        if !c.t.pieceComplete(i) {
diff --git a/file.go b/file.go
index 5fd1babb00fdf9fee24c83991fbea84b2f7dc852..6c728d15e3d5de5662a431091993cf34b9f4e61a 100644 (file)
--- a/file.go
+++ b/file.go
@@ -4,7 +4,6 @@ import (
        "strings"
 
        "github.com/anacrolix/torrent/metainfo"
-       pwp "github.com/anacrolix/torrent/peer_protocol"
 )
 
 // Provides access to regions of torrent data that correspond to its files.
@@ -131,16 +130,16 @@ func (f *File) Priority() piecePriority {
        return f.prio
 }
 
-func (f *File) firstPieceIndex() pwp.Integer {
+func (f *File) firstPieceIndex() pieceIndex {
        if f.t.usualPieceSize() == 0 {
                return 0
        }
-       return pwp.Integer(f.offset / int64(f.t.usualPieceSize()))
+       return pieceIndex(f.offset / int64(f.t.usualPieceSize()))
 }
 
-func (f *File) endPieceIndex() pwp.Integer {
+func (f *File) endPieceIndex() pieceIndex {
        if f.t.usualPieceSize() == 0 {
                return 0
        }
-       return pwp.Integer((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
+       return pieceIndex((f.offset+f.length-1)/int64(f.t.usualPieceSize())) + 1
 }
diff --git a/misc.go b/misc.go
index 3ca4d77b9034ec580395395ae72f033ef631d20a..5f7f66bef278e82479147e8b0fb62ee5cce71a0d 100644 (file)
--- a/misc.go
+++ b/misc.go
@@ -98,7 +98,7 @@ func validateInfo(info *metainfo.Info) error {
        return nil
 }
 
-func chunkIndexSpec(index pieceIndex, pieceLength, chunkSize pp.Integer) chunkSpec {
+func chunkIndexSpec(index pp.Integer, pieceLength, chunkSize pp.Integer) chunkSpec {
        ret := chunkSpec{pp.Integer(index) * chunkSize, chunkSize}
        if ret.Begin+ret.Length > pieceLength {
                ret.Length = pieceLength - ret.Begin
@@ -154,6 +154,6 @@ func min(as ...int64) int64 {
 var unlimited = rate.NewLimiter(rate.Inf, 0)
 
 type (
-       pieceIndex = pp.Integer
+       pieceIndex = int
        InfoHash   = metainfo.Hash
 )
index 38d4406cd71281d9f2dfe74a5c355631da040528..d65a18f5cd0cbdf331b1cf0cc4543787770b6348 100644 (file)
--- a/reader.go
+++ b/reader.go
@@ -85,7 +85,7 @@ func (r *reader) readable(off int64) (ret bool) {
        if r.responsive {
                return r.t.haveChunk(req)
        }
-       return r.t.pieceComplete(req.Index)
+       return r.t.pieceComplete(pieceIndex(req.Index))
 }
 
 // How many bytes are available to read. Max is the most we could require.
index 6c63a3c43a3a7c2b9a6f8e2861daed8259e16006..0777373985f49cc726da3f86ccdb0f6cfa0863c0 100644 (file)
@@ -33,7 +33,7 @@ import (
        "github.com/anacrolix/torrent/tracker"
 )
 
-func (t *Torrent) chunkIndexSpec(chunkIndex, piece pieceIndex) chunkSpec {
+func (t *Torrent) chunkIndexSpec(chunkIndex pp.Integer, piece pieceIndex) chunkSpec {
        return chunkIndexSpec(chunkIndex, t.pieceLength(piece), t.chunkSize)
 }
 
@@ -795,7 +795,7 @@ func (t *Torrent) haveChunk(r request) (ret bool) {
        if !t.haveInfo() {
                return false
        }
-       if t.pieceComplete(r.Index) {
+       if t.pieceComplete(pieceIndex(r.Index)) {
                return true
        }
        p := &t.pieces[r.Index]
@@ -807,7 +807,7 @@ func chunkIndex(cs chunkSpec, chunkSize pp.Integer) int {
 }
 
 func (t *Torrent) wantPiece(r request) bool {
-       if !t.wantPieceIndex(r.Index) {
+       if !t.wantPieceIndex(pieceIndex(r.Index)) {
                return false
        }
        if t.pieces[r.Index].pendingChunk(r.chunkSpec, t.chunkSize) {