diff --git a/CHANGELOG.md b/CHANGELOG.md index 491b2b6ec..9429fba3b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Fingerprint mechanism now calculates Shannon entropy. - Lading now supports a '--json-logs' flag to output logs in structured JSON format. +- Resize blocks after payload generation so that when a significant portion of + the buffer is unused it frees that memory. ## [0.30.0] ## Added diff --git a/lading_payload/src/block.rs b/lading_payload/src/block.rs index ab2c4f99d..3684b711b 100644 --- a/lading_payload/src/block.rs +++ b/lading_payload/src/block.rs @@ -675,7 +675,16 @@ where { let mut block: Writer = BytesMut::with_capacity(chunk_size as usize).writer(); serializer.to_bytes(&mut rng, chunk_size as usize, &mut block)?; - let bytes: Bytes = block.into_inner().freeze(); + let inner = block.into_inner(); + // When the actual block data usage is under half of its allocated capacity (chunk_size), + // shrink its buffer to the actual size to avoid holding onto excess capacity. + // This ensures that generators with lots of small blocks respect the total cache size, and + // no block cache will hold more than 2x the total cache size in allocated buffers. + let bytes: Bytes = if inner.len() < inner.capacity() / 2 { + Bytes::copy_from_slice(&inner) + } else { + inner.freeze() + }; if bytes.is_empty() { // Blocks should not be empty and if they are empty this is an // error. Caller may choose to handle this however they wish, often it