]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Reduce allocations in iterBitmapsDistinct
authorMatt Joiner <anacrolix@gmail.com>
Sat, 8 May 2021 00:35:23 +0000 (10:35 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Sat, 8 May 2021 00:35:23 +0000 (10:35 +1000)
misc_test.go
peerconn.go

index 419ec4d07411bdbdd14ae56f6d07604ab3aeddff..f3a37683ca22da874e8c835f377ab367d39ddca4 100644 (file)
@@ -23,14 +23,20 @@ func TestTorrentOffsetRequest(t *testing.T) {
        check(13, 5, 13, Request{}, false)
 }
 
-func TestIterBitmapsDistinct(t *testing.T) {
-       var skip, first, second bitmap.Bitmap
-       skip.Add(1)
-       first.Add(1, 0, 3)
-       second.Add(1, 2, 0)
-       skipCopy := skip.Copy()
-       assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second)))
-       assert.Equal(t, []int{1}, skip.ToSortedSlice())
+func BenchmarkIterBitmapsDistinct(t *testing.B) {
+       t.ReportAllocs()
+       for range iter.N(t.N) {
+               var skip, first, second bitmap.Bitmap
+               skip.Add(1)
+               first.Add(1, 0, 3)
+               second.Add(1, 2, 0)
+               skipCopy := skip.Copy()
+               t.StartTimer()
+               output := iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second))
+               t.StopTimer()
+               assert.Equal(t, []interface{}{0, 3, 2}, output)
+               assert.Equal(t, []int{1}, skip.ToSortedSlice())
+       }
 }
 
 func TestSpewConnStats(t *testing.T) {
index de82cf3b2a44e3fb18940ea6e6ad87d0085ea073..0c276808f37d8d5e36dbf636e460b719e3fa2d37 100644 (file)
@@ -743,12 +743,13 @@ func (cn *PeerConn) updateRequests() {
 func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
        return func(cb iter.Callback) {
                for _, bm := range bms {
+                       bm.Sub(*skip)
                        if !iter.All(
                                func(i interface{}) bool {
                                        skip.Add(i.(int))
                                        return cb(i)
                                },
-                               bitmap.Sub(bm, *skip).Iter,
+                               bm.Iter,
                        ) {
                                return
                        }