2
0
mirror of synced 2025-02-24 14:48:27 +00:00

Merge branch 'bsi-pending-requests' into te

This commit is contained in:
Matt Joiner 2021-11-27 09:38:58 +11:00
commit 3626bb9fa9
3 changed files with 51 additions and 42 deletions

View File

@ -1,33 +1,50 @@
package torrent package torrent
import (
rbm "github.com/RoaringBitmap/roaring"
roaring "github.com/RoaringBitmap/roaring/BitSliceIndexing"
)
type pendingRequests struct { type pendingRequests struct {
m []int m *roaring.BSI
} }
func (p *pendingRequests) Dec(r RequestIndex) { func (p *pendingRequests) Dec(r RequestIndex) {
prev := p.m[r] _r := uint64(r)
prev, _ := p.m.GetValue(_r)
if prev <= 0 { if prev <= 0 {
panic(prev) panic(prev)
} }
p.m[r]-- p.m.SetValue(_r, prev-1)
} }
func (p *pendingRequests) Inc(r RequestIndex) { func (p *pendingRequests) Inc(r RequestIndex) {
p.m[r]++ _r := uint64(r)
prev, _ := p.m.GetValue(_r)
p.m.SetValue(_r, prev+1)
} }
func (p *pendingRequests) Init(maxIndex RequestIndex) { func (p *pendingRequests) Init(maxIndex RequestIndex) {
p.m = make([]int, maxIndex) p.m = roaring.NewDefaultBSI()
}
var allBits rbm.Bitmap
func init() {
allBits.AddRange(0, rbm.MaxRange)
} }
func (p *pendingRequests) AssertEmpty() { func (p *pendingRequests) AssertEmpty() {
for _, count := range p.m { if p.m == nil {
if count != 0 { panic(p.m)
panic(count)
} }
sum, _ := p.m.Sum(&allBits)
if sum != 0 {
panic(sum)
} }
} }
func (p *pendingRequests) Get(r RequestIndex) int { func (p *pendingRequests) Get(r RequestIndex) int {
return p.m[r] count, _ := p.m.GetValue(uint64(r))
return int(count)
} }

View File

@ -1,19 +1,12 @@
package torrent package torrent
import ( // // Ensure that cmp.Diff will detect errors as required.
"testing" // func TestPendingRequestsDiff(t *testing.T) {
// var a, b pendingRequests
qt "github.com/frankban/quicktest" // c := qt.New(t)
"github.com/google/go-cmp/cmp" // diff := func() string { return cmp.Diff(a.m, b.m) }
) // c.Check(diff(), qt.ContentEquals, "")
// a.m = []int{1, 3}
// Ensure that cmp.Diff will detect errors as required. // b.m = []int{1, 2, 3}
func TestPendingRequestsDiff(t *testing.T) { // c.Check(diff(), qt.Not(qt.Equals), "")
var a, b pendingRequests // }
c := qt.New(t)
diff := func() string { return cmp.Diff(a.m, b.m) }
c.Check(diff(), qt.ContentEquals, "")
a.m = []int{1, 3}
b.m = []int{1, 2, 3}
c.Check(diff(), qt.Not(qt.Equals), "")
}

View File

@ -28,7 +28,6 @@ import (
"github.com/anacrolix/multiless" "github.com/anacrolix/multiless"
"github.com/anacrolix/sync" "github.com/anacrolix/sync"
"github.com/davecgh/go-spew/spew" "github.com/davecgh/go-spew/spew"
"github.com/google/go-cmp/cmp"
"github.com/pion/datachannel" "github.com/pion/datachannel"
"github.com/anacrolix/torrent/bencode" "github.com/anacrolix/torrent/bencode"
@ -1381,20 +1380,20 @@ func (t *Torrent) assertPendingRequests() {
if !check { if !check {
return return
} }
var actual pendingRequests // var actual pendingRequests
if t.haveInfo() { // if t.haveInfo() {
actual.m = make([]int, t.numRequests()) // actual.m = make([]int, t.numRequests())
} // }
t.iterPeers(func(p *Peer) { // t.iterPeers(func(p *Peer) {
p.actualRequestState.Requests.Iterate(func(x uint32) bool { // p.actualRequestState.Requests.Iterate(func(x uint32) bool {
actual.Inc(x) // actual.Inc(x)
return true // return true
}) // })
}) // })
diff := cmp.Diff(actual.m, t.pendingRequests.m) // diff := cmp.Diff(actual.m, t.pendingRequests.m)
if diff != "" { // if diff != "" {
panic(diff) // panic(diff)
} // }
} }
func (t *Torrent) dropConnection(c *PeerConn) { func (t *Torrent) dropConnection(c *PeerConn) {