mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-20 20:48:43 +00:00
Merge pull request #192 from NobodyXu/optimize/parallelize-target-detection
Optimize/parallelize target detection
This commit is contained in:
commit
cc8144e06d
2 changed files with 63 additions and 14 deletions
19
src/main.rs
19
src/main.rs
|
@ -209,6 +209,8 @@ async fn entry() -> Result<()> {
|
||||||
|
|
||||||
let mut uithread = UIThread::new(!opts.no_confirm);
|
let mut uithread = UIThread::new(!opts.no_confirm);
|
||||||
|
|
||||||
|
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 = 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`");
|
||||||
|
@ -258,22 +260,11 @@ async fn entry() -> Result<()> {
|
||||||
manifest.bin,
|
manifest.bin,
|
||||||
);
|
);
|
||||||
|
|
||||||
let desired_targets = {
|
|
||||||
let from_opts = opts
|
|
||||||
.targets
|
|
||||||
.as_ref()
|
|
||||||
.map(|ts| ts.split(',').map(|t| t.to_string()).collect());
|
|
||||||
|
|
||||||
if let Some(ts) = from_opts {
|
|
||||||
ts
|
|
||||||
} else {
|
|
||||||
detect_targets().await
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
let mut fetchers = MultiFetcher::default();
|
let mut fetchers = MultiFetcher::default();
|
||||||
|
|
||||||
for target in &desired_targets {
|
let desired_targets = desired_targets.get().await;
|
||||||
|
|
||||||
|
for target in desired_targets {
|
||||||
debug!("Building metadata for target: {target}");
|
debug!("Building metadata for target: {target}");
|
||||||
let mut target_meta = meta.clone();
|
let mut target_meta = meta.clone();
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,68 @@
|
||||||
use std::io::{BufRead, Cursor};
|
use std::io::{BufRead, Cursor};
|
||||||
use std::process::Output;
|
use std::process::Output;
|
||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
use tokio::process::Command;
|
use tokio::process::Command;
|
||||||
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
/// Compiled target triple, used as default for binary fetching
|
/// Compiled target triple, used as default for binary fetching
|
||||||
pub const TARGET: &str = env!("TARGET");
|
pub const TARGET: &str = env!("TARGET");
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
enum DesiredTargetsInner {
|
||||||
|
AutoDetect(Arc<OnceCell<Vec<String>>>),
|
||||||
|
Initialized(Vec<String>),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct DesiredTargets(DesiredTargetsInner);
|
||||||
|
|
||||||
|
impl DesiredTargets {
|
||||||
|
fn initialized(targets: Vec<String>) -> Self {
|
||||||
|
Self(DesiredTargetsInner::Initialized(targets))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn auto_detect() -> Self {
|
||||||
|
let arc = Arc::new(OnceCell::new());
|
||||||
|
|
||||||
|
let once_cell = arc.clone();
|
||||||
|
tokio::spawn(async move {
|
||||||
|
once_cell.get_or_init(detect_targets).await;
|
||||||
|
});
|
||||||
|
|
||||||
|
Self(DesiredTargetsInner::AutoDetect(arc))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub async fn get(&self) -> &[String] {
|
||||||
|
use DesiredTargetsInner::*;
|
||||||
|
|
||||||
|
match &self.0 {
|
||||||
|
Initialized(targets) => targets,
|
||||||
|
|
||||||
|
// This will mostly just wait for the spawned task,
|
||||||
|
// on rare occausion though, it will poll the future
|
||||||
|
// returned by `detect_targets`.
|
||||||
|
AutoDetect(once_cell) => once_cell.get_or_init(detect_targets).await,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// If opts_targets is `Some`, then it will be used.
|
||||||
|
/// Otherwise, call `detect_targets` using `tokio::spawn` to detect targets.
|
||||||
|
///
|
||||||
|
/// Since `detect_targets` internally spawns a process and wait for it,
|
||||||
|
/// it's pretty costy.
|
||||||
|
///
|
||||||
|
/// Calling it through `tokio::spawn` would enable other tasks, such as
|
||||||
|
/// fetching the crate tarballs, to be executed concurrently.
|
||||||
|
pub fn get_desired_targets(opts_targets: &Option<String>) -> DesiredTargets {
|
||||||
|
if let Some(targets) = opts_targets.as_ref() {
|
||||||
|
DesiredTargets::initialized(targets.split(',').map(|t| t.to_string()).collect())
|
||||||
|
} else {
|
||||||
|
DesiredTargets::auto_detect()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Detect the targets supported at runtime,
|
/// Detect the targets supported at runtime,
|
||||||
/// which might be different from `TARGET` which is detected
|
/// which might be different from `TARGET` which is detected
|
||||||
/// at compile-time.
|
/// at compile-time.
|
||||||
|
|
Loading…
Add table
Reference in a new issue