]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Revert "Use a flat slice for pending request counts"
authorMatt Joiner <anacrolix@gmail.com>
Thu, 25 Nov 2021 11:32:52 +0000 (22:32 +1100)
committerMatt Joiner <anacrolix@gmail.com>
Thu, 25 Nov 2021 11:55:02 +0000 (22:55 +1100)
This reverts commit dfc421824ce6c146ff9d9062ad25748c0f8c9990.

pending-requests.go

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