Skip to content
This repository was archived by the owner on Jul 22, 2024. It is now read-only.

Commit 12c9601

Browse files
committed
refactor substract_mappings and friends to avoid clones of the whole hashmap
1 parent 579156a commit 12c9601

File tree

3 files changed

+45
-36
lines changed

3 files changed

+45
-36
lines changed

src/state/cached_state.rs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ use crate::{
77
services::api::contract_classes::compiled_class::CompiledClass,
88
state::StateDiff,
99
utils::{
10-
get_erc20_balance_var_addresses, subtract_mappings, to_cache_state_storage_mapping,
11-
Address, ClassHash,
10+
get_erc20_balance_var_addresses, subtract_mappings, subtract_mappings_keys,
11+
to_cache_state_storage_mapping, Address, ClassHash,
1212
},
1313
};
1414
use cairo_vm::felt::Felt252;
@@ -281,32 +281,24 @@ impl<T: StateReader> State for CachedState<T> {
281281
self.update_initial_values_of_write_only_accesses()?;
282282

283283
let mut storage_updates = subtract_mappings(
284-
self.cache.storage_writes.clone(),
285-
self.cache.storage_initial_values.clone(),
284+
&self.cache.storage_writes,
285+
&self.cache.storage_initial_values,
286286
);
287287

288288
let storage_unique_updates = storage_updates.keys().map(|k| k.0.clone());
289289

290-
let class_hash_updates: Vec<_> = subtract_mappings(
291-
self.cache.class_hash_writes.clone(),
292-
self.cache.class_hash_initial_values.clone(),
293-
)
294-
.keys()
295-
.cloned()
296-
.collect();
290+
let class_hash_updates = subtract_mappings_keys(
291+
&self.cache.class_hash_writes,
292+
&self.cache.class_hash_initial_values,
293+
);
297294

298-
let nonce_updates: Vec<_> = subtract_mappings(
299-
self.cache.nonce_writes.clone(),
300-
self.cache.nonce_initial_values.clone(),
301-
)
302-
.keys()
303-
.cloned()
304-
.collect();
295+
let nonce_updates =
296+
subtract_mappings_keys(&self.cache.nonce_writes, &self.cache.nonce_initial_values);
305297

306298
let mut modified_contracts: HashSet<Address> = HashSet::new();
307299
modified_contracts.extend(storage_unique_updates);
308-
modified_contracts.extend(class_hash_updates);
309-
modified_contracts.extend(nonce_updates);
300+
modified_contracts.extend(class_hash_updates.cloned());
301+
modified_contracts.extend(nonce_updates.cloned());
310302

311303
// Add fee transfer storage update before actually charging it, as it needs to be included in the
312304
// calculation of the final fee.

src/state/mod.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -134,25 +134,23 @@ impl StateDiff {
134134
let state_cache = cached_state.cache().to_owned();
135135

136136
let substracted_maps = subtract_mappings(
137-
state_cache.storage_writes.clone(),
138-
state_cache.storage_initial_values.clone(),
137+
&state_cache.storage_writes,
138+
&state_cache.storage_initial_values,
139139
);
140140

141141
let storage_updates = to_state_diff_storage_mapping(substracted_maps);
142142

143-
let address_to_nonce = subtract_mappings(
144-
state_cache.nonce_writes.clone(),
145-
state_cache.nonce_initial_values.clone(),
146-
);
143+
let address_to_nonce =
144+
subtract_mappings(&state_cache.nonce_writes, &state_cache.nonce_initial_values);
147145

148146
let class_hash_to_compiled_class = subtract_mappings(
149-
state_cache.compiled_class_hash_writes.clone(),
150-
state_cache.compiled_class_hash_initial_values.clone(),
147+
&state_cache.compiled_class_hash_writes,
148+
&state_cache.compiled_class_hash_initial_values,
151149
);
152150

153151
let address_to_class_hash = subtract_mappings(
154-
state_cache.class_hash_writes.clone(),
155-
state_cache.class_hash_initial_values,
152+
&state_cache.class_hash_writes,
153+
&state_cache.class_hash_initial_values,
156154
);
157155

158156
Ok(StateDiff {

src/utils.rs

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use sha3::{Digest, Keccak256};
2424
use starknet::core::types::FromByteArrayError;
2525
use starknet_api::core::L2_ADDRESS_UPPER_BOUND;
2626
use starknet_crypto::{pedersen_hash, FieldElement};
27+
use std::ops::Deref;
2728
use std::{
2829
collections::{HashMap, HashSet},
2930
hash::Hash,
@@ -233,17 +234,35 @@ where
233234
!(map.contains_key(key) && (Some(value) == val))
234235
}
235236

236-
pub fn subtract_mappings<K, V>(map_a: HashMap<K, V>, map_b: HashMap<K, V>) -> HashMap<K, V>
237+
pub fn subtract_mappings<'a, K, V>(
238+
map_a: &'a HashMap<K, V>,
239+
map_b: &'a HashMap<K, V>,
240+
) -> HashMap<K, V>
237241
where
238242
K: Hash + Eq + Clone,
239243
V: PartialEq + Clone,
240244
{
241245
map_a
242-
.into_iter()
243-
.filter(|(k, v)| contained_and_not_updated(k, v, &map_b))
246+
.iter()
247+
.filter(|(k, v)| contained_and_not_updated(k.deref(), v.deref(), map_b))
248+
.map(|(k, v)| (k.clone(), v.clone()))
244249
.collect()
245250
}
246251

252+
pub fn subtract_mappings_keys<'a, K, V>(
253+
map_a: &'a HashMap<K, V>,
254+
map_b: &'a HashMap<K, V>,
255+
) -> impl Iterator<Item = &'a K>
256+
where
257+
K: Hash + Eq + Clone,
258+
V: PartialEq + Clone,
259+
{
260+
map_a
261+
.iter()
262+
.filter(|(k, v)| contained_and_not_updated(k.deref(), v.deref(), map_b))
263+
.map(|x| x.0)
264+
}
265+
247266
/// Converts StateDiff storage mapping (addresses map to a key-value mapping) to CachedState
248267
/// storage mapping (Tuple of address and key map to the associated value).
249268
pub fn to_cache_state_storage_mapping(
@@ -647,7 +666,7 @@ mod test {
647666
.into_iter()
648667
.collect::<HashMap<&str, i32>>();
649668

650-
assert_eq!(subtract_mappings(a, b), res);
669+
assert_eq!(subtract_mappings(&a, &b), res);
651670

652671
let mut c = HashMap::new();
653672
let mut d = HashMap::new();
@@ -664,7 +683,7 @@ mod test {
664683
.into_iter()
665684
.collect::<HashMap<i32, i32>>();
666685

667-
assert_eq!(subtract_mappings(c, d), res);
686+
assert_eq!(subtract_mappings(&c, &d), res);
668687

669688
let mut e = HashMap::new();
670689
let mut f = HashMap::new();
@@ -676,7 +695,7 @@ mod test {
676695
f.insert(3, 4);
677696
f.insert(6, 7);
678697

679-
assert_eq!(subtract_mappings(e, f), HashMap::new())
698+
assert_eq!(subtract_mappings(&e, &f), HashMap::new())
680699
}
681700

682701
#[test]

0 commit comments

Comments
 (0)