A modern terminal with a built-in multiplexer: close it and your sessions keep running. Run shells and tons of AI agents across your machines, then pick them up from desktop, browser, TUI and more. Written in Rust with no UI framework: interface drawn straight on the GPU. No Electron, GPUI or webview.
Rust agentagent-orchestrationaiai-agentsclaude-codecodexcoding-agentlinuxmacosmultiplayermultiplexerrustterminalterminal-emulatorterminal-multiplexertmuxtuiweztermwindows
perf(varbincode): grow large buffers without realloc
RoversX36eeb42b2153
diff --git a/codec/src/lib.rs b/codec/src/lib.rsindex 61b8c3bc..f94db12a 100644--- a/codec/src/lib.rs+++ b/codec/src/lib.rs@@ -2406,6 +2406,47 @@ mod test {assert_eq!(back, payload);}+ // varbincode is vendored outside the workspace, so what the codec+ // relies on from its `reserve` is tested here.+ #[test]+ fn large_growth_stops_at_the_known_size() {+ const STEP: usize = 64 * 1024;+ let limit = 3 * 1024 * 1024 + 5;+ let mut buf = Vec::with_capacity(STEP);+ let mut fill = 0u8;+ while buf.len() < limit {+ let chunk = (limit - buf.len()).min(STEP);+ varbincode::reserve(&mut buf, chunk, limit);+ assert!(buf.capacity() <= limit, "grew past the known size: {}", buf.capacity());+ buf.extend(std::iter::repeat(fill).take(chunk));+ fill = fill.wrapping_add(1);+ }+ assert_eq!(buf.capacity(), limit);+ for (n, chunk) in buf.chunks(STEP).enumerate() {+ assert!(chunk.iter().all(|&b| b == n as u8), "chunk {} survived the moves", n);+ }+ }++ #[test]+ fn unbounded_growth_still_doubles() {+ let mut buf = vec![1u8; 2 * 1024 * 1024];+ varbincode::reserve(&mut buf, 1, usize::MAX);+ assert!(buf.capacity() >= 4 * 1024 * 1024, "{}", buf.capacity());+ assert!(buf.iter().all(|&b| b == 1));+ }++ #[test]+ fn small_growth_stops_at_the_known_size_too() {+ let limit = 600 * 1000;+ let mut buf = Vec::with_capacity(64 * 1024);+ while buf.len() < limit {+ let chunk = (limit - buf.len()).min(64 * 1024);+ varbincode::reserve(&mut buf, chunk, limit);+ buf.extend(std::iter::repeat(3u8).take(chunk));+ }+ assert_eq!(buf.capacity(), limit);+ }+/// The wasm client's receive path: a native server compresses with the/// zstd C library, the wasm side decodes with pure-Rust ruzstd. Proven/// here on native, where both libraries are available.diff --git a/deps/varbincode/Cargo.toml b/deps/varbincode/Cargo.tomlindex ee0b82ac..044ded09 100644--- a/deps/varbincode/Cargo.toml+++ b/deps/varbincode/Cargo.toml@@ -3,7 +3,9 @@# corrupt or hostile length costs only the bytes actually present in the# stream instead of aborting the process on allocation failure; and values# may nest only so deep, so a stream of nested tags cannot run the decoder-# off its stack; aggregate decoded allocation is also bounded.+# off its stack; aggregate decoded allocation is also bounded; and large+# byte strings grow into fresh allocations rather than by realloc (`reserve`),+# or are read whole when the input is known to hold them (`with_input_bound`).[package]name = "varbincode"version = "0.1.0"diff --git a/deps/varbincode/src/de.rs b/deps/varbincode/src/de.rsindex 717fa25a..09653784 100644--- a/deps/varbincode/src/de.rs+++ b/deps/varbincode/src/de.rs@@ -13,11 +13,22 @@ pub struct Deserializer<'a> {reader: &'a mut std::io::Read,depth: usize,remaining_bytes: usize,+ /// The most the input can hold, when the caller knows it: an input+ /// already in memory. See `with_input_bound`.+ input_bound: Option<usize>,}impl<'a> Deserializer<'a> {pub fn new(reader: &'a mut std::io::Read) -> Self {- Self { reader, depth: 0, remaining_bytes: MAX_DECODED_BYTES }+ Self { reader, depth: 0, remaining_bytes: MAX_DECODED_BYTES, input_bound: None }+ }++ /// For an input of at most `bound` bytes that is already in memory: a+ /// byte string the input could hold is read whole into a buffer of its+ /// own size, rather than grown as it arrives, since its bytes are+ /// already there to bound it.+ pub fn with_input_bound(reader: &'a mut std::io::Read, bound: usize) -> Self {+ Self { input_bound: Some(bound), ..Self::new(reader) }}fn charge(&mut self, bytes: usize) -> Result<()> {@@ -51,6 +62,15 @@ impl<'a> Deserializer<'a> {fn read_vec(&mut self) -> Result<Vec<u8>> {let len: usize = serde::Deserialize::deserialize(&mut *self)?;self.charge(len)?;+ if self.input_bound.is_some_and(|bound| len <= bound) {+ use std::io::Read;+ let mut result = Vec::with_capacity(len);+ (&mut *self.reader).take(len as u64).read_to_end(&mut result)?;+ if result.len() < len {+ return Err(std::io::Error::from(std::io::ErrorKind::UnexpectedEof).into());+ }+ return Ok(result);+ }// Grow as the bytes arrive rather than trusting the declared// length: the stream, not the header, bounds the allocation.const STEP: usize = 64 * 1024;@@ -59,6 +79,7 @@ impl<'a> Deserializer<'a> {while remaining > 0 {let chunk = remaining.min(STEP);let start = result.len();+ crate::reserve(&mut result, chunk, len);result.resize(start + chunk, 0);self.reader.read_exact(&mut result[start..])?;remaining -= chunk;diff --git a/deps/varbincode/src/lib.rs b/deps/varbincode/src/lib.rsindex b274998e..4727dde3 100644--- a/deps/varbincode/src/lib.rs+++ b/deps/varbincode/src/lib.rs@@ -12,6 +12,38 @@ mod test;pub use de::Deserializer;pub use ser::Serializer;+/// Growth to this size or past it moves to a fresh allocation; see+/// `reserve`.+pub const LARGE_GROWTH: usize = 1024 * 1024;++/// Make room in `buf` for `additional` more bytes, growing as `Vec` would+/// (doubling, at least 8) but never past `limit`, which callers that know+/// the final size pass. Growth to `LARGE_GROWTH` or more moves the bytes to+/// a fresh allocation instead of `realloc` extending the block: macOS keeps+/// the large blocks such a `realloc` leaves behind rather than reusing+/// them, and picture frames decoded that way held over a gigabyte of freed+/// memory in each process. Doubling still means a buffer is copied about as+/// many bytes as it ends up holding.+pub fn reserve(buf: &mut Vec<u8>, additional: usize, limit: usize) {+ let needed = buf.len().saturating_add(additional);+ if needed <= buf.capacity() {+ return;+ }+ let capacity = buf+ .capacity()+ .saturating_mul(2)+ .max(needed)+ .max(8)+ .min(limit.max(needed));+ if capacity < LARGE_GROWTH {+ buf.reserve_exact(capacity - buf.len());+ return;+ }+ let mut grown = Vec::with_capacity(capacity);+ grown.extend_from_slice(buf);+ *buf = grown;+}+/// A convenience function for serializing a value as a byte vector/// See also `ser::Serializer`.pub fn serialize<T: serde::Serialize>(t: &T) -> Result<Vec<u8>, error::Error> {