From 1997cb3ed77a1d9d48b51b90ee861968ddbb045f Mon Sep 17 00:00:00 2001 From: jotabulacios Date: Tue, 14 Jul 2026 10:30:31 -0300 Subject: [PATCH] Stream proof serialization into the writer --- bin/cli/src/main.rs | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/bin/cli/src/main.rs b/bin/cli/src/main.rs index 95c3050b7..a0af02df6 100644 --- a/bin/cli/src/main.rs +++ b/bin/cli/src/main.rs @@ -627,15 +627,15 @@ fn cmd_prove( }; let mut writer = BufWriter::new(file); - let bytes = match bincode::serialize(&proof) { - Ok(b) => b, - Err(e) => { - eprintln!("Failed to serialize proof: {}", e); - return ExitCode::FAILURE; - } - }; - - if let Err(e) = writer.write_all(&bytes) { + // Stream the proof straight into the writer instead of `bincode::serialize` + // into an intermediate `Vec`: that buffer holds a full serialized copy of + // the proof while `proof` is still alive (~2x the proof in RAM). `serialize_into` + // keeps the write peak at ~1x. + if let Err(e) = bincode::serialize_into(&mut writer, &proof) { + eprintln!("Failed to serialize proof: {}", e); + return ExitCode::FAILURE; + } + if let Err(e) = writer.flush() { eprintln!("Failed to write proof: {}", e); return ExitCode::FAILURE; } @@ -799,14 +799,16 @@ fn cmd_prove_continuation( } }; let mut writer = BufWriter::new(file); - let bytes = match bincode::serialize(&bundle) { - Ok(b) => b, - Err(e) => { - eprintln!("Failed to serialize proof: {}", e); - return ExitCode::FAILURE; - } - }; - if let Err(e) = writer.write_all(&bytes) { + // Stream the bundle straight into the writer instead of `bincode::serialize` + // into an intermediate `Vec`: that buffer holds a full serialized copy of + // the proof while `bundle` is still alive (~2x the bundle in RAM). For large + // continuation bundles (many epochs) that doubling becomes the memory peak — it + // dominates the per-epoch working set. `serialize_into` keeps the write peak at ~1x. + if let Err(e) = bincode::serialize_into(&mut writer, &bundle) { + eprintln!("Failed to serialize proof: {}", e); + return ExitCode::FAILURE; + } + if let Err(e) = writer.flush() { eprintln!("Failed to write proof: {}", e); return ExitCode::FAILURE; }