Skip to content

Commit ce124f2

Browse files
committed
optimize file read in Config::verify
`Config::verify` refactored to improve the efficiency and memory usage of file hashing. Signed-off-by: onur-ozkan <work@onurozkan.dev>
1 parent 7ed044c commit ce124f2

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

src/bootstrap/download.rs

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
env,
33
ffi::{OsStr, OsString},
44
fs::{self, File},
5-
io::{BufRead, BufReader, BufWriter, ErrorKind, Write},
5+
io::{BufRead, BufReader, BufWriter, ErrorKind, Read, Write},
66
path::{Path, PathBuf},
77
process::{Command, Stdio},
88
};
@@ -320,25 +320,41 @@ impl Config {
320320
}
321321

322322
/// Returns whether the SHA256 checksum of `path` matches `expected`.
323-
fn verify(&self, path: &Path, expected: &str) -> bool {
323+
pub(crate) fn verify(&self, path: &Path, expected: &str) -> bool {
324324
use sha2::Digest;
325325

326326
self.verbose(&format!("verifying {}", path.display()));
327+
328+
if self.dry_run() {
329+
return false;
330+
}
331+
327332
let mut hasher = sha2::Sha256::new();
328-
// FIXME: this is ok for rustfmt (4.1 MB large at time of writing), but it seems memory-intensive for rustc and larger components.
329-
// Consider using streaming IO instead?
330-
let contents = if self.dry_run() { vec![] } else { t!(fs::read(path)) };
331-
hasher.update(&contents);
332-
let found = hex::encode(hasher.finalize().as_slice());
333-
let verified = found == expected;
334-
if !verified && !self.dry_run() {
333+
334+
let mut file = t!(File::open(path));
335+
let mut buffer = [0; 4096]; // read in chunks of 4KB
336+
337+
loop {
338+
let read_len = t!(file.read(&mut buffer));
339+
// break if EOF
340+
if read_len == 0 {
341+
break;
342+
}
343+
hasher.update(&buffer[0..read_len]);
344+
}
345+
346+
let checksum = hex::encode(hasher.finalize().as_slice());
347+
let verified = checksum == expected;
348+
349+
if !verified {
335350
println!(
336351
"invalid checksum: \n\
337-
found: {found}\n\
352+
found: {checksum}\n\
338353
expected: {expected}",
339354
);
340355
}
341-
return verified;
356+
357+
verified
342358
}
343359
}
344360

0 commit comments

Comments
 (0)