Skip to content

Commit

Permalink
Merge pull request ouch-org#155 from ouch-org/extension-heap-alloc
Browse files Browse the repository at this point in the history
extension: Use hardcoded slices instead of `Vecs` when creating an `Extension`
  • Loading branch information
marcospb19 authored Nov 3, 2021
2 parents a1c4f03 + a798d20 commit 54b5787
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 19 deletions.
4 changes: 2 additions & 2 deletions src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub fn run(args: Opts, question_policy: QuestionPolicy) -> crate::Result<()> {
} else if inp_ext
.compression_formats
.iter()
.zip(&out_ext.compression_formats)
.zip(out_ext.compression_formats.iter())
.all(|(inp, out)| inp == out)
{
let new_ext = Extension::new(
Expand Down Expand Up @@ -348,7 +348,7 @@ fn decompress_file(
// in-memory decompression/copying first.
//
// Any other Zip decompression done can take up the whole RAM and freeze ouch.
if formats.len() == 1 && *formats[0].compression_formats.as_slice() == [Zip] {
if formats.len() == 1 && *formats[0].compression_formats == [Zip] {
utils::create_dir_if_non_existent(output_dir)?;
let zip_archive = zip::ZipArchive::new(reader)?;
let _files = crate::archive::zip::unpack_archive(zip_archive, output_dir, question_policy)?;
Expand Down
36 changes: 19 additions & 17 deletions src/extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@ use self::CompressionFormat::*;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Extension {
/// One extension like "tgz" can be made of multiple CompressionFormats ([Tar, Gz])
pub compression_formats: Vec<CompressionFormat>,
pub compression_formats: &'static [CompressionFormat],
/// The input text for this extension, like "tgz", "tar" or "xz"
pub display_text: String,
}

impl Extension {
/// # Panics:
/// Will panic if `formats` is empty
pub fn new(formats: impl Into<Vec<CompressionFormat>>, text: impl Into<String>) -> Self {
let formats = formats.into();
pub fn new(formats: &'static [CompressionFormat], text: impl Into<String>) -> Self {
assert!(!formats.is_empty());
Self { compression_formats: formats, display_text: text.into() }
}
Expand Down Expand Up @@ -110,21 +109,24 @@ pub fn separate_known_extensions_from_name(mut path: &Path) -> (&Path, Vec<Exten

// While there is known extensions at the tail, grab them
while let Some(extension) = path.extension().and_then(OsStr::to_str) {
extensions.push(match extension {
"tar" => Extension::new([Tar], extension),
"tgz" => Extension::new([Tar, Gzip], extension),
"tbz" | "tbz2" => Extension::new([Tar, Bzip], extension),
"tlz4" => Extension::new([Tar, Lz4], extension),
"txz" | "tlz" | "tlzma" => Extension::new([Tar, Lzma], extension),
"tzst" => Extension::new([Tar, Zstd], ".tzst"),
"zip" => Extension::new([Zip], extension),
"bz" | "bz2" => Extension::new([Bzip], extension),
"gz" => Extension::new([Gzip], extension),
"lz4" => Extension::new([Lz4], extension),
"xz" | "lzma" | "lz" => Extension::new([Lzma], extension),
"zst" => Extension::new([Zstd], extension),
let formats: &[CompressionFormat] = match extension {
"tar" => &[Tar],
"tgz" => &[Tar, Gzip],
"tbz" | "tbz2" => &[Tar, Bzip],
"tlz4" => &[Tar, Lz4],
"txz" | "tlz" | "tlzma" => &[Tar, Lzma],
"tzst" => &[Tar, Zstd],
"zip" => &[Zip],
"bz" | "bz2" => &[Bzip],
"gz" => &[Gzip],
"lz4" => &[Lz4],
"xz" | "lzma" | "lz" => &[Lzma],
"zst" => &[Zstd],
_ => break,
});
};

let extension = Extension::new(formats, extension);
extensions.push(extension);

// Update for the next iteration
path = if let Some(stem) = path.file_stem() { Path::new(stem) } else { Path::new("") };
Expand Down

0 comments on commit 54b5787

Please sign in to comment.