Optimize main.rs: Avoid frequent Box::clone

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-07-18 17:17:56 +10:00
parent c66d8154eb
commit bcb46cd736
No known key found for this signature in database
GPG key ID: 591C0B03040416D6

View file

@ -230,10 +230,12 @@ async fn entry() -> Result<()> {
let desired_targets = get_desired_targets(&opts.targets); let desired_targets = get_desired_targets(&opts.targets);
// Compute install directory // Compute install directory
let install_path = get_install_path(opts.install_path.as_deref()).ok_or_else(|| { let install_path: Arc<Path> = Arc::from(
get_install_path(opts.install_path.as_deref()).ok_or_else(|| {
error!("No viable install path found of specified, try `--install-path`"); error!("No viable install path found of specified, try `--install-path`");
miette!("No install path found or specified") miette!("No install path found or specified")
})?; })?,
);
debug!("Using install path: {}", install_path.display()); debug!("Using install path: {}", install_path.display());
// Create a temporary directory for downloads etc. // Create a temporary directory for downloads etc.
@ -241,6 +243,8 @@ async fn entry() -> Result<()> {
.map_err(BinstallError::from) .map_err(BinstallError::from)
.wrap_err("Creating a temporary directory failed.")?; .wrap_err("Creating a temporary directory failed.")?;
let temp_dir_path: Arc<Path> = Arc::from(temp_dir.path());
let tasks: Vec<_> = crate_names let tasks: Vec<_> = crate_names
.into_iter() .into_iter()
.map(|crate_name| { .map(|crate_name| {
@ -249,7 +253,7 @@ async fn entry() -> Result<()> {
crate_name, crate_name,
desired_targets.clone(), desired_targets.clone(),
cli_overrides.clone(), cli_overrides.clone(),
temp_dir.path().to_path_buf(), temp_dir_path.clone(),
install_path.clone(), install_path.clone(),
client.clone(), client.clone(),
)) ))
@ -264,8 +268,6 @@ async fn entry() -> Result<()> {
.as_str(), .as_str(),
); );
let temp_dir_path = Arc::from(temp_dir.path());
let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm { let tasks: Vec<_> = if !opts.dry_run && !opts.no_confirm {
let mut resolutions = Vec::with_capacity(tasks.len()); let mut resolutions = Vec::with_capacity(tasks.len());
for task in tasks { for task in tasks {
@ -383,8 +385,8 @@ async fn resolve(
crate_name: CrateName, crate_name: CrateName,
desired_targets: DesiredTargets, desired_targets: DesiredTargets,
cli_overrides: Arc<PkgOverride>, cli_overrides: Arc<PkgOverride>,
temp_dir: PathBuf, temp_dir: Arc<Path>,
install_path: PathBuf, install_path: Arc<Path>,
client: Client, client: Client,
) -> Result<Resolution> { ) -> Result<Resolution> {
info!("Installing package: '{}'", crate_name); info!("Installing package: '{}'", crate_name);
@ -471,7 +473,7 @@ async fn resolve(
meta, meta,
binaries, binaries,
bin_path.clone(), bin_path.clone(),
install_path, install_path.to_path_buf(),
)?; )?;
Ok(Resolution::Fetch { Ok(Resolution::Fetch {