Skip to content

Commit

Permalink
Network: optimize cache mem and speed for messageFilter (#5913)
Browse files Browse the repository at this point in the history
  • Loading branch information
jannotti authored Jan 22, 2024
1 parent 877090b commit 135a1b4
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions network/messageFilter.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,20 @@ import (
// IncomingMessage represents a message arriving from some peer in our p2p network
type messageFilter struct {
deadlock.Mutex
buckets []map[crypto.Digest]bool
buckets []map[crypto.Digest]struct{}
maxBucketSize int
currentTopBucket int
nonce [16]byte
}

func makeMessageFilter(bucketsCount, maxBucketSize int) *messageFilter {
mf := &messageFilter{
buckets: make([]map[crypto.Digest]bool, bucketsCount),
buckets: make([]map[crypto.Digest]struct{}, bucketsCount),
maxBucketSize: maxBucketSize,
currentTopBucket: 0,
}
for i := range mf.buckets {
mf.buckets[i] = make(map[crypto.Digest]bool)
}
crypto.RandBytes(mf.nonce[:])
mf.buckets[mf.currentTopBucket] = make(map[crypto.Digest]struct{}, mf.maxBucketSize)
return mf
}

Expand Down Expand Up @@ -69,19 +67,19 @@ func (f *messageFilter) CheckDigest(msgHash crypto.Digest, add bool, promote boo

if !has {
// we don't have this entry. add it.
f.buckets[f.currentTopBucket][msgHash] = true
f.buckets[f.currentTopBucket][msgHash] = struct{}{}
} else {
// we already have it.
// do we need to promote it ?
if promote && f.currentTopBucket != idx {
delete(f.buckets[idx], msgHash)
f.buckets[f.currentTopBucket][msgHash] = true
f.buckets[f.currentTopBucket][msgHash] = struct{}{}
}
}
// check to see if the current bucket reached capacity.
if len(f.buckets[f.currentTopBucket]) >= f.maxBucketSize {
f.currentTopBucket = (f.currentTopBucket + len(f.buckets) - 1) % len(f.buckets)
f.buckets[f.currentTopBucket] = make(map[crypto.Digest]bool)
f.buckets[f.currentTopBucket] = make(map[crypto.Digest]struct{}, f.maxBucketSize)
}

return has
Expand Down

0 comments on commit 135a1b4

Please sign in to comment.