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

Use a flat slice for pending request counts

Under heavy load, seems to be 2-3x faster.
This commit is contained in:
Matt Joiner 2021-10-10 11:54:19 +11:00
parent 7d0be0ac65
commit dfc421824c

View File

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