Remove unnecessary clones

Summary: Follow-up to D38155922. This was done almost entirely by just following suggestions from the compiler. No behaviour changes, just fewer bit-copies in memory.

Reviewed By: lblasa

Differential Revision: D38199244

fbshipit-source-id: 777e9d833d25cb660307380ab88b12a1d4eedf09
This commit is contained in:
Pascal Hartig
2022-07-27 07:43:12 -07:00
committed by Facebook GitHub Bot
parent 6913d6f653
commit e83f3e4dc7

View File

@@ -96,8 +96,8 @@ struct PackFile {
} }
#[derive(Debug, serde::Serialize)] #[derive(Debug, serde::Serialize)]
struct PackManifest { struct PackManifest<'a> {
files: BTreeMap<PackType, PackFile>, files: BTreeMap<&'a PackType, PackFile>,
} }
fn default_progress_bar(len: u64) -> indicatif::ProgressBar { fn default_progress_bar(len: u64) -> indicatif::ProgressBar {
@@ -110,12 +110,12 @@ fn default_progress_bar(len: u64) -> indicatif::ProgressBar {
pb pb
} }
fn pack( fn pack<'a>(
platform: &Platform, platform: &'a Platform,
dist_dir: &std::path::Path, dist_dir: &'a std::path::Path,
pack_list: &PackList, pack_list: &'a PackList,
output_directory: &std::path::Path, output_directory: &'a std::path::Path,
) -> Result<Vec<(PackType, path::PathBuf)>> { ) -> Result<Vec<(&'a PackType, path::PathBuf)>> {
let pb = default_progress_bar(pack_list.0.len() as u64 * 2 - 1); let pb = default_progress_bar(pack_list.0.len() as u64 * 2 - 1);
pb.set_prefix(format!( pb.set_prefix(format!(
"{:width$}", "{:width$}",
@@ -150,9 +150,7 @@ fn pack(
tar.finish()?; tar.finish()?;
pb.inc(1); pb.inc(1);
// TODO: Consider borrowing and changing all return types instead Ok((pack_type, output_path))
// of the unnecessary clone.
Ok((pack_type.clone(), output_path))
}) })
.collect(); .collect();
@@ -270,9 +268,9 @@ fn main() -> Result<(), anyhow::Error> {
/// Takes a list of archive paths, compresses them with LZMA and returns /// Takes a list of archive paths, compresses them with LZMA and returns
/// the updated paths. /// the updated paths.
/// TODO: Remove compressed artifacts. /// TODO: Remove compressed artifacts.
fn compress_paths( fn compress_paths<'a>(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &'a [(&'a PackType, path::PathBuf)],
) -> Result<Vec<(PackType, path::PathBuf)>> { ) -> Result<Vec<(&PackType, path::PathBuf)>> {
let pb = default_progress_bar(archive_paths.len() as u64 - 1); let pb = default_progress_bar(archive_paths.len() as u64 - 1);
pb.set_prefix(format!( pb.set_prefix(format!(
"{:width$}", "{:width$}",
@@ -298,17 +296,17 @@ fn compress_paths(
let mut encoder = xz2::write::XzEncoder::new(writer, 9); let mut encoder = xz2::write::XzEncoder::new(writer, 9);
std::io::copy(&mut reader, &mut encoder)?; std::io::copy(&mut reader, &mut encoder)?;
pb.inc(1); pb.inc(1);
Ok((pack_type.clone(), output_path)) Ok((*pack_type, output_path))
}) })
.collect::<Result<Vec<(PackType, path::PathBuf)>>>()?; .collect::<Result<Vec<(&PackType, path::PathBuf)>>>()?;
pb.finish(); pb.finish();
Ok(res) Ok(res)
} }
fn manifest( fn manifest(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &[(&PackType, path::PathBuf)],
compressed_archive_paths: &Option<Vec<(PackType, path::PathBuf)>>, compressed_archive_paths: &Option<Vec<(&PackType, path::PathBuf)>>,
output_directory: &path::Path, output_directory: &path::Path,
) -> Result<path::PathBuf> { ) -> Result<path::PathBuf> {
let archive_manifest = gen_manifest(archive_paths, compressed_archive_paths)?; let archive_manifest = gen_manifest(archive_paths, compressed_archive_paths)?;
@@ -326,19 +324,19 @@ fn write_manifest(
Ok(path) Ok(path)
} }
fn gen_manifest( fn gen_manifest<'a>(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &'a [(&PackType, path::PathBuf)],
compressed_archive_paths: &Option<Vec<(PackType, path::PathBuf)>>, compressed_archive_paths: &'a Option<Vec<(&PackType, path::PathBuf)>>,
) -> Result<PackManifest> { ) -> Result<PackManifest<'a>> {
Ok(PackManifest { Ok(PackManifest {
files: gen_manifest_files(archive_paths, compressed_archive_paths)?, files: gen_manifest_files(archive_paths, compressed_archive_paths)?,
}) })
} }
fn gen_manifest_files( fn gen_manifest_files<'a>(
archive_paths: &[(PackType, path::PathBuf)], archive_paths: &'a [(&PackType, path::PathBuf)],
compressed_archive_paths: &Option<Vec<(PackType, path::PathBuf)>>, compressed_archive_paths: &'a Option<Vec<(&PackType, path::PathBuf)>>,
) -> Result<BTreeMap<PackType, PackFile>> { ) -> Result<BTreeMap<&'a PackType, PackFile>> {
use std::iter; use std::iter;
let pb = default_progress_bar((archive_paths.len() as u64 - 1) * 2); let pb = default_progress_bar((archive_paths.len() as u64 - 1) * 2);
pb.set_prefix(format!( pb.set_prefix(format!(
@@ -351,7 +349,7 @@ fn gen_manifest_files(
// of `None`. This allows us to zip it below and avoid having to rely on index // of `None`. This allows us to zip it below and avoid having to rely on index
// arithmetic. The `as _` is necessary to tell rustc to perform the casts from // arithmetic. The `as _` is necessary to tell rustc to perform the casts from
// something like a `std::iter::Map` to the `Iterator` trait. // something like a `std::iter::Map` to the `Iterator` trait.
let compressed_iter: Box<dyn Iterator<Item = Option<&(PackType, path::PathBuf)>>> = let compressed_iter: Box<dyn Iterator<Item = Option<&(&PackType, path::PathBuf)>>> =
compressed_archive_paths.as_ref().map_or_else( compressed_archive_paths.as_ref().map_or_else(
|| Box::new(iter::repeat(None)) as _, || Box::new(iter::repeat(None)) as _,
|inner| Box::new(inner.iter().map(Some)) as _, |inner| Box::new(inner.iter().map(Some)) as _,
@@ -372,7 +370,7 @@ fn gen_manifest_files(
let extrinsic_checksum = sha256_digest(&mut BufReader::new(File::open(path)?))?; let extrinsic_checksum = sha256_digest(&mut BufReader::new(File::open(path)?))?;
pb.inc(1); pb.inc(1);
Ok(( Ok((
pack_type, *pack_type,
PackFile { PackFile {
file_name: path file_name: path
.file_name() .file_name()
@@ -389,7 +387,7 @@ fn gen_manifest_files(
.fold( .fold(
BTreeMap::new(), BTreeMap::new(),
|mut acc: BTreeMap<_, _>, (pack_type, pack_file)| { |mut acc: BTreeMap<_, _>, (pack_type, pack_file)| {
acc.insert(pack_type.clone(), pack_file); acc.insert(pack_type, pack_file);
acc acc
}, },
); );
@@ -416,7 +414,7 @@ mod test {
.join("archive_a.tar"); .join("archive_a.tar");
let tmp_dir = tempdir::TempDir::new("manifest_test")?; let tmp_dir = tempdir::TempDir::new("manifest_test")?;
let archive_paths = &[(PackType::new("core"), artifact_path)]; let archive_paths = &[(&PackType::new("core"), artifact_path)];
let path = manifest(archive_paths, &None, tmp_dir.path())?; let path = manifest(archive_paths, &None, tmp_dir.path())?;
let manifest_content = std::fs::read_to_string(&path)?; let manifest_content = std::fs::read_to_string(&path)?;