lambdaclass / cairo-vm

cairo-vm is a Rust implementation of the Cairo VM. Cairo (CPU Algebraic Intermediate Representation) is a programming language for writing provable programs, where one party can prove to another that a certain computation was executed correctly without the need for this party to re-execute the same program.

Home Page:https://lambdaclass.github.io/cairo-vm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Double unwrap for `program_base` in Cairo runner `initialize_state`

tcoratger opened this issue · comments

Issue Description

In the initialize_state function of the CairoRunner structure, there is redundant code in my opinion, that can be improved for clarity and efficiency.

At the beginning of the function, there is a conditional check (if let Some(prog_base) = self.program_base) to ensure that self.program_base is not None.

if let Some(prog_base) = self.program_base {
let initial_pc = Relocatable {
segment_index: prog_base.segment_index,
offset: prog_base.offset + entrypoint,
};
self.initial_pc = Some(initial_pc);
vm.segments
.load_data(prog_base, &self.program.shared_program_data.data)
.map_err(RunnerError::MemoryInitializationError)?;
// Mark all addresses from the program segment as accessed
let base = self
.program_base
.unwrap_or_else(|| Relocatable::from((0, 0)));
for i in 0..self.program.shared_program_data.data.len() {
vm.segments.memory.mark_as_accessed((base + i)?);
}
}

However, a few lines later, the unwrap method is used again on self.program_base to obtain base, and in case of None, it sets base to Relocatable::from((0, 0)).

let base = self
.program_base
.unwrap_or_else(|| Relocatable::from((0, 0)));

This redundant check and assignment can be eliminated because the None case will never happen inside the if statement, making the base declaration unnecessary. Instead, we can directly use the prog_base variable in the loop to mark memory segments as accessed.