Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

#### Upcoming Changes

* opt(breaking): Avoid cloning constants when compiling hints [#2208](https://github.com/lambdaclass/cairo-vm/pull/2208)

* fix: Added `cairo_1_test_contracts` and `cairo_2_test_contracts` as dependencies for `test-extensive_hints` target [#2201](https://github.com/lambdaclass/cairo-vm/pull/2201)

* breaking: Store constants in Hint Data [#2191](https://github.com/lambdaclass/cairo-vm/pull/2191)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ use crate::{
hint_processor_definition::HintReference,
},
serde::deserialize_program::ApTracking,
stdlib::{any::Any, collections::HashMap, prelude::*, rc::Rc},
stdlib::{any::Any, collections::HashMap, prelude::*, rc::Rc, sync::Arc},
types::exec_scope::ExecutionScopes,
vm::{errors::hint_errors::HintError, vm_core::VirtualMachine},
};
Expand All @@ -133,7 +133,7 @@ pub struct HintProcessorData {
pub ap_tracking: ApTracking,
pub ids_data: HashMap<String, HintReference>,
pub accessible_scopes: Vec<String>,
pub constants: Rc<HashMap<String, Felt252>>,
pub constants: Arc<HashMap<String, Felt252>>,
}

impl HintProcessorData {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use super::hint_processor_utils::*;
use crate::any_box;
use crate::hint_processor::cairo_1_hint_processor::dict_manager::DictSquashExecScope;
use crate::hint_processor::hint_processor_definition::HintReference;
use crate::stdlib::rc::Rc;
use crate::stdlib::sync::Arc;
use crate::stdlib::{boxed::Box, collections::HashMap, prelude::*};
use crate::types::relocatable::{MaybeRelocatable, Relocatable};
use crate::vm::runners::cairo_runner::ResourceTracker;
Expand Down Expand Up @@ -1267,7 +1267,7 @@ impl HintProcessorLogic for Cairo1HintProcessor {
// List of accessible scopes in the hint
_accessible_scopes: &[String],
// Identifiers stored in the hint's program.
_constants: Rc<HashMap<String, Felt252>>,
_constants: Arc<HashMap<String, Felt252>>,
) -> Result<Box<dyn Any>, VirtualMachineError> {
let data = hint_code.parse().ok().and_then(|x: usize| self.hints.get(&x).cloned())
.ok_or_else(|| VirtualMachineError::CompileHintFail(
Expand Down
4 changes: 2 additions & 2 deletions vm/src/hint_processor/hint_processor_definition.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, rc::Rc};
use crate::stdlib::{any::Any, boxed::Box, collections::HashMap, prelude::*, sync::Arc};

use crate::any_box;
use crate::serde::deserialize_program::ApTracking;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub trait HintProcessorLogic {
// List of accessible scopes in the hint
accessible_scopes: &[String],
// Identifiers stored in the hint's program.
constants: Rc<HashMap<String, Felt252>>,
constants: Arc<HashMap<String, Felt252>>,
) -> Result<Box<dyn Any>, VirtualMachineError> {
Ok(any_box!(HintProcessorData {
code: hint_code.to_string(),
Expand Down
2 changes: 1 addition & 1 deletion vm/src/serde/deserialize_program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ pub fn parse_program_json(
};
Ok(Program {
shared_program_data: Arc::new(shared_program_data),
constants,
constants: Arc::new(constants),
builtins: program_json.builtins,
})
}
Expand Down
15 changes: 8 additions & 7 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ pub type HintRange = (usize, NonZeroUsize);
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Program {
pub shared_program_data: Arc<SharedProgramData>,
pub constants: HashMap<String, Felt252>,
pub constants: Arc<HashMap<String, Felt252>>,
pub(crate) builtins: Vec<BuiltinName>,
}

Expand Down Expand Up @@ -235,7 +235,7 @@ impl Program {
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
constants: Arc::new(constants),
builtins,
})
}
Expand Down Expand Up @@ -269,7 +269,7 @@ impl Program {
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
constants: Arc::new(constants),
builtins,
})
}
Expand Down Expand Up @@ -420,7 +420,7 @@ impl Default for Program {
fn default() -> Self {
Self {
shared_program_data: Arc::new(SharedProgramData::default()),
constants: HashMap::new(),
constants: Arc::new(HashMap::new()),
builtins: Vec::new(),
}
}
Expand Down Expand Up @@ -760,7 +760,8 @@ mod tests {
[("__main__.main.SIZEOF_LOCALS", Felt252::ZERO)]
.into_iter()
.map(|(key, value)| (key.to_string(), value))
.collect::<HashMap<_, _>>(),
.collect::<HashMap<_, _>>()
.into(),
);
}

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

assert_eq!(program.constants, constants);
assert_eq!(program.constants, constants.into());
}

#[test]
Expand All @@ -1439,7 +1440,7 @@ mod tests {
};
let program = Program {
shared_program_data: Arc::new(shared_program_data),
constants: HashMap::new(),
constants: Arc::new(HashMap::new()),
builtins: Vec::new(),
};

Expand Down
12 changes: 6 additions & 6 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ pub mod test_utils {
};
Program {
shared_program_data: Arc::new(shared_program_data),
constants: crate::stdlib::collections::HashMap::new(),
constants: crate::stdlib::collections::HashMap::new().into(),
builtins: vec![$( $builtin_name ),*],
}
}};
Expand Down Expand Up @@ -400,7 +400,7 @@ pub mod test_utils {
identifiers: val.identifiers,
reference_manager: Program::get_reference_list(&val.reference_manager),
}),
constants: val.constants,
constants: val.constants.into(),
builtins: val.builtins,
}
}
Expand Down Expand Up @@ -476,7 +476,7 @@ pub mod test_utils {
($vm:expr, $ids_data:expr, $hint_code:expr, $exec_scopes:expr, $constants:expr) => {{
let mut hint_data = HintProcessorData::new_default($hint_code.to_string(), $ids_data);
let constants: &HashMap<String, Felt252> = $constants;
hint_data.constants = crate::stdlib::rc::Rc::new(constants.clone());
hint_data.constants = constants.clone().into();
let mut hint_processor = BuiltinHintProcessor::new_empty();
hint_processor.execute_hint(&mut $vm, $exec_scopes, &any_box!(hint_data))
}};
Expand Down Expand Up @@ -978,7 +978,7 @@ mod test {
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
constants: HashMap::new().into(),
builtins: Vec::new(),
};
assert_eq!(program, program!())
Expand All @@ -1002,7 +1002,7 @@ mod test {
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
constants: HashMap::new().into(),
builtins: vec![BuiltinName::range_check],
};

Expand All @@ -1027,7 +1027,7 @@ mod test {
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
constants: HashMap::new().into(),
builtins: vec![BuiltinName::range_check],
};

Expand Down
5 changes: 1 addition & 4 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use crate::{
collections::{BTreeMap, HashMap, HashSet},
ops::{Add, AddAssign, Mul, MulAssign, Sub, SubAssign},
prelude::*,
rc::Rc,
},
types::{builtin_name::BuiltinName, layout::CairoLayoutParams, layout_name::LayoutName},
vm::{
Expand Down Expand Up @@ -648,8 +647,6 @@ impl CairoRunner {
references: &[HintReference],
hint_executor: &mut dyn HintProcessor,
) -> Result<Vec<Box<dyn Any>>, VirtualMachineError> {
let constants = Rc::new(self.program.constants.clone());

self.program
.shared_program_data
.hints_collection
Expand All @@ -662,7 +659,7 @@ impl CairoRunner {
&hint.flow_tracking_data.reference_ids,
references,
&hint.accessible_scopes,
constants.clone(),
self.program.constants.clone(),
)
.map_err(|_| VirtualMachineError::CompileHintFail(hint.code.clone().into()))
})
Expand Down
Loading