From f851611001b512e516cf1bd6fb49fd3f7273abfd Mon Sep 17 00:00:00 2001 From: Nick Fitzgerald Date: Sun, 27 Mar 2016 14:26:05 -0700 Subject: [PATCH] tail: Pre-fill the buffer with zeroes Rather than fill the buffer on every file read iteration, pre-fill it with zeroes once at initialization time. --- src/tail/tail.rs | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/tail/tail.rs b/src/tail/tail.rs index d89c95a60..996740e81 100755 --- a/src/tail/tail.rs +++ b/src/tail/tail.rs @@ -272,6 +272,8 @@ fn follow(mut reader: BufReader, settings: &Settings) { fn backwards_thru_file(file: &mut File, size: u64, buf: &mut Vec, should_stop: &mut F) where F: FnMut(u8) -> bool { + assert!(buf.len() >= BLOCK_SIZE as usize); + let max_blocks_to_read = (size as f64 / BLOCK_SIZE as f64).ceil() as usize; for block_idx in 0..max_blocks_to_read { @@ -281,13 +283,6 @@ fn backwards_thru_file(file: &mut File, size: u64, buf: &mut Vec, should_ BLOCK_SIZE }; - // Ensure that the buffer is filled and zeroed, if needed. - if buf.len() < (block_size as usize) { - for _ in buf.len()..(block_size as usize) { - buf.push(0); - } - } - // Seek backwards by the next block, read the full block into // `buf`, and then seek back to the start of the block again. let pos = file.seek(SeekFrom::Current(-(block_size as i64))).unwrap(); @@ -327,7 +322,7 @@ fn bounded_tail(mut file: File, settings: &Settings) { return; } - let mut buf = Vec::with_capacity(BLOCK_SIZE as usize); + let mut buf = vec![0; BLOCK_SIZE as usize]; // Find the position in the file to start printing from. match settings.mode {