From 6d6197b0a1b8593e8b64c25c827a8b5ca9ce20d3 Mon Sep 17 00:00:00 2001 From: Matt Joiner Date: Tue, 17 Jul 2018 21:28:01 +1000 Subject: [PATCH] Switch pieceIndex back to an int 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 | 24 ++++++++++++------------ file.go | 9 ++++----- misc.go | 4 ++-- reader.go | 2 +- torrent.go | 6 +++--- 5 files changed, 22 insertions(+), 23 deletions(-) diff --git a/connection.go b/connection.go index 56a0ee5b..0393497c 100644 --- a/connection.go +++ b/connection.go @@ -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 5fd1babb..6c728d15 100644 --- 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 3ca4d77b..5f7f66be 100644 --- 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 ) diff --git a/reader.go b/reader.go index 38d4406c..d65a18f5 100644 --- 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. diff --git a/torrent.go b/torrent.go index 6c63a3c4..07773739 100644 --- a/torrent.go +++ b/torrent.go @@ -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) {