1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-07-29 03:57:44 +00:00

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.
This commit is contained in:
Nick Fitzgerald 2016-03-27 14:26:05 -07:00
parent 183224db15
commit f851611001

View file

@ -272,6 +272,8 @@ fn follow<T: Read>(mut reader: BufReader<T>, settings: &Settings) {
fn backwards_thru_file<F>(file: &mut File, size: u64, buf: &mut Vec<u8>, 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<F>(file: &mut File, size: u64, buf: &mut Vec<u8>, 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 {