From 948dbd324ea7068bdf743736918f5752be6ed5a5 Mon Sep 17 00:00:00 2001 From: Alex Lyon Date: Thu, 22 Mar 2018 04:22:34 -0700 Subject: [PATCH] yes: choose between throughput and latency at compile-time --- src/yes/Cargo.toml | 4 ++++ src/yes/yes.rs | 32 +++++++++++++++++++++----------- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/src/yes/Cargo.toml b/src/yes/Cargo.toml index 5c98a3e65..1f5a76aa9 100644 --- a/src/yes/Cargo.toml +++ b/src/yes/Cargo.toml @@ -13,6 +13,10 @@ path = "yes.rs" clap = "2.31" uucore = { path = "../uucore" } +[features] +latency = [] +default = [] + [[bin]] name = "yes" path = "../../uumain.rs" diff --git a/src/yes/yes.rs b/src/yes/yes.rs index 797511cce..cca6ec912 100644 --- a/src/yes/yes.rs +++ b/src/yes/yes.rs @@ -55,23 +55,33 @@ pub fn uumain(args: Vec) -> i32 { }; let mut buffer = [0; BUF_SIZE]; - let bytes = if string.len() < BUF_SIZE / 2 { - let mut size = 0; - while size < BUF_SIZE - string.len() { - let (_, right) = buffer.split_at_mut(size); - right[..string.len()].copy_from_slice(string.as_bytes()); - size += string.len(); - } - &buffer[..size] - } else { - string.as_bytes() - }; + let bytes = prepare_buffer(&string, &mut buffer); exec(bytes); 0 } +#[cfg(not(feature = "latency"))] +fn prepare_buffer<'a>(input: &'a str, buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] { + if input.len() < BUF_SIZE / 2 { + let mut size = 0; + while size < BUF_SIZE - input.len() { + let (_, right) = buffer.split_at_mut(size); + right[..input.len()].copy_from_slice(input.as_bytes()); + size += input.len(); + } + &buffer[..size] + } else { + input.as_bytes() + } +} + +#[cfg(feature = "latency")] +fn prepare_buffer<'a>(input: &'a str, _buffer: &'a mut [u8; BUF_SIZE]) -> &'a [u8] { + input.as_bytes() +} + pub fn exec(bytes: &[u8]) { let stdout_raw = io::stdout(); let mut stdout = stdout_raw.lock();