feat: parallel iterations and sorting using rayon

This commit is contained in:
Christof Weickhardt 2022-05-01 12:17:55 +00:00
parent 2d68a74637
commit 128ca70b78
4 changed files with 9 additions and 5 deletions

1
Cargo.lock generated
View file

@ -161,6 +161,7 @@ dependencies = [
"env_logger", "env_logger",
"flate2", "flate2",
"log", "log",
"rayon",
"reqwest", "reqwest",
"semver", "semver",
"serde", "serde",

View file

@ -28,6 +28,7 @@ crates_io_api = { version = "0.8.0", default-features = false, features = ["rust
dirs = "4.0.0" dirs = "4.0.0"
flate2 = "1.0.22" flate2 = "1.0.22"
log = "0.4.14" log = "0.4.14"
rayon = "1.5.2"
reqwest = { version = "0.11.10", features = [ "rustls-tls" ], default-features = false } reqwest = { version = "0.11.10", features = [ "rustls-tls" ], default-features = false }
semver = "1.0.7" semver = "1.0.7"
serde = { version = "1.0.136", features = [ "derive" ] } serde = { version = "1.0.136", features = [ "derive" ] }

View file

@ -1,5 +1,6 @@
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::time::Duration; use std::time::Duration;
use rayon::prelude::*;
use anyhow::{anyhow, Context}; use anyhow::{anyhow, Context};
use log::debug; use log::debug;
@ -10,7 +11,7 @@ use crates_io_api::AsyncClient;
use crate::helpers::*; use crate::helpers::*;
use crate::PkgFmt; use crate::PkgFmt;
fn find_version<'a, V: Iterator<Item = &'a str>>( fn find_version<'a, V: ParallelIterator<Item = &'a str>>(
requirement: &str, requirement: &str,
version_iter: V, version_iter: V,
) -> Result<String, anyhow::Error> { ) -> Result<String, anyhow::Error> {
@ -40,7 +41,7 @@ fn find_version<'a, V: Iterator<Item = &'a str>>(
.collect(); .collect();
// Sort by highest matching version // Sort by highest matching version
filtered.sort_by(|a, b| { filtered.par_sort_by(|a, b| {
let a = Version::parse(a).unwrap(); let a = Version::parse(a).unwrap();
let b = Version::parse(b).unwrap(); let b = Version::parse(b).unwrap();
@ -83,7 +84,7 @@ pub async fn fetch_crate_cratesio(
}; };
// Locate matching version // Locate matching version
let version_iter = base_info.versions().iter().filter_map(|v| { let version_iter = base_info.versions().par_iter().filter_map(|v| {
if !v.is_yanked() { if !v.is_yanked() {
Some(v.version()) Some(v.version())
} else { } else {
@ -105,7 +106,7 @@ pub async fn fetch_crate_cratesio(
.context("Error fetching crate information")?; .context("Error fetching crate information")?;
// Fetch information for the filtered version // Fetch information for the filtered version
let version = match crate_info.versions.iter().find(|v| v.num == version_name) { let version = match crate_info.versions.par_iter().find_first(|v| v.num == version_name) {
Some(v) => v, Some(v) => v,
None => { None => {
return Err(anyhow::anyhow!( return Err(anyhow::anyhow!(

View file

@ -6,6 +6,7 @@ use simplelog::{ColorChoice, ConfigBuilder, TermLogger, TerminalMode};
use structopt::StructOpt; use structopt::StructOpt;
use tempdir::TempDir; use tempdir::TempDir;
use tokio::process::Command; use tokio::process::Command;
use rayon::prelude::*;
use cargo_binstall::{ use cargo_binstall::{
bins, bins,
@ -275,7 +276,7 @@ async fn install_from_package(
}; };
let bin_files = binaries let bin_files = binaries
.iter() .par_iter()
.map(|p| bins::BinFile::from_product(&bin_data, p)) .map(|p| bins::BinFile::from_product(&bin_data, p))
.collect::<Result<Vec<_>, anyhow::Error>>()?; .collect::<Result<Vec<_>, anyhow::Error>>()?;