Revert "Use a flat slice for pending request counts"

This reverts commit dfc421824c.
This commit is contained in:
Matt Joiner 2021-11-25 22:32:52 +11:00
parent 65ceef557a
commit 8ddbf5a852
1 changed files with 27 additions and 10 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)
} }