From a7022b4751f1bd7e59f6ef82993af6596c96b39d Mon Sep 17 00:00:00 2001 From: jimboj Date: Mon, 26 Aug 2024 15:50:24 +0800 Subject: [PATCH 1/3] remove duplicates from AllKeys --- lib/runtime/storage/storagediff.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/runtime/storage/storagediff.go b/lib/runtime/storage/storagediff.go index 25e5b64962..ab5bbf15cf 100644 --- a/lib/runtime/storage/storagediff.go +++ b/lib/runtime/storage/storagediff.go @@ -143,7 +143,11 @@ func (cs *storageDiff) clearPrefixInChild(keyToChild string, prefix []byte, func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int) (deleted uint32, allDeleted bool) { allKeys := slices.Clone(trieKeys) newKeys := maps.Keys(cs.upserts) - allKeys = append(allKeys, newKeys...) + for _, k := range newKeys { + if !slices.Contains(allKeys, k) { + allKeys = append(allKeys, k) + } + } deleted = 0 sort.Strings(allKeys) From 8fa9133f158b96d6e7bfa77679b83ab97ef37ebc Mon Sep 17 00:00:00 2001 From: jimboj Date: Wed, 28 Aug 2024 13:52:02 +0800 Subject: [PATCH 2/3] make more efficient --- lib/runtime/storage/storagediff.go | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/runtime/storage/storagediff.go b/lib/runtime/storage/storagediff.go index ab5bbf15cf..5a95ac0dba 100644 --- a/lib/runtime/storage/storagediff.go +++ b/lib/runtime/storage/storagediff.go @@ -141,17 +141,17 @@ func (cs *storageDiff) clearPrefixInChild(keyToChild string, prefix []byte, // optional limit. It returns the number of keys deleted and a boolean // indicating if all keys with the prefix were removed. func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int) (deleted uint32, allDeleted bool) { - allKeys := slices.Clone(trieKeys) newKeys := maps.Keys(cs.upserts) - for _, k := range newKeys { - if !slices.Contains(allKeys, k) { - allKeys = append(allKeys, k) + keysToClear := newKeys + for _, k := range trieKeys { + if _, ok := cs.upserts[k]; !ok { + keysToClear = append(keysToClear, k) } } deleted = 0 - sort.Strings(allKeys) - for _, k := range allKeys { + sort.Strings(keysToClear) + for _, k := range keysToClear { if limit == 0 { break } @@ -165,7 +165,7 @@ func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int) } } - return deleted, deleted == uint32(len(allKeys)) + return deleted, deleted == uint32(len(keysToClear)) } // getFromChild attempts to retrieve a value associated with a specific key From b8494f1536509e33fce56092e6c7d901f65bbae9 Mon Sep 17 00:00:00 2001 From: jimboj Date: Tue, 3 Sep 2024 15:55:20 +0900 Subject: [PATCH 3/3] feedback --- lib/runtime/storage/storagediff.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/runtime/storage/storagediff.go b/lib/runtime/storage/storagediff.go index 5a95ac0dba..d2d3dddf44 100644 --- a/lib/runtime/storage/storagediff.go +++ b/lib/runtime/storage/storagediff.go @@ -142,7 +142,7 @@ func (cs *storageDiff) clearPrefixInChild(keyToChild string, prefix []byte, // indicating if all keys with the prefix were removed. func (cs *storageDiff) clearPrefix(prefix []byte, trieKeys []string, limit int) (deleted uint32, allDeleted bool) { newKeys := maps.Keys(cs.upserts) - keysToClear := newKeys + keysToClear := maps.Keys(cs.upserts) for _, k := range trieKeys { if _, ok := cs.upserts[k]; !ok { keysToClear = append(keysToClear, k)