summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJohn Jannotti <jannotti@gmail.com>2024-01-22 13:43:42 -0500
committerGitHub <noreply@github.com>2024-01-22 13:43:42 -0500
commit135a1b4f0dd24c9fb9298b83170600e6657eee86 (patch)
treeb42692d3254909d141dfc3bc53079d2d0ddcb6d7
parent877090be830c2003ff9e44b56ed5a8f355258ae5 (diff)
Network: optimize cache mem and speed for messageFilter (#5913)
-rw-r--r--network/messageFilter.go14
1 files changed, 6 insertions, 8 deletions
diff --git a/network/messageFilter.go b/network/messageFilter.go
index ed19f24b5..b098eddf7 100644
--- a/network/messageFilter.go
+++ b/network/messageFilter.go
@@ -26,7 +26,7 @@ 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
@@ -34,14 +34,12 @@ type messageFilter struct {
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
}
@@ -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