Skip to content

Commit

Permalink
don't download pieces we already have
Browse files Browse the repository at this point in the history
  • Loading branch information
majestrate committed Sep 14, 2017
1 parent f6ac605 commit a804127
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
10 changes: 3 additions & 7 deletions src/xd/lib/bittorrent/bitfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,13 +167,10 @@ func (bf *Bitfield) Completed() bool {
type rareSet map[uint32]uint32

// FindRarest finds the set bit we have that is rarest in others
func (bf *Bitfield) FindRarest(others []*Bitfield, excluding []uint32) (idx uint32) {
func (bf *Bitfield) FindRarest(others []*Bitfield, exclude func(uint32) bool) (idx uint32) {
bits := make(rareSet)
i := bf.Length
m := make(map[uint32]bool)
for _, k := range excluding {
m[k] = true
}

for i > 0 {
i--
bits[i] = 0
Expand All @@ -186,8 +183,7 @@ func (bf *Bitfield) FindRarest(others []*Bitfield, excluding []uint32) (idx uint

min := uint32(len(others) + 1)
for index, count := range bits {
_, exclude := m[index]
if exclude {
if exclude(index) {
continue
}
if count < min && bf.Has(index) {
Expand Down
10 changes: 9 additions & 1 deletion src/xd/lib/bittorrent/swarm/torrent.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,15 @@ func (t *Torrent) getRarestPiece(remote *bittorrent.Bitfield, exclude []uint32)
swarm = append(swarm, c.bf)
}
})
idx = remote.FindRarest(swarm, exclude)
m := make(map[uint32]bool)
for idx := range exclude {
m[exclude[idx]] = true
}
bt := t.st.Bitfield()
idx = remote.FindRarest(swarm, func(idx uint32) bool {
_, has := m[idx]
return bt.Has(idx) || has
})
return
}

Expand Down

0 comments on commit a804127

Please sign in to comment.