Abstract the pendingPieces as a bitmap

This commit is contained in:
Matt Joiner 2016-02-01 01:46:28 +11:00
parent 9824041e5a
commit b90dacd324
3 changed files with 14 additions and 12 deletions

View File

@ -1675,8 +1675,8 @@ func (t *torrent) needData() bool {
if !t.haveInfo() { if !t.haveInfo() {
return true return true
} }
for i := range t.pendingPieces { for i := t.pendingPieces.Iter(); i.Next(); {
if t.wantPiece(i) { if t.wantPiece(i.Value()) {
return true return true
} }
} }
@ -2476,7 +2476,7 @@ func (me *Client) pieceHashed(t *torrent, piece int, correct bool) {
} }
func (me *Client) onCompletedPiece(t *torrent, piece int) { func (me *Client) onCompletedPiece(t *torrent, piece int) {
delete(t.pendingPieces, piece) t.pendingPieces.Remove(piece)
for _, conn := range t.Conns { for _, conn := range t.Conns {
conn.Have(piece) conn.Have(piece)
for r := range conn.Requests { for r := range conn.Requests {

View File

@ -572,7 +572,8 @@ func (c *connection) fillRequests() {
} }
return true return true
}) })
for i := range c.t.pendingPieces { for it := c.t.pendingPieces.Iter(); it.Next(); {
i := it.Value()
if !c.t.wantPiece(i) { if !c.t.wantPiece(i) {
continue continue
} }

View File

@ -12,6 +12,7 @@ import (
"time" "time"
"github.com/anacrolix/missinggo" "github.com/anacrolix/missinggo"
"github.com/anacrolix/missinggo/bitmap"
"github.com/anacrolix/missinggo/perf" "github.com/anacrolix/missinggo/perf"
"github.com/anacrolix/missinggo/pubsub" "github.com/anacrolix/missinggo/pubsub"
"github.com/bradfitz/iter" "github.com/bradfitz/iter"
@ -98,7 +99,7 @@ type torrent struct {
readers map[*Reader]struct{} readers map[*Reader]struct{}
pendingPieces map[int]struct{} pendingPieces *bitmap.Bitmap
} }
var ( var (
@ -737,7 +738,7 @@ func (t *torrent) wantPiece(index int) bool {
if t.pieceComplete(index) { if t.pieceComplete(index) {
return false return false
} }
if _, ok := t.pendingPieces[index]; ok { if t.pendingPieces.Contains(index) {
return true return true
} }
return !t.forReaderOffsetPieces(func(begin, end int) bool { return !t.forReaderOffsetPieces(func(begin, end int) bool {
@ -757,8 +758,8 @@ func (t *torrent) forNeededPieces(f func(piece int) (more bool)) (all bool) {
} }
func (t *torrent) connHasWantedPieces(c *connection) bool { func (t *torrent) connHasWantedPieces(c *connection) bool {
for i := range t.pendingPieces { for it := t.pendingPieces.Iter(); it.Next(); {
if c.PeerHasPiece(i) { if c.PeerHasPiece(it.Value()) {
return true return true
} }
} }
@ -890,7 +891,7 @@ func (t *torrent) piecePriority(piece int) (ret piecePriority) {
if t.pieceComplete(piece) { if t.pieceComplete(piece) {
return return
} }
if _, ok := t.pendingPieces[piece]; ok { if t.pendingPieces.Contains(piece) {
ret = PiecePriorityNormal ret = PiecePriorityNormal
} }
raiseRet := func(prio piecePriority) { raiseRet := func(prio piecePriority) {
@ -912,15 +913,15 @@ func (t *torrent) piecePriority(piece int) (ret piecePriority) {
func (t *torrent) pendPiece(piece int, cl *Client) { func (t *torrent) pendPiece(piece int, cl *Client) {
if t.pendingPieces == nil { if t.pendingPieces == nil {
t.pendingPieces = make(map[int]struct{}, t.Info.NumPieces()) t.pendingPieces = bitmap.New()
} }
if _, ok := t.pendingPieces[piece]; ok { if t.pendingPieces.Contains(piece) {
return return
} }
if t.havePiece(piece) { if t.havePiece(piece) {
return return
} }
t.pendingPieces[piece] = struct{}{} t.pendingPieces.Add(piece)
for _, c := range t.Conns { for _, c := range t.Conns {
if !c.PeerHasPiece(piece) { if !c.PeerHasPiece(piece) {
continue continue