Implement pending requests using BSI

This commit is contained in:
Matt Joiner 2021-10-10 11:19:08 +11:00
parent 9aff9f3592
commit 10b9e88356
2 changed files with 32 additions and 16 deletions

View File

@ -1,34 +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 map[RequestIndex]int m *roaring.BSI
} }
func (p pendingRequests) Dec(r RequestIndex) { func (p *pendingRequests) Dec(r RequestIndex) {
p.m[r]-- _r := uint64(r)
n := p.m[r] prev, _ := p.m.GetValue(_r)
if n == 0 { if prev <= 0 {
delete(p.m, r) panic(prev)
}
if n < 0 {
panic(n)
} }
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() { func (p *pendingRequests) Init() {
p.m = make(map[RequestIndex]int) p.m = roaring.NewDefaultBSI()
}
var allBits rbm.Bitmap
func init() {
allBits.AddRange(0, rbm.MaxRange)
} }
func (p *pendingRequests) AssertEmpty() { func (p *pendingRequests) AssertEmpty() {
if len(p.m) != 0 { if p.m == nil {
panic(p.m) panic(p.m)
} }
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

@ -1405,7 +1405,7 @@ func (t *Torrent) deletePeerConn(c *PeerConn) (ret bool) {
} }
torrent.Add("deleted connections", 1) torrent.Add("deleted connections", 1)
c.deleteAllRequests() c.deleteAllRequests()
if t.numActivePeers() == 0 { if t.numActivePeers() == 0 && t.haveInfo() {
t.assertNoPendingRequests() t.assertNoPendingRequests()
} }
return return