]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Use a flat slice for pending request counts
authorMatt Joiner <anacrolix@gmail.com>
Sun, 10 Oct 2021 00:54:19 +0000 (11:54 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 19 Oct 2021 03:08:56 +0000 (14:08 +1100)
Under heavy load, seems to be 2-3x faster.

pending-requests.go

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