From 5ee568134310861e2165155e7dbe981351556a6c Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Fri, 11 Nov 2022 22:44:55 -0500 Subject: [PATCH] dd: don't allocate buffer if count=0 As an optimization, prevent `dd` from allocating buffers for use in its main loop when `count=0`. --- src/uu/dd/src/dd.rs | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/uu/dd/src/dd.rs b/src/uu/dd/src/dd.rs index f9864f7ee..c1476607b 100644 --- a/src/uu/dd/src/dd.rs +++ b/src/uu/dd/src/dd.rs @@ -401,6 +401,12 @@ where let output_thread = thread::spawn(gen_prog_updater(rx, i.settings.status)); let mut progress_as_secs = 0; + // Optimization: if no blocks are to be written, then don't + // bother allocating any buffers. + if let Some(Num::Blocks(0) | Num::Bytes(0)) = i.settings.count { + return self.finalize(rstat, wstat, start, &prog_tx, output_thread); + }; + // Create a common buffer with a capacity of the block size. // This is the max size needed. let mut buf = vec![BUF_INIT_BYTE; bsize]; @@ -439,7 +445,18 @@ where prog_tx.send(prog_update).unwrap_or(()); } } + self.finalize(rstat, wstat, start, &prog_tx, output_thread) + } + /// Flush output, print final stats, and join with the progress thread. + fn finalize( + &mut self, + rstat: ReadStat, + wstat: WriteStat, + start: time::Instant, + prog_tx: &mpsc::Sender, + output_thread: thread::JoinHandle, + ) -> std::io::Result<()> { // Flush the output, if configured to do so. self.sync()?;