2
0
mirror of synced 2025-02-23 06:08:07 +00:00

Reduce allocations in iterBitmapsDistinct

This commit is contained in:
Matt Joiner 2021-05-08 10:35:23 +10:00
parent 8dcc52ccf9
commit 95d72a452a
2 changed files with 16 additions and 9 deletions

View File

@ -23,14 +23,20 @@ func TestTorrentOffsetRequest(t *testing.T) {
check(13, 5, 13, Request{}, false) check(13, 5, 13, Request{}, false)
} }
func TestIterBitmapsDistinct(t *testing.T) { func BenchmarkIterBitmapsDistinct(t *testing.B) {
var skip, first, second bitmap.Bitmap t.ReportAllocs()
skip.Add(1) for range iter.N(t.N) {
first.Add(1, 0, 3) var skip, first, second bitmap.Bitmap
second.Add(1, 2, 0) skip.Add(1)
skipCopy := skip.Copy() first.Add(1, 0, 3)
assert.Equal(t, []interface{}{0, 3, 2}, iter.ToSlice(iterBitmapsDistinct(&skipCopy, first, second))) second.Add(1, 2, 0)
assert.Equal(t, []int{1}, skip.ToSortedSlice()) 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) { func TestSpewConnStats(t *testing.T) {

View File

@ -743,12 +743,13 @@ func (cn *PeerConn) updateRequests() {
func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func { func iterBitmapsDistinct(skip *bitmap.Bitmap, bms ...bitmap.Bitmap) iter.Func {
return func(cb iter.Callback) { return func(cb iter.Callback) {
for _, bm := range bms { for _, bm := range bms {
bm.Sub(*skip)
if !iter.All( if !iter.All(
func(i interface{}) bool { func(i interface{}) bool {
skip.Add(i.(int)) skip.Add(i.(int))
return cb(i) return cb(i)
}, },
bitmap.Sub(bm, *skip).Iter, bm.Iter,
) { ) {
return return
} }