Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lading_payload/src/apache_common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -347,13 +347,19 @@ impl crate::Serialize for ApacheCommon {
W: Write,
{
let mut bytes_remaining = max_bytes;
// Reuse a single buffer across iterations to avoid repeated allocations.
// Each log line is formatted here, measured, then written to the output.
let mut buffer: Vec<u8> = Vec::with_capacity(256);
loop {
let member: Member = self.generate(&mut rng)?;
let encoding = format!("{member}");
let line_length = encoding.len() + 1; // add one for the newline
buffer.clear();
// Format into the reusable buffer - write! on Vec<u8> is infallible
write!(&mut buffer, "{member}").expect("formatting to Vec<u8> cannot fail");
let line_length = buffer.len() + 1; // add one for the newline
match bytes_remaining.checked_sub(line_length) {
Some(remainder) => {
writeln!(writer, "{encoding}")?;
writer.write_all(&buffer)?;
writer.write_all(b"\n")?;
bytes_remaining = remainder;
}
None => break,
Expand Down
Loading