Skip to content

Commit 99c5081

Browse files
Add support for caching Cairo 1 runners
1 parent c001e4b commit 99c5081

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

vm/src/vm/runners/cairo_runner.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,10 @@ impl CairoRunnerBuilder {
241241
|| self.runner_mode == RunnerMode::ProofModeCairo1
242242
}
243243

244+
pub fn get_program_base(&self) -> Option<Relocatable> {
245+
self.program_base
246+
}
247+
244248
pub fn add_memory_segment(&mut self) -> Relocatable {
245249
self.memory.add()
246250
}
@@ -378,6 +382,41 @@ impl CairoRunnerBuilder {
378382
Ok(())
379383
}
380384

385+
/// *Initializes* and *includes* all the given builtins.
386+
///
387+
/// Doesn't take the current layout into account.
388+
pub fn initialize_builtin_runners(
389+
&mut self,
390+
builtins: &[BuiltinName],
391+
) -> Result<(), RunnerError> {
392+
for builtin_name in builtins {
393+
let builtin_runner = match builtin_name {
394+
BuiltinName::pedersen => HashBuiltinRunner::new(Some(32), true).into(),
395+
BuiltinName::range_check => {
396+
RangeCheckBuiltinRunner::<RC_N_PARTS_STANDARD>::new(Some(1), true).into()
397+
}
398+
BuiltinName::output => OutputBuiltinRunner::new(true).into(),
399+
BuiltinName::ecdsa => SignatureBuiltinRunner::new(Some(1), true).into(),
400+
BuiltinName::bitwise => BitwiseBuiltinRunner::new(Some(1), true).into(),
401+
BuiltinName::ec_op => EcOpBuiltinRunner::new(Some(1), true).into(),
402+
BuiltinName::keccak => KeccakBuiltinRunner::new(Some(1), true).into(),
403+
BuiltinName::poseidon => PoseidonBuiltinRunner::new(Some(1), true).into(),
404+
BuiltinName::segment_arena => SegmentArenaBuiltinRunner::new(true).into(),
405+
BuiltinName::range_check96 => {
406+
RangeCheckBuiltinRunner::<RC_N_PARTS_96>::new(Some(1), true).into()
407+
}
408+
BuiltinName::add_mod => {
409+
ModBuiltinRunner::new_add_mod(&ModInstanceDef::new(Some(1), 1, 96), true).into()
410+
}
411+
BuiltinName::mul_mod => {
412+
ModBuiltinRunner::new_mul_mod(&ModInstanceDef::new(Some(1), 1, 96), true).into()
413+
}
414+
};
415+
self.builtin_runners.push(builtin_runner);
416+
}
417+
Ok(())
418+
}
419+
381420
pub fn initialize_base_segments(&mut self) {
382421
self.program_base = Some(self.add_memory_segment());
383422
self.execution_base = Some(self.add_memory_segment());
@@ -412,6 +451,16 @@ impl CairoRunnerBuilder {
412451
Ok(())
413452
}
414453

454+
/// Preallocates memory for `n` more elements in the given segment.
455+
pub fn preallocate_segment(
456+
&mut self,
457+
segment: Relocatable,
458+
n: usize,
459+
) -> Result<(), RunnerError> {
460+
self.memory.memory.preallocate_segment(segment, n)?;
461+
Ok(())
462+
}
463+
415464
/// Loads decoded program instructions.
416465
///
417466
/// # Safety

vm/src/vm/vm_memory/memory.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,22 @@ impl Memory {
747747
self.mark_as_accessed(key);
748748
Ok(())
749749
}
750+
751+
/// Preallocates memory for `n` more elements in the given segment.
752+
///
753+
/// Why not using using `reserve`? When cloning a vector, the capacity
754+
/// of the clone is not necessary equal to the capacity of the original.
755+
/// Because of this, to actually preallocate memory in the builder, we need
756+
/// to insert empty memory cells.
757+
pub fn preallocate_segment(
758+
&mut self,
759+
segment: Relocatable,
760+
n: usize,
761+
) -> Result<(), MemoryError> {
762+
let segment = self.get_segment(segment)?;
763+
segment.extend((0..n).map(|_| MemoryCell::NONE));
764+
Ok(())
765+
}
750766
}
751767

752768
impl From<&Memory> for CairoPieMemory {

0 commit comments

Comments
 (0)