Skip to content

Commit 9cedff9

Browse files
Avoid cloning constants when compiling hints (#2208)
* Avoid cloning constants when compiling hints By changing the compile_hint parameter type to `Arc` instead of `Rc`, we can reuse the constants that are already included in `Program`. This avoids cloning all the constants. With this commit, there is a 9.8% performance increase when replaying mainnet block 10000, compared to 2.5.0. * Update CHANGELOG.md --------- Co-authored-by: Gabriel Bosio <38794644+gabrielbosio@users.noreply.github.com>
1 parent f0022cd commit 9cedff9

File tree

8 files changed

+24
-24
lines changed

8 files changed

+24
-24
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
#### Upcoming Changes
44

5+
* opt(breaking): Avoid cloning constants when compiling hints [#2208](https://github.com/lambdaclass/cairo-vm/pull/2208)
6+
57
* dev(BREAKING): Make blake2s API internal [#2265](https://github.com/lambdaclass/cairo-vm/pull/2265)
68

79
* bugfix: Fix temp segment chain bug [#2195](https://github.com/lambdaclass/cairo-vm/pull/2195)

vm/src/hint_processor/builtin_hint_processor/builtin_hint_processor_definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ use crate::{
115115
hint_processor_definition::HintReference,
116116
},
117117
serde::deserialize_program::ApTracking,
118-
stdlib::{any::Any, collections::HashMap, prelude::*, rc::Rc},
118+
stdlib::{any::Any, collections::HashMap, prelude::*, rc::Rc, sync::Arc},
119119
types::exec_scope::ExecutionScopes,
120120
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
121121
};
@@ -133,7 +133,7 @@ pub struct HintProcessorData {
133133
pub ap_tracking: ApTracking,
134134
pub ids_data: HashMap<String, HintReference>,
135135
pub accessible_scopes: Vec<String>,
136-
pub constants: Rc<HashMap<String, Felt252>>,
136+
pub constants: Arc<HashMap<String, Felt252>>,
137137
}
138138

139139
impl HintProcessorData {

vm/src/hint_processor/cairo_1_hint_processor/hint_processor.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use super::hint_processor_utils::*;
99
use crate::any_box;
1010
use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope;
1111
use crate::hint_processor::hint_processor_definition::HintReference;
12-
use crate::stdlib::rc::Rc;
12+
use crate::stdlib::sync::Arc;
1313
use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*};
1414
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
1515
use crate::vm::runners::cairo_runner::ResourceTracker;
@@ -1267,7 +1267,7 @@ impl HintProcessorLogic for Cairo1HintProcessor {
12671267
// List of accessible scopes in the hint
12681268
_accessible_scopes: &[String],
12691269
// Identifiers stored in the hint's program.
1270-
_constants: Rc<HashMap<String, Felt252>>,
1270+
_constants: Arc<HashMap<String, Felt252>>,
12711271
) -> Result<Box<dyn Any>, VirtualMachineError> {
12721272
let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned())
12731273
.ok_or_else(|| VirtualMachineError::CompileHintFail(

vm/src/hint_processor/hint_processor_definition.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, rc::Rc};
1+
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, sync::Arc};
22

33
use crate::any_box;
44
use crate::serde::deserialize_program::ApTracking;
@@ -44,7 +44,7 @@ pub trait HintProcessorLogic {
4444
// List of accessible scopes in the hint
4545
accessible_scopes: &[String],
4646
// Identifiers stored in the hint's program.
47-
constants: Rc<HashMap<String, Felt252>>,
47+
constants: Arc<HashMap<String, Felt252>>,
4848
) -> Result<Box<dyn Any>, VirtualMachineError> {
4949
Ok(any_box!(HintProcessorData {
5050
code: hint_code.to_string(),

vm/src/serde/deserialize_program.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ pub fn parse_program_json(
493493
};
494494
Ok(Program {
495495
shared_program_data: Arc::new(shared_program_data),
496-
constants,
496+
constants: Arc::new(constants),
497497
builtins: program_json.builtins,
498498
})
499499
}

vm/src/types/program.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ pub type HintRange = (usize, NonZeroUsize);
201201
#[derive(Clone, Debug, PartialEq, Eq)]
202202
pub struct Program {
203203
pub shared_program_data: Arc<SharedProgramData>,
204-
pub constants: HashMap<String, Felt252>,
204+
pub constants: Arc<HashMap<String, Felt252>>,
205205
pub(crate) builtins: Vec<BuiltinName>,
206206
}
207207

@@ -235,7 +235,7 @@ impl Program {
235235
};
236236
Ok(Self {
237237
shared_program_data: Arc::new(shared_program_data),
238-
constants,
238+
constants: Arc::new(constants),
239239
builtins,
240240
})
241241
}
@@ -269,7 +269,7 @@ impl Program {
269269
};
270270
Ok(Self {
271271
shared_program_data: Arc::new(shared_program_data),
272-
constants,
272+
constants: Arc::new(constants),
273273
builtins,
274274
})
275275
}
@@ -420,7 +420,7 @@ impl Default for Program {
420420
fn default() -> Self {
421421
Self {
422422
shared_program_data: Arc::new(SharedProgramData::default()),
423-
constants: HashMap::new(),
423+
constants: Arc::new(HashMap::new()),
424424
builtins: Vec::new(),
425425
}
426426
}
@@ -760,7 +760,8 @@ mod tests {
760760
[("__main__.main.SIZEOF_LOCALS", Felt252::ZERO)]
761761
.into_iter()
762762
.map(|(key, value)| (key.to_string(), value))
763-
.collect::<HashMap<_, _>>(),
763+
.collect::<HashMap<_, _>>()
764+
.into(),
764765
);
765766
}
766767

@@ -1413,7 +1414,7 @@ mod tests {
14131414
.map(|(key, value)| (key.to_string(), value))
14141415
.collect::<HashMap<_, _>>();
14151416

1416-
assert_eq!(program.constants, constants);
1417+
assert_eq!(program.constants, constants.into());
14171418
}
14181419

14191420
#[test]
@@ -1439,7 +1440,7 @@ mod tests {
14391440
};
14401441
let program = Program {
14411442
shared_program_data: Arc::new(shared_program_data),
1442-
constants: HashMap::new(),
1443+
constants: Arc::new(HashMap::new()),
14431444
builtins: Vec::new(),
14441445
};
14451446

vm/src/utils.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ pub mod test_utils {
316316
};
317317
Program {
318318
shared_program_data: Arc::new(shared_program_data),
319-
constants: crate::stdlib::collections::HashMap::new(),
319+
constants: crate::stdlib::collections::HashMap::new().into(),
320320
builtins: vec![$( $builtin_name ),*],
321321
}
322322
}};
@@ -400,7 +400,7 @@ pub mod test_utils {
400400
identifiers: val.identifiers,
401401
reference_manager: Program::get_reference_list(&val.reference_manager),
402402
}),
403-
constants: val.constants,
403+
constants: val.constants.into(),
404404
builtins: val.builtins,
405405
}
406406
}
@@ -476,7 +476,7 @@ pub mod test_utils {
476476
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
477477
let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
478478
let constants: &HashMap<String, Felt252> = $constants;
479-
hint_data.constants = crate::stdlib::rc::Rc::new(constants.clone());
479+
hint_data.constants = constants.clone().into();
480480
let mut hint_processor = BuiltinHintProcessor::new_empty();
481481
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
482482
}};
@@ -978,7 +978,7 @@ mod test {
978978
};
979979
let program = Program {
980980
shared_program_data: Arc::new(shared_data),
981-
constants: HashMap::new(),
981+
constants: HashMap::new().into(),
982982
builtins: Vec::new(),
983983
};
984984
assert_eq!(program, program!())
@@ -1002,7 +1002,7 @@ mod test {
10021002
};
10031003
let program = Program {
10041004
shared_program_data: Arc::new(shared_data),
1005-
constants: HashMap::new(),
1005+
constants: HashMap::new().into(),
10061006
builtins: vec![BuiltinName::range_check],
10071007
};
10081008

@@ -1027,7 +1027,7 @@ mod test {
10271027
};
10281028
let program = Program {
10291029
shared_program_data: Arc::new(shared_data),
1030-
constants: HashMap::new(),
1030+
constants: HashMap::new().into(),
10311031
builtins: vec![BuiltinName::range_check],
10321032
};
10331033

vm/src/vm/runners/cairo_runner.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use crate::{
99
collections::{BTreeMap, HashMap, HashSet},
1010
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
1111
prelude::*,
12-
rc::Rc,
1312
},
1413
types::{builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName},
1514
vm::{
@@ -648,8 +647,6 @@ impl CairoRunner {
648647
references: &[HintReference],
649648
hint_executor: &mut dyn HintProcessor,
650649
) -> Result<Vec<Box<dyn Any>>, VirtualMachineError> {
651-
let constants = Rc::new(self.program.constants.clone());
652-
653650
self.program
654651
.shared_program_data
655652
.hints_collection
@@ -662,7 +659,7 @@ impl CairoRunner {
662659
&hint.flow_tracking_data.reference_ids,
663660
references,
664661
&hint.accessible_scopes,
665-
constants.clone(),
662+
self.program.constants.clone(),
666663
)
667664
.map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into()))
668665
})

0 commit comments

Comments
 (0)