diff options
| author | Banyc <[email protected]> | 2023-07-18 00:48:52 +0800 |
|---|---|---|
| committer | Jack O'Connor <[email protected]> | 2023-09-16 14:20:39 -0700 |
| commit | e0bb91564125407102af81e219399025aa2c24b9 (patch) | |
| tree | f17b2553af54a7bb800753763b0e1f12b0090f84 /b3sum/src | |
| parent | 12b368541f917d69b6169a4d895e206144acd44f (diff) | |
move file operations from b3sum to blake3
Diffstat (limited to 'b3sum/src')
| -rw-r--r-- | b3sum/src/main.rs | 58 |
1 files changed, 3 insertions, 55 deletions
diff --git a/b3sum/src/main.rs b/b3sum/src/main.rs index fd35f68..165c579 100644 --- a/b3sum/src/main.rs +++ b/b3sum/src/main.rs @@ -182,7 +182,7 @@ impl Input { } let file = File::open(path)?; if !args.no_mmap() { - if let Some(mmap) = maybe_memmap_file(&file)? { + if let Some(mmap) = blake3::file::maybe_memmap_file(&file)? { return Ok(Self::Mmap(io::Cursor::new(mmap))); } } @@ -208,12 +208,12 @@ impl Input { // one. We might implement that in the future, but since this is // the slow path anyway, it's not high priority. Self::File(file) => { - copy_wide(file, &mut hasher)?; + blake3::copy_wide(file, &mut hasher)?; } Self::Stdin => { let stdin = io::stdin(); let lock = stdin.lock(); - copy_wide(lock, &mut hasher)?; + blake3::copy_wide(lock, &mut hasher)?; } } let mut output_reader = hasher.finalize_xof(); @@ -232,58 +232,6 @@ impl Read for Input { } } -// A 16 KiB buffer is enough to take advantage of all the SIMD instruction sets -// that we support, but `std::io::copy` currently uses 8 KiB. Most platforms -// can support at least 64 KiB, and there's some performance benefit to using -// bigger reads, so that's what we use here. -fn copy_wide(mut reader: impl Read, hasher: &mut blake3::Hasher) -> io::Result<u64> { - let mut buffer = [0; 65536]; - let mut total = 0; - loop { - match reader.read(&mut buffer) { - Ok(0) => return Ok(total), - Ok(n) => { - hasher.update(&buffer[..n]); - total += n as u64; - } - Err(ref e) if e.kind() == io::ErrorKind::Interrupted => continue, - Err(e) => return Err(e), - } - } -} - -// Mmap a file, if it looks like a good idea. Return None in cases where we -// know mmap will fail, or if the file is short enough that mmapping isn't -// worth it. However, if we do try to mmap and it fails, return the error. -fn maybe_memmap_file(file: &File) -> Result<Option<memmap2::Mmap>> { - let metadata = file.metadata()?; - let file_size = metadata.len(); - Ok(if !metadata.is_file() { - // Not a real file. - None - } else if file_size > isize::max_value() as u64 { - // Too long to safely map. - // https://github.com/danburkert/memmap-rs/issues/69 - None - } else if file_size == 0 { - // Mapping an empty file currently fails. - // https://github.com/danburkert/memmap-rs/issues/72 - None - } else if file_size < 16 * 1024 { - // Mapping small files is not worth it. - None - } else { - // Explicitly set the length of the memory map, so that filesystem - // changes can't race to violate the invariants we just checked. - let map = unsafe { - memmap2::MmapOptions::new() - .len(file_size as usize) - .map(file)? - }; - Some(map) - }) -} - fn write_hex_output(mut output: blake3::OutputReader, args: &Args) -> Result<()> { // Encoding multiples of the 64 bytes is most efficient. // TODO: This computes each output block twice when the --seek argument isn't a multiple of 64. |
