diff --git a/Cargo.lock b/Cargo.lock index f2c7f7f1651..836c254c270 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4652,8 +4652,7 @@ dependencies = [ [[package]] name = "cairo-vm" version = "3.0.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "93802c2d382cec1e5f50caec5db81f2a9ac8fe928d55b942e2d1cd395baa9e85" +source = "git+https://github.com/heemankv/cairo-vm?branch=feat%2Fmemory-optimization-methods#ad957f2fcd6150cd62efb77355f7b26ca0baed61" dependencies = [ "anyhow", "bincode 2.0.1", @@ -12565,7 +12564,7 @@ dependencies = [ "dashmap", "educe 0.5.11", "fnv", - "hashbrown 0.15.5", + "hashbrown 0.16.0", "hex", "indexmap 2.12.0", "itertools 0.12.1", @@ -12623,7 +12622,7 @@ name = "stwo-constraint-framework" version = "0.1.1" source = "git+https://github.com/starkware-libs/stwo?rev=6b5c843#6b5c84396da9ce282dc6accb343dd7ef6d1b9966" dependencies = [ - "hashbrown 0.15.5", + "hashbrown 0.16.0", "itertools 0.12.1", "num-traits", "rand 0.8.5", diff --git a/Cargo.toml b/Cargo.toml index 392aaf8da5a..e7a51a338d1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/crates/starknet_os/src/runner.rs b/crates/starknet_os/src/runner.rs index e9abcd980b0..c12e71b6461 100644 --- a/crates/starknet_os/src/runner.rs +++ b/crates/starknet_os/src/runner.rs @@ -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; @@ -85,16 +83,30 @@ pub(crate) fn run_program( 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 }) }