Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf: share constants between Program instances #1442

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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

* perf: share `constants` field across `Program` instances [#1442](https://github.com/lambdaclass/cairo-vm/pull/1442)

* fix: ec_recover hints no longer panic when divisor is 0 [#1433](https://github.com/lambdaclass/cairo-vm/pull/1433)

* feat: Implement the Serialize and Deserialize traits for the CairoPie struct [#1438](https://github.com/lambdaclass/cairo-vm/pull/1438)
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 @@ -500,11 +500,11 @@ pub fn parse_program_json(
.debug_info
.map(|debug_info| debug_info.instruction_locations),
identifiers: program_json.identifiers,
constants,
reference_manager: Program::get_reference_list(&program_json.reference_manager),
};
Ok(Program {
shared_program_data: Arc::new(shared_program_data),
constants,
builtins: program_json.builtins,
})
}
Expand Down
14 changes: 7 additions & 7 deletions vm/src/types/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub(crate) struct SharedProgramData {
pub(crate) error_message_attributes: Vec<Attribute>,
pub(crate) instruction_locations: Option<HashMap<usize, InstructionLocation>>,
pub(crate) identifiers: HashMap<String, Identifier>,
pub(crate) constants: HashMap<String, Felt252>,
pub(crate) reference_manager: Vec<HintReference>,
}

Expand Down Expand Up @@ -91,6 +92,7 @@ impl<'a> Arbitrary<'a> for SharedProgramData {
error_message_attributes: Vec::<Attribute>::arbitrary(u)?,
instruction_locations: Option::<HashMap<usize, InstructionLocation>>::arbitrary(u)?,
identifiers: HashMap::<String, Identifier>::arbitrary(u)?,
constants: HashMap::arbitrary(u)?,
reference_manager: Vec::<HintReference>::arbitrary(u)?,
})
}
Expand Down Expand Up @@ -160,7 +162,6 @@ type HintRange = Option<(usize, NonZeroUsize)>;
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Program {
pub(crate) shared_program_data: Arc<SharedProgramData>,
pub(crate) constants: HashMap<String, Felt252>,
pub(crate) builtins: Vec<BuiltinName>,
}

Expand Down Expand Up @@ -197,11 +198,11 @@ impl Program {
error_message_attributes,
instruction_locations,
identifiers,
constants,
reference_manager: Self::get_reference_list(&reference_manager),
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
builtins,
})
}
Expand Down Expand Up @@ -231,11 +232,11 @@ impl Program {
error_message_attributes,
instruction_locations,
identifiers,
constants,
reference_manager: Self::get_reference_list(&reference_manager),
};
Ok(Self {
shared_program_data: Arc::new(shared_program_data),
constants,
builtins,
})
}
Expand Down Expand Up @@ -340,7 +341,6 @@ impl Default for Program {
fn default() -> Self {
Self {
shared_program_data: Arc::new(SharedProgramData::default()),
constants: HashMap::new(),
builtins: Vec::new(),
}
}
Expand Down Expand Up @@ -643,7 +643,7 @@ mod tests {
assert_eq!(program.shared_program_data.main, None);
assert_eq!(program.shared_program_data.identifiers, identifiers);
assert_eq!(
program.constants,
program.shared_program_data.constants,
[("__main__.main.SIZEOF_LOCALS", Felt252::zero())]
.into_iter()
.map(|(key, value)| (key.to_string(), value))
Expand Down Expand Up @@ -1212,7 +1212,7 @@ mod tests {
.map(|(key, value)| (key.to_string(), value))
.collect::<HashMap<_, _>>();

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

#[test]
Expand All @@ -1232,13 +1232,13 @@ mod tests {
error_message_attributes: Vec::new(),
instruction_locations: None,
identifiers: HashMap::new(),
constants: HashMap::new(),
reference_manager: Program::get_reference_list(&ReferenceManager {
references: Vec::new(),
}),
};
let program = Program {
shared_program_data: Arc::new(shared_program_data),
constants: HashMap::new(),
builtins: Vec::new(),
};

Expand Down
10 changes: 5 additions & 5 deletions vm/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,13 +266,13 @@ pub mod test_utils {
error_message_attributes: crate::stdlib::vec::Vec::new(),
instruction_locations: None,
identifiers: crate::stdlib::collections::HashMap::new(),
constants: crate::stdlib::collections::HashMap::new(),
reference_manager: Program::get_reference_list(&ReferenceManager {
references: crate::stdlib::vec::Vec::new(),
}),
};
Program {
shared_program_data: Arc::new(shared_program_data),
constants: crate::stdlib::collections::HashMap::new(),
builtins: vec![$( $builtin_name ),*],
}
}};
Expand Down Expand Up @@ -356,9 +356,9 @@ pub mod test_utils {
error_message_attributes: val.error_message_attributes,
instruction_locations: val.instruction_locations,
identifiers: val.identifiers,
constants: val.constants,
reference_manager: Program::get_reference_list(&val.reference_manager),
}),
constants: val.constants,
builtins: val.builtins,
}
}
Expand Down Expand Up @@ -932,13 +932,13 @@ mod test {
error_message_attributes: Vec::new(),
instruction_locations: None,
identifiers: HashMap::new(),
constants: HashMap::new(),
reference_manager: Program::get_reference_list(&ReferenceManager {
references: Vec::new(),
}),
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
builtins: Vec::new(),
};
assert_eq!(program, program!())
Expand All @@ -956,13 +956,13 @@ mod test {
error_message_attributes: Vec::new(),
instruction_locations: None,
identifiers: HashMap::new(),
constants: HashMap::new(),
reference_manager: Program::get_reference_list(&ReferenceManager {
references: Vec::new(),
}),
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
builtins: vec![BuiltinName::range_check],
};

Expand All @@ -981,13 +981,13 @@ mod test {
error_message_attributes: Vec::new(),
instruction_locations: None,
identifiers: HashMap::new(),
constants: HashMap::new(),
reference_manager: Program::get_reference_list(&ReferenceManager {
references: Vec::new(),
}),
};
let program = Program {
shared_program_data: Arc::new(shared_data),
constants: HashMap::new(),
builtins: vec![BuiltinName::range_check],
};

Expand Down
6 changes: 3 additions & 3 deletions vm/src/vm/runners/cairo_runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ impl CairoRunner {
}

pub fn get_constants(&self) -> &HashMap<String, Felt252> {
&self.program.constants
&self.program.shared_program_data.constants
}

pub fn get_program_builtins(&self) -> &Vec<BuiltinName> {
Expand Down Expand Up @@ -565,7 +565,7 @@ impl CairoRunner {
hint_processor,
&mut self.exec_scopes,
hint_data,
&self.program.constants,
&self.program.shared_program_data.constants,
)?;
hint_processor.consume_step();
}
Expand Down Expand Up @@ -605,7 +605,7 @@ impl CairoRunner {
hint_processor,
&mut self.exec_scopes,
hint_data,
&self.program.constants,
&self.program.shared_program_data.constants,
)?;
}

Expand Down