From 6f4f8e7595413bf04e4ff85cac37950d9d232968 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 26 May 2020 21:02:33 -0700 Subject: [PATCH] Improve verification performance Port the hashing performance improvements to the verifier: - Wrap file in a BufReader - Hash all read data at the same time type: performance --- src/hasher.rs | 3 +-- src/verifier.rs | 41 ++++++++++++++++++----------------------- 2 files changed, 19 insertions(+), 25 deletions(-) diff --git a/src/hasher.rs b/src/hasher.rs index ee7b0c9..cd15ad4 100644 --- a/src/hasher.rs +++ b/src/hasher.rs @@ -107,12 +107,11 @@ impl Hasher { break; } - bytes_hashed += bytes_read; - let read = &remaining[..bytes_read]; self.sha1.update(read); + bytes_hashed += bytes_read; self.piece_bytes_hashed += bytes_read; if self.piece_bytes_hashed == self.piece_length { diff --git a/src/verifier.rs b/src/verifier.rs index 51d8450..f7affe2 100644 --- a/src/verifier.rs +++ b/src/verifier.rs @@ -71,36 +71,31 @@ impl<'a> Verifier<'a> { } pub(crate) fn hash(&mut self, path: &Path) -> io::Result<()> { - let mut file = File::open(path)?; + let mut file = BufReader::new(File::open(path)?); - let mut remaining = path.metadata()?.len(); + loop { + let remaining = &mut self.buffer[..self.piece_length - self.piece_bytes_hashed]; - while remaining > 0 { - let to_buffer: usize = remaining - .min(self.buffer.len().into_u64()) - .try_into() - .invariant_unwrap("min with usize should fit in usize"); + let bytes_read = file.read(remaining)?; - let buffer = &mut self.buffer[0..to_buffer]; - - file.read_exact(buffer)?; - - for byte in buffer.iter().cloned() { - self.sha1.update(&[byte]); - - self.piece_bytes_hashed += 1; - - if self.piece_bytes_hashed == self.piece_length { - self.pieces.push(self.sha1.digest().into()); - self.sha1.reset(); - self.piece_bytes_hashed = 0; - } + if bytes_read == 0 { + break; } - remaining -= buffer.len().into_u64(); + let read = &remaining[..bytes_read]; + + self.sha1.update(read); + + self.piece_bytes_hashed += bytes_read; + + if self.piece_bytes_hashed == self.piece_length { + self.pieces.push(self.sha1.digest().into()); + self.sha1.reset(); + self.piece_bytes_hashed = 0; + } if let Some(progress_bar) = &self.progress_bar { - progress_bar.inc(to_buffer.into_u64()); + progress_bar.inc(bytes_read.into_u64()); } }