Skip to content

Commit d109cc6

Browse files
Seulgi Kimsgkim126
authored andcommitted
Cleanup MemoryDB
1 parent b67a4e5 commit d109cc6

File tree

1 file changed

+12
-35
lines changed

1 file changed

+12
-35
lines changed

util/memorydb/src/lib.rs

Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
2+
// Copyright 2019 Kodebox, Inc.
23
// This file is part of Parity.
34

45
// Parity is free software: you can redistribute it and/or modify
@@ -85,9 +86,7 @@ pub struct MemoryDB {
8586
impl MemoryDB {
8687
/// Create a new instance of the memory DB.
8788
pub fn new() -> MemoryDB {
88-
MemoryDB {
89-
data: H256FastMap::default(),
90-
}
89+
Default::default()
9190
}
9291

9392
/// Clear all data from the database.
@@ -157,18 +156,11 @@ impl MemoryDB {
157156
/// Consolidate all the entries of `other` into `self`.
158157
pub fn consolidate(&mut self, mut other: Self) {
159158
for (key, (value, rc)) in other.drain() {
160-
match self.data.entry(key) {
161-
Entry::Occupied(mut entry) => {
162-
if entry.get().1 < 0 {
163-
entry.get_mut().0 = value;
164-
}
165-
166-
entry.get_mut().1 += rc;
167-
}
168-
Entry::Vacant(entry) => {
169-
entry.insert((value, rc));
170-
}
159+
let (old_value, old_rc) = self.data.entry(key).or_default();
160+
if *old_rc <= 0 {
161+
*old_value = value;
171162
}
163+
*old_rc += rc;
172164
}
173165
}
174166
}
@@ -214,35 +206,20 @@ impl HashDB for MemoryDB {
214206
return BLAKE_NULL_RLP
215207
}
216208
let key = blake256(value);
217-
match self.data.entry(key) {
218-
Entry::Occupied(mut entry) => {
219-
let &mut (ref mut old_value, ref mut rc) = entry.get_mut();
220-
if *rc <= 0 {
221-
*old_value = value.to_vec();
222-
}
223-
*rc += 1;
224-
}
225-
Entry::Vacant(entry) => {
226-
entry.insert((value.to_vec(), 1));
227-
}
209+
let (old_value, rc) = self.data.entry(key).or_default();
210+
if *rc <= 0 {
211+
*old_value = value.to_vec();
228212
}
213+
*rc += 1;
229214
key
230215
}
231216

232217
fn remove(&mut self, key: &H256) {
233218
if key == &BLAKE_NULL_RLP {
234219
return
235220
}
236-
237-
match self.data.entry(*key) {
238-
Entry::Occupied(mut entry) => {
239-
let &mut (_, ref mut rc) = entry.get_mut();
240-
*rc -= 1;
241-
}
242-
Entry::Vacant(entry) => {
243-
entry.insert((DBValue::new(), -1));
244-
}
245-
}
221+
let (_, rc) = self.data.entry(*key).or_default();
222+
*rc -= 1;
246223
}
247224

248225
fn is_empty(&self) -> bool {

0 commit comments

Comments
 (0)