Skip to content

Commit

Permalink
remove page from innerCheck
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy committed Jun 9, 2023
1 parent 1685b49 commit bf3bad5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 22 deletions.
12 changes: 6 additions & 6 deletions data/txDupCache.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func (c *txSaltedCache) innerSwap() {

// innerCheck returns true if exists, the salted hash if does not exist
// locking semantic: read lock must be held
func (c *txSaltedCache) innerCheck(msg []byte) (*crypto.Digest, *sync.Map, *map[crypto.Digest]*sync.Map, bool) {
func (c *txSaltedCache) innerCheck(msg []byte) (*crypto.Digest, *sync.Map, bool) {
ptr := saltedPool.Get()
defer saltedPool.Put(ptr)

Expand All @@ -194,24 +194,24 @@ func (c *txSaltedCache) innerCheck(msg []byte) (*crypto.Digest, *sync.Map, *map[

v, found := c.cur[d]
if found {
return &d, v, &c.cur, true
return &d, v, true
}

toBeHashed = append(toBeHashed[:len(msg)], c.prevSalt[:]...)
toBeHashed = toBeHashed[:len(msg)+len(c.prevSalt)]
pd := crypto.Digest(blake2b.Sum256(toBeHashed))
v, found = c.prev[pd]
if found {
return &pd, v, &c.prev, true
return &pd, v, true
}
return &d, nil, nil, false
return &d, nil, false
}

// CheckAndPut adds (msg, sender) pair into a cache. The sender is appended into values if msg is already in the cache.
// Returns a hashing key used for insertion and its associated map of values. The boolean flag `found` is false if the msg not in the cache.
func (c *txSaltedCache) CheckAndPut(msg []byte, sender network.Peer) (d *crypto.Digest, vals *sync.Map, found bool) {
c.mu.RLock()
d, vals, _, found = c.innerCheck(msg)
d, vals, found = c.innerCheck(msg)
salt := c.curSalt
c.mu.RUnlock()
// fast read-only path: assuming most messages are duplicates, hash msg and check cache
Expand All @@ -228,7 +228,7 @@ func (c *txSaltedCache) CheckAndPut(msg []byte, sender network.Peer) (d *crypto.
c.mu.Lock()
// salt may have changed between RUnlock() and Lock(), rehash if needed
if salt != c.curSalt {
d, vals, _, found = c.innerCheck(msg)
d, vals, found = c.innerCheck(msg)
if found {
c.mu.Unlock()
if _, senderFound = vals.Load(sender); !senderFound {
Expand Down
21 changes: 5 additions & 16 deletions data/txDupCache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ func TestTxHandlerDigestCache(t *testing.T) {
}

func (c *txSaltedCache) check(msg []byte) bool {
_, _, _, found := c.innerCheck(msg)
_, _, found := c.innerCheck(msg)
return found
}

Expand Down Expand Up @@ -427,9 +427,8 @@ func TestTxHandlerSaltedCacheValues(t *testing.T) {
id int
}

d, v, p, found := cache.innerCheck([]byte{1})
d, v, found := cache.innerCheck([]byte{1})
require.False(t, found)
require.Nil(t, p)
require.Nil(t, v)
require.NotNil(t, d)
require.NotEmpty(t, d)
Expand All @@ -439,15 +438,11 @@ func TestTxHandlerSaltedCacheValues(t *testing.T) {
require.False(t, found)
require.NotNil(t, d1)
require.NotEmpty(t, d1)
d, v, p, found = cache.innerCheck([]byte{1})
d, v, found = cache.innerCheck([]byte{1})
require.True(t, found)
require.NotNil(t, p)
require.NotNil(t, v)
require.NotNil(t, d)
require.Equal(t, *d, *d1)
require.Equal(t, p, &cache.cur)
require.Equal(t, *p, cache.cur)
require.Len(t, *p, 1)
smapLenEqual(t, v, 1)
require.Equal(t, v, v1)
smapContains(t, v, snd{id: 1})
Expand All @@ -467,16 +462,13 @@ func TestTxHandlerSaltedCacheValues(t *testing.T) {
require.NotNil(t, dt)
require.NotEmpty(t, dt)
require.Nil(t, cache.prev)
d, v, p, found = cache.innerCheck([]byte{1})
d, v, found = cache.innerCheck([]byte{1})
require.True(t, found)
require.NotNil(t, p)
require.NotNil(t, v)
require.NotNil(t, d)
require.Equal(t, *d, *dt)
require.Equal(t, *d, *d1)
require.Equal(t, v, vt)
require.Equal(t, p, &cache.cur)
require.Len(t, *p, 1)
smapLenEqual(t, v, 2)
smapContains(t, v, snd{id: 1})
smapContains(t, v, snd{id: 2})
Expand Down Expand Up @@ -528,15 +520,12 @@ func TestTxHandlerSaltedCacheValues(t *testing.T) {
require.Len(t, cache.prev, 2)
smapLenEqual(t, cache.prev[*d1], 2)
smapLenEqual(t, cache.prev[*d2], 3)
d, v, p, found = cache.innerCheck([]byte{2})
d, v, found = cache.innerCheck([]byte{2})
require.True(t, found)
require.NotNil(t, p)
require.NotNil(t, v)
require.NotNil(t, d)
require.Equal(t, *d, *dt)
require.Equal(t, *d, *d2)
require.Equal(t, p, &cache.prev)
require.Len(t, *p, 2)
smapLenEqual(t, v, 3)
require.Equal(t, vt, v)
smapContains(t, v, snd{id: 3})
Expand Down

0 comments on commit bf3bad5

Please sign in to comment.