Skip to content

Commit 3be453d

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 <[email protected]>
1 parent 7ed044c commit 3be453d

File tree

1 file changed

+28
-10
lines changed

1 file changed

+28
-10
lines changed

Diff for: src/bootstrap/download.rs

+28-10
Original file line numberDiff line numberDiff line change
@@ -320,25 +320,43 @@ 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 file = t!(File::open(path));
335+
let mut reader = BufReader::new(file);
336+
337+
loop {
338+
let buffer = t!(reader.fill_buf());
339+
let l = buffer.len();
340+
// break if EOF
341+
if l == 0 {
342+
break;
343+
}
344+
hasher.update(buffer);
345+
reader.consume(l);
346+
}
347+
348+
let checksum = hex::encode(hasher.finalize().as_slice());
349+
let verified = checksum == expected;
350+
351+
if !verified {
335352
println!(
336353
"invalid checksum: \n\
337-
found: {found}\n\
354+
found: {checksum}\n\
338355
expected: {expected}",
339356
);
340357
}
341-
return verified;
358+
359+
verified
342360
}
343361
}
344362

0 commit comments

Comments
 (0)