From aa37bb1dbe73e3870efffee3dec3eb2d73e07945 Mon Sep 17 00:00:00 2001 From: Ian Suvak Date: Tue, 17 Jan 2023 23:38:32 -0500 Subject: [PATCH 1/4] Improve txdedupe hitrate --- data/txDupCache.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/data/txDupCache.go b/data/txDupCache.go index 6e127e2dba..1fca5d0c7b 100644 --- a/data/txDupCache.go +++ b/data/txDupCache.go @@ -244,6 +244,13 @@ func (c *txSaltedCache) CheckAndPut(msg []byte) (*crypto.Digest, bool) { d = &dn } + if _, found := c.cur[*d]; found { + return nil, true + } + if _, found := c.prev[*d]; found { + return nil, true + } + c.cur[*d] = struct{}{} return d, false } From fdcf3bb534a7ddda6f45b3d3b535bf6c5d460095 Mon Sep 17 00:00:00 2001 From: Ian Suvak Date: Tue, 17 Jan 2023 23:52:12 -0500 Subject: [PATCH 2/4] Properly return the hash and add a comment --- data/txDupCache.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/data/txDupCache.go b/data/txDupCache.go index 1fca5d0c7b..d8d571fbf0 100644 --- a/data/txDupCache.go +++ b/data/txDupCache.go @@ -244,11 +244,13 @@ func (c *txSaltedCache) CheckAndPut(msg []byte) (*crypto.Digest, bool) { d = &dn } + // Do a final check to see if another copy of the transaction got to the write lock at the same time. + // No need to rehash since we have it already if _, found := c.cur[*d]; found { - return nil, true + return d, true } if _, found := c.prev[*d]; found { - return nil, true + return d, true } c.cur[*d] = struct{}{} From 34db9804468f919bc67316c7969a6f9c5faa05ed Mon Sep 17 00:00:00 2001 From: Ian Suvak Date: Wed, 18 Jan 2023 15:40:28 -0500 Subject: [PATCH 3/4] Move the check before the pageswap --- data/txDupCache.go | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/data/txDupCache.go b/data/txDupCache.go index d8d571fbf0..d09079bda5 100644 --- a/data/txDupCache.go +++ b/data/txDupCache.go @@ -230,6 +230,12 @@ func (c *txSaltedCache) CheckAndPut(msg []byte) (*crypto.Digest, bool) { } } + // Do another check to see if another copy of the transaction won the race to write it to the cache + // Only check current to save a lookup since swaps are rare and no need to re-hash + if _, found := c.cur[*d]; found { + return d, found + } + if len(c.cur) >= c.maxSize { c.innerSwap(false) ptr := saltedPool.Get() @@ -244,15 +250,6 @@ func (c *txSaltedCache) CheckAndPut(msg []byte) (*crypto.Digest, bool) { d = &dn } - // Do a final check to see if another copy of the transaction got to the write lock at the same time. - // No need to rehash since we have it already - if _, found := c.cur[*d]; found { - return d, true - } - if _, found := c.prev[*d]; found { - return d, true - } - c.cur[*d] = struct{}{} return d, false } From 94a6f5bf6cb27754373e24fec8a808a1a31fa5a7 Mon Sep 17 00:00:00 2001 From: Ian Suvak Date: Wed, 18 Jan 2023 16:33:13 -0500 Subject: [PATCH 4/4] Move inside else block to avoid redundant lookup --- data/txDupCache.go | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data/txDupCache.go b/data/txDupCache.go index d09079bda5..96a426dc7b 100644 --- a/data/txDupCache.go +++ b/data/txDupCache.go @@ -228,12 +228,12 @@ func (c *txSaltedCache) CheckAndPut(msg []byte) (*crypto.Digest, bool) { // already added to cache between RUnlock() and Lock(), return return d, found } - } - - // Do another check to see if another copy of the transaction won the race to write it to the cache - // Only check current to save a lookup since swaps are rare and no need to re-hash - if _, found := c.cur[*d]; found { - return d, found + } else { + // Do another check to see if another copy of the transaction won the race to write it to the cache + // Only check current to save a lookup since swaps are rare and no need to re-hash + if _, found := c.cur[*d]; found { + return d, found + } } if len(c.cur) >= c.maxSize {