|
1 | 1 | // Copyright 2015-2017 Parity Technologies (UK) Ltd. |
| 2 | +// Copyright 2019 Kodebox, Inc. |
2 | 3 | // This file is part of Parity. |
3 | 4 |
|
4 | 5 | // Parity is free software: you can redistribute it and/or modify |
@@ -85,9 +86,7 @@ pub struct MemoryDB { |
85 | 86 | impl MemoryDB { |
86 | 87 | /// Create a new instance of the memory DB. |
87 | 88 | pub fn new() -> MemoryDB { |
88 | | - MemoryDB { |
89 | | - data: H256FastMap::default(), |
90 | | - } |
| 89 | + Default::default() |
91 | 90 | } |
92 | 91 |
|
93 | 92 | /// Clear all data from the database. |
@@ -157,18 +156,11 @@ impl MemoryDB { |
157 | 156 | /// Consolidate all the entries of `other` into `self`. |
158 | 157 | pub fn consolidate(&mut self, mut other: Self) { |
159 | 158 | 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; |
171 | 162 | } |
| 163 | + *old_rc += rc; |
172 | 164 | } |
173 | 165 | } |
174 | 166 | } |
@@ -214,35 +206,20 @@ impl HashDB for MemoryDB { |
214 | 206 | return BLAKE_NULL_RLP |
215 | 207 | } |
216 | 208 | 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(); |
228 | 212 | } |
| 213 | + *rc += 1; |
229 | 214 | key |
230 | 215 | } |
231 | 216 |
|
232 | 217 | fn remove(&mut self, key: &H256) { |
233 | 218 | if key == &BLAKE_NULL_RLP { |
234 | 219 | return |
235 | 220 | } |
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; |
246 | 223 | } |
247 | 224 |
|
248 | 225 | fn is_empty(&self) -> bool { |
|
0 commit comments