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
7 changes: 3 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -426,3 +426,7 @@ unexpected_cfgs = { level = "warn", check-cfg = [

[workspace.lints.clippy]
as_conversions = "warn"

# Memory optimization patches - use cairo-vm with clear_trace() and clear_data() methods
[patch.crates-io]
cairo-vm = { git = "https://github.com/heemankv/cairo-vm", branch = "feat/memory-optimization-methods" }
26 changes: 19 additions & 7 deletions crates/starknet_os/src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ use cairo_vm::types::builtin_name::BuiltinName;
use cairo_vm::types::layout_name::LayoutName;
use cairo_vm::types::program::Program;
use cairo_vm::vm::runners::cairo_pie::{
BuiltinAdditionalData,
CairoPie,
OutputBuiltinAdditionalData,
BuiltinAdditionalData, CairoPie, OutputBuiltinAdditionalData,
};
use cairo_vm::vm::runners::cairo_runner::CairoRunner;
use starknet_types_core::felt::Felt;
Expand Down Expand Up @@ -85,16 +83,30 @@ pub(crate) fn run_program<HP: HintProcessor + CommonHintProcessor>(
cairo_runner
.read_return_values(allow_missing_builtins)
.map_err(StarknetOsError::RunnerError)?;
// TODO(Meshi): Add trace relocation to CairoRunConfig.
cairo_runner
.relocate(cairo_run_config.relocate_mem, false)
.map_err(|e| StarknetOsError::VirtualMachineError(e.into()))?;

// MEMORY OPTIMIZATION: Skip full relocation for CairoPie generation.
// The relocate() call creates relocated_memory and relocated_trace which are NOT used by
// get_cairo_pie(). CairoPie uses the original vm.segments.memory directly.
// Only compute_effective_sizes() is needed for segment size metadata.
// This saves ~2.7GB peak memory by avoiding trace duplication for large blocks.
cairo_runner.vm.segments.compute_effective_sizes();

// MEMORY OPTIMIZATION: Clear the trace before creating CairoPie.
// The trace is only needed for proof generation (via relocate_trace), not for CairoPie.
// For large blocks, the trace can be ~1.5GB. Clearing it before get_cairo_pie()
// prevents peak memory from including both trace AND CairoPieMemory simultaneously.
cairo_runner.vm.clear_trace();

#[cfg(any(test, feature = "testing"))]
crate::test_utils::validations::validate_builtins(&mut cairo_runner);

// Parse the Cairo VM output.
let cairo_pie = cairo_runner.get_cairo_pie().map_err(StarknetOsError::RunnerError)?;

// MEMORY OPTIMIZATION: Clear VM memory after CairoPie creation.
// The CairoPie now has its own copy of the memory, so we can release the original.
// This prevents holding ~1.2GB of duplicate memory during metrics collection.
cairo_runner.vm.segments.memory.clear_data();
Ok(RunnerReturnObject { raw_output, cairo_pie, cairo_runner })
}

Expand Down