Skip to content

Commit f4e7959

Browse files
committed
resize blocks
1 parent cf12da3 commit f4e7959

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
- Fingerprint mechanism now calculates Shannon entropy.
1818
- Lading now supports a '--json-logs' flag to output logs in structured JSON
1919
format.
20+
- Resize blocks after payload generation so that when a significant portion of
21+
the buffer is unused it frees that memory.
2022

2123
## [0.30.0]
2224
## Added

lading_payload/src/block.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -675,7 +675,16 @@ where
675675
{
676676
let mut block: Writer<BytesMut> = BytesMut::with_capacity(chunk_size as usize).writer();
677677
serializer.to_bytes(&mut rng, chunk_size as usize, &mut block)?;
678-
let bytes: Bytes = block.into_inner().freeze();
678+
let inner = block.into_inner();
679+
// When the actual block data usage is under half of its allocated capacity (chunk_size),
680+
// shrink its buffer to the actual size to avoid holding onto excess capacity.
681+
// This ensures that generators with lots of small blocks respect the total cache size, and
682+
// no block cache will hold more than 2x the total cache size in allocated buffers.
683+
let bytes: Bytes = if inner.len() < inner.capacity() / 2 {
684+
Bytes::copy_from_slice(&inner)
685+
} else {
686+
inner.freeze()
687+
};
679688
if bytes.is_empty() {
680689
// Blocks should not be empty and if they are empty this is an
681690
// error. Caller may choose to handle this however they wish, often it

0 commit comments

Comments
 (0)