From: Matt Joiner Date: Sat, 6 Feb 2016 14:20:40 +0000 (+1100) Subject: Speed up torrent.needData, and use new missinggo iterator interface X-Git-Tag: v1.0.0~915 X-Git-Url: http://www.git.stargrave.org/?a=commitdiff_plain;h=2e3bc1090bec7c0c2b2825184f833c909e098afb;p=btrtrc.git Speed up torrent.needData, and use new missinggo iterator interface --- diff --git a/client.go b/client.go index 34921bd7..149fc71a 100644 --- a/client.go +++ b/client.go @@ -24,6 +24,7 @@ import ( "github.com/anacrolix/missinggo" . "github.com/anacrolix/missinggo" + "github.com/anacrolix/missinggo/bitmap" "github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/sync" "github.com/anacrolix/utp" @@ -1666,23 +1667,23 @@ func (me *Client) addConnection(t *torrent, c *connection) bool { return true } +func (t *torrent) readerPieces() (ret bitmap.Bitmap) { + t.forReaderOffsetPieces(func(begin, end int) bool { + ret.AddRange(begin, end) + return true + }) + return +} + func (t *torrent) needData() bool { if !t.haveInfo() { return true } - for i := t.pendingPieces.IterTyped(); i.Next(); { - if t.wantPiece(i.ValueInt()) { - i.Stop() - return true - } - } - return !t.forReaderOffsetPieces(func(begin, end int) (again bool) { - for i := begin; i < end; i++ { - if !t.pieceComplete(i) { - return false - } - } + if t.pendingPieces.Len() != 0 { return true + } + return !t.readerPieces().IterTyped(func(piece int) bool { + return t.pieceComplete(piece) }) }