From b5bd10d0d89ab9bc8ff9f36131128d9cf3a01014 Mon Sep 17 00:00:00 2001 From: "Dylan M. Taylor" Date: Sun, 15 Nov 2026 14:49:04 +0000 Subject: [PATCH] Stream envelope fields with bounded buffering --- crates/vectorcraft-format/src/stream.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/crates/vectorcraft-format/src/stream.rs b/crates/vectorcraft-format/src/stream.rs index 71b6e90eb..16bc77ee8 100644 --- a/crates/vectorcraft-format/src/stream.rs +++ b/crates/vectorcraft-format/src/stream.rs @@ -198,11 +198,15 @@ impl Envelope { Self { pretty, first: true } } - /// Serialize a complete member before writing it to the envelope. + /// Serialize a member directly to the output without buffering the full value. pub fn field(&mut self, out: &mut impl Write, key: &str, value: &impl Serialize) -> io::Result<()> { - let json = if self.pretty { serde_json::to_vec_pretty(value) } else { serde_json::to_vec(value) }.map_err(io::Error::other)?; self.key(out, key)?; - Indent { inner: out, on: self.pretty }.write_all(&json) + // serde_json emits small writes; keep compression and underlying I/O efficient while + // bounding temporary memory independently of the serialized table size. + let mut buffered = io::BufWriter::with_capacity(64 << 10, out); + let indented = Indent { inner: &mut buffered, on: self.pretty }; + if self.pretty { serde_json::to_writer_pretty(indented, value) } else { serde_json::to_writer(indented, value) }.map_err(io::Error::other)?; + buffered.into_inner().map(|_| ()).map_err(|e| e.into_error()) } /// The start of member `key` (a plain name): its value follows, indented one level when