diff --git a/crates/phala-trie-storage/src/memdb.rs b/crates/phala-trie-storage/src/memdb.rs index d9fad75f7b..7e8a553c70 100644 --- a/crates/phala-trie-storage/src/memdb.rs +++ b/crates/phala-trie-storage/src/memdb.rs @@ -308,6 +308,9 @@ impl MemoryDB } entry.get_mut().1 += rc; + if entry.get().1 == 0 { + _ = entry.remove(); + } }, Entry::Vacant(entry) => { entry.insert((value, rc)); @@ -354,6 +357,9 @@ impl PlainDB for MemoryDB *old_value = value; } *rc += 1; + if *rc == 0 { + _ = entry.remove(); + } }, Entry::Vacant(entry) => { entry.insert((value, 1)); @@ -366,6 +372,9 @@ impl PlainDB for MemoryDB Entry::Occupied(mut entry) => { let &mut (_, ref mut rc) = entry.get_mut(); *rc -= 1; + if *rc == 0 { + _ = entry.remove(); + } }, Entry::Vacant(entry) => { let value = T::default(); @@ -433,6 +442,9 @@ impl HashDB for MemoryDB *old_value = value; } *rc += 1; + if *rc == 0 { + _ = entry.remove(); + } }, Entry::Vacant(entry) => { entry.insert((value, 1)); @@ -460,6 +472,9 @@ impl HashDB for MemoryDB Entry::Occupied(mut entry) => { let &mut (_, ref mut rc) = entry.get_mut(); *rc -= 1; + if *rc == 0 { + _ = entry.remove(); + } }, Entry::Vacant(entry) => { let value = T::default();