Merge pull request #4130 from jfinkels/dd-no-allocate-count-zero

dd: don't allocate buffer if count=0
This commit is contained in:
Terts Diepraam 2022-11-13 12:05:17 +01:00 committed by GitHub
commit 46016bf9ce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<T>(
&mut self,
rstat: ReadStat,
wstat: WriteStat,
start: time::Instant,
prog_tx: &mpsc::Sender<ProgUpdate>,
output_thread: thread::JoinHandle<T>,
) -> std::io::Result<()> {
// Flush the output, if configured to do so.
self.sync()?;