#[cfg(test)]
mod tests {
#![allow(
clippy::pedantic,
clippy::nursery,
clippy::expect_fun_call,
clippy::map_unwrap_or,
clippy::cast_possible_wrap,
clippy::future_not_send
)]
use crate::bytes::{Bytes, BytesMut};
use std::time::Instant;
#[cfg(feature = "test-internals")]
use crate::bytes::profiling::{get_allocation_metrics, reset_allocation_metrics};
fn diagnostics_enabled() -> bool {
std::env::var("BYTES_PROFILING").is_ok()
}
#[test]
fn hotpath_bytes_mut_incremental_growth() {
const CHUNKS: usize = 100;
const CHUNK_SIZE: usize = 256;
const TOTAL: usize = CHUNKS * CHUNK_SIZE;
#[cfg(feature = "test-internals")]
reset_allocation_metrics();
let start = Instant::now();
let mut buf = BytesMut::new();
let initial_capacity = buf.capacity();
for i in 0..CHUNKS {
let chunk = vec![i as u8; CHUNK_SIZE];
buf.put_slice(&chunk);
}
let no_prealloc_duration = start.elapsed();
assert_eq!(buf.len(), TOTAL, "no-prealloc final length mismatch");
assert!(
buf.capacity() > initial_capacity,
"no-prealloc capacity must have grown at least once \
(initial={initial_capacity}, final={})",
buf.capacity()
);
let start = Instant::now();
let mut buf2 = BytesMut::with_capacity(TOTAL);
let initial_ptr = buf2.as_ptr();
let initial_capacity2 = buf2.capacity();
for i in 0..CHUNKS {
let chunk = vec![i as u8; CHUNK_SIZE];
buf2.put_slice(&chunk);
}
let prealloc_duration = start.elapsed();
assert_eq!(buf2.len(), TOTAL, "prealloc final length mismatch");
assert_eq!(
buf2.capacity(),
initial_capacity2,
"prealloc capacity must not have grown — \
this is the regression guard against allocator-vs-amortized \
growth heuristics"
);
assert!(
std::ptr::eq(buf2.as_ptr(), initial_ptr),
"prealloc backing pointer must be stable (no realloc happened)"
);
assert_eq!(
&buf[..],
&buf2[..],
"no-prealloc and prealloc paths must produce identical bytes"
);
if diagnostics_enabled() {
println!("BytesMut growth (no prealloc): {no_prealloc_duration:?} for {TOTAL} bytes");
println!("BytesMut growth (with prealloc): {prealloc_duration:?} for {TOTAL} bytes");
}
#[cfg(feature = "test-internals")]
if diagnostics_enabled() {
let metrics = get_allocation_metrics();
println!("Allocation metrics: {metrics:?}");
}
}
#[test]
fn hotpath_bytes_mut_splitting() {
const TOTAL: usize = 32_768;
const FRAME: usize = 1_024;
const FILL: u8 = 0x42;
#[cfg(feature = "test-internals")]
reset_allocation_metrics();
let start = Instant::now();
let mut buf = BytesMut::with_capacity(TOTAL);
buf.resize(TOTAL, FILL);
assert_eq!(buf.len(), TOTAL);
let original_len = buf.len();
let mut frames = Vec::new();
while buf.len() >= FRAME {
let frame = buf.split_to(FRAME);
assert_eq!(
frame.len(),
FRAME,
"split_to must yield exactly the requested frame size"
);
assert!(
frame.iter().all(|&b| b == FILL),
"frame must preserve original payload byte-for-byte"
);
frames.push(frame);
}
let split_to_duration = start.elapsed();
let bytes_in_frames: usize = frames.iter().map(crate::bytes::BytesMut::len).sum();
assert_eq!(
bytes_in_frames + buf.len(),
original_len,
"split_to must conserve bytes — frames + remaining must \
equal the original length (no copies, no losses)"
);
assert_eq!(
frames.len(),
TOTAL / FRAME,
"expected exactly {} frames, got {}",
TOTAL / FRAME,
frames.len()
);
if diagnostics_enabled() {
println!(
"split_to operations: {split_to_duration:?} for {} frames",
frames.len()
);
}
#[cfg(feature = "test-internals")]
if diagnostics_enabled() {
let metrics = get_allocation_metrics();
println!("Split allocation metrics: {metrics:?}");
}
}
#[test]
fn hotpath_bytes_creation() {
const PAYLOAD: usize = 4_096;
#[cfg(feature = "test-internals")]
reset_allocation_metrics();
let test_data: Vec<u8> = (0..PAYLOAD).map(|i| (i * 31 + 7) as u8).collect();
let start = Instant::now();
let mut last_copy: Option<Bytes> = None;
for _ in 0..1_000 {
last_copy = Some(Bytes::copy_from_slice(&test_data));
}
let copy_duration = start.elapsed();
let copy_sample = last_copy.expect("loop ran");
assert_eq!(copy_sample.len(), PAYLOAD);
assert_eq!(©_sample[..], &test_data[..]);
let start = Instant::now();
let mut last_from_vec: Option<Bytes> = None;
for _ in 0..1_000 {
let vec = test_data.clone();
last_from_vec = Some(Bytes::from(vec));
}
let from_vec_duration = start.elapsed();
let from_vec_sample = last_from_vec.expect("loop ran");
assert_eq!(from_vec_sample.len(), PAYLOAD);
assert_eq!(&from_vec_sample[..], &test_data[..]);
let start = Instant::now();
let mut last_freeze: Option<Bytes> = None;
for _ in 0..1_000 {
let mut buf = BytesMut::with_capacity(PAYLOAD);
buf.extend_from_slice(&test_data);
last_freeze = Some(buf.freeze());
}
let freeze_duration = start.elapsed();
let freeze_sample = last_freeze.expect("loop ran");
assert_eq!(freeze_sample.len(), PAYLOAD);
assert_eq!(&freeze_sample[..], &test_data[..]);
assert_eq!(©_sample[..], &from_vec_sample[..]);
assert_eq!(©_sample[..], &freeze_sample[..]);
if diagnostics_enabled() {
println!("Bytes::copy_from_slice: {copy_duration:?} for 1000x{PAYLOAD}B");
println!("Bytes::from(Vec): {from_vec_duration:?} for 1000x{PAYLOAD}B");
println!("BytesMut::freeze: {freeze_duration:?} for 1000x{PAYLOAD}B");
}
#[cfg(feature = "test-internals")]
if diagnostics_enabled() {
let metrics = get_allocation_metrics();
println!("Creation allocation metrics: {metrics:?}");
}
}
#[test]
fn hotpath_realistic_workload() {
const ITERATIONS: usize = 100;
#[cfg(feature = "test-internals")]
reset_allocation_metrics();
let start = Instant::now();
let mut frozen_responses: Vec<Bytes> = Vec::with_capacity(ITERATIONS);
for req_num in 0..ITERATIONS {
let mut request_buf = BytesMut::with_capacity(2_048);
for chunk_num in 0..10 {
let chunk = format!("Header-{req_num}-{chunk_num}: value\r\n");
request_buf.put_slice(chunk.as_bytes());
}
request_buf.put_slice(b"\r\n");
let header_end = request_buf[..]
.windows(4)
.position(|w| w == b"\r\n\r\n")
.map(|p| p + 4)
.expect("end-of-headers boundary must be present");
let headers = request_buf.split_to(header_end);
assert_eq!(
headers.len(),
header_end,
"split_to must consume exactly the header bytes"
);
let header_bytes = headers.freeze();
assert_eq!(header_bytes.len(), header_end);
let marker = format!("Header-{req_num}-0:");
assert!(
header_bytes
.windows(marker.len())
.any(|w| w == marker.as_bytes()),
"frozen header buffer must contain iteration marker {marker:?}"
);
let mut response_buf = BytesMut::with_capacity(1_024);
response_buf.put_slice(b"HTTP/1.1 200 OK\r\n");
response_buf.put_slice(b"Content-Type: application/json\r\n\r\n");
response_buf.put_slice(format!(r#"{{"request":{req_num},"status":"ok"}}"#).as_bytes());
let response_bytes = response_buf.freeze();
assert!(
!response_bytes.is_empty(),
"every response must be non-empty"
);
frozen_responses.push(response_bytes);
}
assert_eq!(
frozen_responses.len(),
ITERATIONS,
"all {ITERATIONS} iterations must have produced a frozen response"
);
let workload_duration = start.elapsed();
if diagnostics_enabled() {
println!("Realistic workload: {workload_duration:?} for {ITERATIONS} requests");
}
#[cfg(feature = "test-internals")]
if diagnostics_enabled() {
let final_metrics = get_allocation_metrics();
println!("Final allocation metrics: {final_metrics:?}");
}
}
}