From 4a882dc2cbaf58d49aebd3d38271f6134e14a84f Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Sat, 11 Jun 2022 15:44:16 +1000 Subject: [PATCH] Use `BufRead` in`extract_compressed_from_readable` Use `BufRead` instead of `Read` to avoid copying of data and improve efficiency. `GzDecoder` and `XzDecoder` both have their `BufRead` counterpart in mod `bufread` and their `read` counterpart merely wraps the input in a `std::io::BufReader`. `ZstdDecoder::new` also wraps it in a `BufReader` and pass it to `ZstdDecoder::with_buffer`. Signed-off-by: Jiahao XU --- src/helpers/extracter.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/helpers/extracter.rs b/src/helpers/extracter.rs index fbb9d5c0..e1c91cef 100644 --- a/src/helpers/extracter.rs +++ b/src/helpers/extracter.rs @@ -1,11 +1,11 @@ use std::fs::{self, File}; -use std::io::Read; +use std::io::{BufRead, Read}; use std::path::Path; -use flate2::read::GzDecoder; +use flate2::bufread::GzDecoder; use log::debug; use tar::Archive; -use xz2::read::XzDecoder; +use xz2::bufread::XzDecoder; use zip::read::ZipArchive; use zstd::stream::Decoder as ZstdDecoder; @@ -55,7 +55,7 @@ fn untar bool>( /// Note that this is a best-effort and it only works when `fmt` /// is not `PkgFmt::Bin` or `PkgFmt::Zip`. pub(crate) fn extract_compressed_from_readable bool>( - dat: impl Read, + dat: impl BufRead, fmt: PkgFmt, path: &Path, filter: Option, @@ -89,7 +89,7 @@ pub(crate) fn extract_compressed_from_readable bool>( // as of zstd 0.10.2 and 0.11.2, which is specified // as &[] by ZstdDecoder::new, thus ZstdDecoder::new // should not return any error. - let tar = ZstdDecoder::new(dat)?; + let tar = ZstdDecoder::with_buffer(dat)?; untar(tar, path, filter)?; } PkgFmt::Zip => panic!("Unexpected PkgFmt::Zip!"),