Skip to content

Commit 6003d66

Browse files
authored
performance(sierra-to-casm): Small map optimization of vars map. (#8480)
1 parent bef23a9 commit 6003d66

File tree

5 files changed

+16
-18
lines changed

5 files changed

+16
-18
lines changed

crates/cairo-lang-sierra-to-casm/src/annotations.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use std::iter;
2-
31
use cairo_lang_casm::ap_change::{ApChangeError, ApplyApChange};
42
use cairo_lang_sierra::edit_state::{put_results, take_args};
53
use cairo_lang_sierra::ids::{ConcreteTypeId, FunctionId, VarId};
@@ -154,7 +152,7 @@ pub struct ProgramAnnotations {
154152
impl ProgramAnnotations {
155153
fn new(n_statements: usize, backwards_jump_indices: UnorderedHashSet<StatementIdx>) -> Self {
156154
ProgramAnnotations {
157-
per_statement_annotations: iter::repeat_with(|| None).take(n_statements).collect(),
155+
per_statement_annotations: vec![None; n_statements],
158156
backwards_jump_indices,
159157
}
160158
}

crates/cairo-lang-sierra-to-casm/src/references.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use cairo_lang_sierra::ids::{ConcreteTypeId, VarId};
55
use cairo_lang_sierra::program::{Function, StatementIdx};
66
use cairo_lang_sierra_type_size::TypeSizeMap;
77
use cairo_lang_utils::casts::IntoOrPanic;
8-
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
8+
use cairo_lang_utils::small_ordered_map::SmallOrderedMap;
99
use cairo_lang_utils::write_comma_separated;
1010
use thiserror::Error;
1111

@@ -23,7 +23,7 @@ pub enum ReferencesError {
2323
UnknownType(ConcreteTypeId),
2424
}
2525

26-
pub type StatementRefs = OrderedHashMap<VarId, ReferenceValue>;
26+
pub type StatementRefs = SmallOrderedMap<VarId, ReferenceValue>;
2727

2828
/// A Sierra reference to a value.
2929
/// Corresponds to an argument or return value of a Sierra statement.

crates/cairo-lang-sierra/src/edit_state.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
1+
use cairo_lang_utils::small_ordered_map::SmallOrderedMap;
22
use thiserror::Error;
33

44
use crate::ids::VarId;
@@ -25,12 +25,12 @@ impl EditStateError {
2525

2626
/// Given a map with var ids as keys, extracts out the given ids, failing if some id is missing.
2727
pub fn take_args<'a, V: 'a>(
28-
mut state: OrderedHashMap<VarId, V>,
28+
mut state: SmallOrderedMap<VarId, V>,
2929
ids: impl Iterator<Item = &'a VarId>,
30-
) -> Result<(OrderedHashMap<VarId, V>, Vec<V>), EditStateError> {
30+
) -> Result<(SmallOrderedMap<VarId, V>, Vec<V>), EditStateError> {
3131
let mut vals = vec![];
3232
for id in ids {
33-
match state.swap_remove(id) {
33+
match state.remove(id) {
3434
None => {
3535
return Err(EditStateError::MissingReference(id.clone()));
3636
}
@@ -44,9 +44,9 @@ pub fn take_args<'a, V: 'a>(
4444

4545
/// Adds the given pairs to map with var ids as keys, failing if some variable is overridden.
4646
pub fn put_results<'a, V>(
47-
mut state: OrderedHashMap<VarId, V>,
47+
mut state: SmallOrderedMap<VarId, V>,
4848
results: impl Iterator<Item = (&'a VarId, V)>,
49-
) -> Result<OrderedHashMap<VarId, V>, EditStateError> {
49+
) -> Result<SmallOrderedMap<VarId, V>, EditStateError> {
5050
for (id, v) in results {
5151
if state.insert(id.clone(), v).is_some() {
5252
return Err(EditStateError::VariableOverride(id.clone()));
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
use cairo_lang_test_utils::test;
2-
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
2+
use cairo_lang_utils::small_ordered_map::SmallOrderedMap;
33

44
use crate::edit_state::{EditStateError, put_results, take_args};
55
use crate::ids::VarId;
66

7-
pub type State = OrderedHashMap<VarId, i64>;
7+
pub type State = SmallOrderedMap<VarId, i64>;
88

99
#[test]
1010
fn empty() {
@@ -15,19 +15,19 @@ fn empty() {
1515
#[test]
1616
fn basic_mapping() {
1717
assert_eq!(
18-
take_args(State::from([("arg".into(), 0)]), vec![&"arg".into()].into_iter(),),
18+
take_args(State::from_iter([("arg".into(), 0)]), vec![&"arg".into()].into_iter(),),
1919
Ok((State::default(), vec![0]))
2020
);
2121
assert_eq!(
2222
put_results(State::default(), vec![(&"res".into(), 1)].into_iter(),),
23-
Ok(State::from([("res".into(), 1)]))
23+
Ok(State::from_iter([("res".into(), 1)]))
2424
);
2525
assert_eq!(
2626
take_args(State::default(), vec![&"arg".into()].into_iter(),),
2727
Err(EditStateError::MissingReference("arg".into()))
2828
);
2929
assert_eq!(
30-
put_results(State::from([("res".into(), 1)]), vec![(&"res".into(), 1)].into_iter(),),
30+
put_results(State::from_iter([("res".into(), 1)]), vec![(&"res".into(), 1)].into_iter(),),
3131
Err(EditStateError::VariableOverride("res".into()))
3232
);
3333
}

crates/cairo-lang-sierra/src/simulation/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::collections::HashMap;
22

3-
use cairo_lang_utils::ordered_hash_map::OrderedHashMap;
3+
use cairo_lang_utils::small_ordered_map::SmallOrderedMap;
44
use itertools::izip;
55
use thiserror::Error;
66

@@ -83,7 +83,7 @@ impl SimulationContext<'_> {
8383
actual: inputs.len(),
8484
});
8585
}
86-
let mut state = OrderedHashMap::<VarId, CoreValue>::from_iter(
86+
let mut state = SmallOrderedMap::<VarId, CoreValue>::from_iter(
8787
izip!(func.params.iter(), inputs).map(|(param, input)| (param.id.clone(), input)),
8888
);
8989
loop {

0 commit comments

Comments
 (0)