mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-03 17:42:56 +00:00
Minor optimization (#544)
* Optimization: Rm `debug!` in `find_version` printing all version iterated obviously doesn't help much in debugging in the problem but rather just confusing. Also this makes it hard for the compiler to optimize the iterators. * Use let-else in `ManifestVisitor` * Optimize `BinFile::preview_{bin, link}` for zero-copy Return `impl Display` that lazily format instead of allocating a `String` * Optimize `infer_bin_dir_template`: Generate dir lazily * Optimize `find_version`: Lazily clone `version_req` only on err * Refactor `find_version`: Use `bool::then_some` * Add dep either v1.8.0 to binstalk * Optimize `GhCrateMeta::find`: Avoid cloning and `Vec` creation by using `Either` * Optimize `ops::install::install_from_package`: Make it a regular fn instead of async fn since it does not `.await` on any async fn. * Optimize `QuickInstall`: Rm field `target` since `Arc<Data>` already contains that field. * Optimize `Fetcher`s: Extract new struct `TargetData` so that `Data` can be shared by all fetchers, regardless of the target. * Optimize `QuickInstall`: Rm unused field `data` * Optimize `Resolution::print`: Replace branching with conditional move Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
696d8c2a82
commit
bdb4b2070d
11 changed files with 145 additions and 128 deletions
crates/binstalk/src/fetchers
|
@ -1,6 +1,7 @@
|
|||
use std::{future::Future, path::Path, sync::Arc};
|
||||
use std::{future::Future, iter, ops::Deref, path::Path, sync::Arc};
|
||||
|
||||
use compact_str::{CompactString, ToCompactString};
|
||||
use either::Either;
|
||||
use futures_util::stream::{FuturesUnordered, StreamExt};
|
||||
use once_cell::sync::OnceCell;
|
||||
use serde::Serialize;
|
||||
|
@ -19,7 +20,7 @@ use crate::{
|
|||
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
|
||||
};
|
||||
|
||||
use super::Data;
|
||||
use super::{Data, TargetData};
|
||||
|
||||
pub(crate) mod hosting;
|
||||
use hosting::RepositoryHost;
|
||||
|
@ -27,6 +28,7 @@ use hosting::RepositoryHost;
|
|||
pub struct GhCrateMeta {
|
||||
client: Client,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
resolution: OnceCell<(Url, PkgFmt)>,
|
||||
}
|
||||
|
||||
|
@ -41,7 +43,7 @@ impl GhCrateMeta {
|
|||
) -> impl Iterator<Item = impl Future<Output = FindTaskRes> + 'a> + 'a {
|
||||
// build up list of potential URLs
|
||||
let urls = pkg_fmt.extensions().iter().filter_map(move |ext| {
|
||||
let ctx = Context::from_data_with_repo(&self.data, ext, repo);
|
||||
let ctx = Context::from_data_with_repo(&self.data, &self.target_data.target, ext, repo);
|
||||
match ctx.render_url(pkg_url) {
|
||||
Ok(url) => Some(url),
|
||||
Err(err) => {
|
||||
|
@ -68,10 +70,15 @@ impl GhCrateMeta {
|
|||
|
||||
#[async_trait::async_trait]
|
||||
impl super::Fetcher for GhCrateMeta {
|
||||
fn new(client: &Client, data: &Arc<Data>) -> Arc<dyn super::Fetcher> {
|
||||
fn new(
|
||||
client: Client,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
) -> Arc<dyn super::Fetcher> {
|
||||
Arc::new(Self {
|
||||
client: client.clone(),
|
||||
data: data.clone(),
|
||||
client,
|
||||
data,
|
||||
target_data,
|
||||
resolution: OnceCell::new(),
|
||||
})
|
||||
}
|
||||
|
@ -87,20 +94,20 @@ impl super::Fetcher for GhCrateMeta {
|
|||
None
|
||||
};
|
||||
|
||||
let pkg_urls = if let Some(pkg_url) = self.data.meta.pkg_url.clone() {
|
||||
vec![pkg_url]
|
||||
let pkg_urls = if let Some(pkg_url) = self.target_data.meta.pkg_url.as_deref() {
|
||||
Either::Left(pkg_url)
|
||||
} else if let Some(repo) = repo.as_ref() {
|
||||
if let Some(pkg_urls) =
|
||||
RepositoryHost::guess_git_hosting_services(repo)?.get_default_pkg_url_template()
|
||||
{
|
||||
pkg_urls
|
||||
Either::Right(pkg_urls)
|
||||
} else {
|
||||
warn!(
|
||||
concat!(
|
||||
"Unknown repository {}, cargo-binstall cannot provide default pkg_url for it.\n",
|
||||
"Please ask the upstream to provide it for target {}."
|
||||
),
|
||||
repo, self.data.target
|
||||
repo, self.target_data.target
|
||||
);
|
||||
|
||||
return Ok(false);
|
||||
|
@ -111,7 +118,7 @@ impl super::Fetcher for GhCrateMeta {
|
|||
"Package does not specify repository, cargo-binstall cannot provide default pkg_url for it.\n",
|
||||
"Please ask the upstream to provide it for target {}."
|
||||
),
|
||||
self.data.target
|
||||
self.target_data.target
|
||||
);
|
||||
|
||||
return Ok(false);
|
||||
|
@ -119,12 +126,15 @@ impl super::Fetcher for GhCrateMeta {
|
|||
|
||||
let repo = repo.as_ref().map(|u| u.as_str().trim_end_matches('/'));
|
||||
let launch_baseline_find_tasks = |pkg_fmt| {
|
||||
pkg_urls
|
||||
.iter()
|
||||
.flat_map(move |pkg_url| self.launch_baseline_find_tasks(pkg_fmt, pkg_url, repo))
|
||||
match &pkg_urls {
|
||||
Either::Left(pkg_url) => Either::Left(iter::once(*pkg_url)),
|
||||
Either::Right(pkg_urls) => Either::Right(pkg_urls.iter().map(Deref::deref)),
|
||||
}
|
||||
.flat_map(move |pkg_url| self.launch_baseline_find_tasks(pkg_fmt, pkg_url, repo))
|
||||
};
|
||||
|
||||
let mut handles: FuturesUnordered<_> = if let Some(pkg_fmt) = self.data.meta.pkg_fmt {
|
||||
let mut handles: FuturesUnordered<_> = if let Some(pkg_fmt) = self.target_data.meta.pkg_fmt
|
||||
{
|
||||
launch_baseline_find_tasks(pkg_fmt).collect()
|
||||
} else {
|
||||
PkgFmt::iter()
|
||||
|
@ -156,7 +166,7 @@ impl super::Fetcher for GhCrateMeta {
|
|||
}
|
||||
|
||||
fn target_meta(&self) -> PkgMeta {
|
||||
let mut meta = self.data.meta.clone();
|
||||
let mut meta = self.target_data.meta.clone();
|
||||
meta.pkg_fmt = Some(self.pkg_fmt());
|
||||
meta
|
||||
}
|
||||
|
@ -185,7 +195,7 @@ impl super::Fetcher for GhCrateMeta {
|
|||
}
|
||||
|
||||
fn target(&self) -> &str {
|
||||
&self.data.target
|
||||
&self.target_data.target
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -215,6 +225,7 @@ struct Context<'c> {
|
|||
impl<'c> Context<'c> {
|
||||
pub(self) fn from_data_with_repo(
|
||||
data: &'c Data,
|
||||
target: &'c str,
|
||||
archive_suffix: &'c str,
|
||||
repo: Option<&'c str>,
|
||||
) -> Self {
|
||||
|
@ -230,12 +241,12 @@ impl<'c> Context<'c> {
|
|||
Self {
|
||||
name: &data.name,
|
||||
repo,
|
||||
target: &data.target,
|
||||
target,
|
||||
version: &data.version,
|
||||
format: archive_format,
|
||||
archive_format,
|
||||
archive_suffix,
|
||||
binary_ext: if data.target.contains("windows") {
|
||||
binary_ext: if target.contains("windows") {
|
||||
".exe"
|
||||
} else {
|
||||
""
|
||||
|
@ -244,8 +255,8 @@ impl<'c> Context<'c> {
|
|||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(self) fn from_data(data: &'c Data, archive_format: &'c str) -> Self {
|
||||
Self::from_data_with_repo(data, archive_format, data.repo.as_deref())
|
||||
pub(self) fn from_data(data: &'c Data, target: &'c str, archive_format: &'c str) -> Self {
|
||||
Self::from_data_with_repo(data, target, archive_format, data.repo.as_deref())
|
||||
}
|
||||
|
||||
pub(self) fn render_url(&self, template: &str) -> Result<Url, BinstallError> {
|
||||
|
@ -273,16 +284,13 @@ mod test {
|
|||
|
||||
#[test]
|
||||
fn defaults() {
|
||||
let meta = PkgMeta::default();
|
||||
let data = Data {
|
||||
name: "cargo-binstall".to_compact_string(),
|
||||
target: "x86_64-unknown-linux-gnu".to_string(),
|
||||
version: "1.2.3".to_compact_string(),
|
||||
repo: Some("https://github.com/ryankurte/cargo-binstall".to_string()),
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".tgz");
|
||||
let ctx = Context::from_data(&data, "x86_64-unknown-linux-gnu", ".tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(DEFAULT_PKG_URL).unwrap(),
|
||||
url("https://github.com/ryankurte/cargo-binstall/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
|
||||
|
@ -295,15 +303,12 @@ mod test {
|
|||
let meta = PkgMeta::default();
|
||||
let data = Data {
|
||||
name: "cargo-binstall".to_compact_string(),
|
||||
target: "x86_64-unknown-linux-gnu".to_string(),
|
||||
version: "1.2.3".to_compact_string(),
|
||||
repo: None,
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".tgz");
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap())
|
||||
.unwrap();
|
||||
let ctx = Context::from_data(&data, "x86_64-unknown-linux-gnu", ".tgz");
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -315,15 +320,13 @@ mod test {
|
|||
|
||||
let data = Data {
|
||||
name: "cargo-binstall".to_compact_string(),
|
||||
target: "x86_64-unknown-linux-gnu".to_string(),
|
||||
version: "1.2.3".to_compact_string(),
|
||||
repo: None,
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".tgz");
|
||||
let ctx = Context::from_data(&data, "x86_64-unknown-linux-gnu", ".tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
url("https://example.com/releases/download/v1.2.3/cargo-binstall-x86_64-unknown-linux-gnu-v1.2.3.tgz")
|
||||
);
|
||||
}
|
||||
|
@ -339,15 +342,13 @@ mod test {
|
|||
|
||||
let data = Data {
|
||||
name: "radio-sx128x".to_compact_string(),
|
||||
target: "x86_64-unknown-linux-gnu".to_string(),
|
||||
version: "0.14.1-alpha.5".to_compact_string(),
|
||||
repo: Some("https://github.com/rust-iot/rust-radio-sx128x".to_string()),
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".tgz");
|
||||
let ctx = Context::from_data(&data, "x86_64-unknown-linux-gnu", ".tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
|
||||
);
|
||||
}
|
||||
|
@ -361,15 +362,13 @@ mod test {
|
|||
|
||||
let data = Data {
|
||||
name: "radio-sx128x".to_compact_string(),
|
||||
target: "x86_64-unknown-linux-gnu".to_string(),
|
||||
version: "0.14.1-alpha.5".to_compact_string(),
|
||||
repo: Some("https://github.com/rust-iot/rust-radio-sx128x".to_string()),
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".tgz");
|
||||
let ctx = Context::from_data(&data, "x86_64-unknown-linux-gnu", ".tgz");
|
||||
assert_eq!(
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
url("https://github.com/rust-iot/rust-radio-sx128x/releases/download/v0.14.1-alpha.5/sx128x-util-x86_64-unknown-linux-gnu-v0.14.1-alpha.5.tgz")
|
||||
);
|
||||
}
|
||||
|
@ -387,15 +386,13 @@ mod test {
|
|||
|
||||
let data = Data {
|
||||
name: "cargo-watch".to_compact_string(),
|
||||
target: "aarch64-apple-darwin".to_string(),
|
||||
version: "9.0.0".to_compact_string(),
|
||||
repo: Some("https://github.com/watchexec/cargo-watch".to_string()),
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".txz");
|
||||
let ctx = Context::from_data(&data, "aarch64-apple-darwin", ".txz");
|
||||
assert_eq!(
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-apple-darwin.tar.xz")
|
||||
);
|
||||
}
|
||||
|
@ -410,15 +407,13 @@ mod test {
|
|||
|
||||
let data = Data {
|
||||
name: "cargo-watch".to_compact_string(),
|
||||
target: "aarch64-pc-windows-msvc".to_string(),
|
||||
version: "9.0.0".to_compact_string(),
|
||||
repo: Some("https://github.com/watchexec/cargo-watch".to_string()),
|
||||
meta,
|
||||
};
|
||||
|
||||
let ctx = Context::from_data(&data, ".bin");
|
||||
let ctx = Context::from_data(&data, "aarch64-pc-windows-msvc", ".bin");
|
||||
assert_eq!(
|
||||
ctx.render_url(data.meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
ctx.render_url(meta.pkg_url.as_deref().unwrap()).unwrap(),
|
||||
url("https://github.com/watchexec/cargo-watch/releases/download/v9.0.0/cargo-watch-v9.0.0-aarch64-pc-windows-msvc.exe")
|
||||
);
|
||||
}
|
||||
|
|
|
@ -15,7 +15,7 @@ use crate::{
|
|||
manifests::cargo_toml_binstall::{PkgFmt, PkgMeta},
|
||||
};
|
||||
|
||||
use super::Data;
|
||||
use super::{Data, TargetData};
|
||||
|
||||
const BASE_URL: &str = "https://github.com/alsuren/cargo-quickinstall/releases/download";
|
||||
const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
|
||||
|
@ -23,21 +23,23 @@ const STATS_URL: &str = "https://warehouse-clerk-tmp.vercel.app/api/crate";
|
|||
pub struct QuickInstall {
|
||||
client: Client,
|
||||
package: String,
|
||||
target: String,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
}
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl super::Fetcher for QuickInstall {
|
||||
fn new(client: &Client, data: &Arc<Data>) -> Arc<dyn super::Fetcher> {
|
||||
fn new(
|
||||
client: Client,
|
||||
data: Arc<Data>,
|
||||
target_data: Arc<TargetData>,
|
||||
) -> Arc<dyn super::Fetcher> {
|
||||
let crate_name = &data.name;
|
||||
let version = &data.version;
|
||||
let target = data.target.clone();
|
||||
let target = &target_data.target;
|
||||
Arc::new(Self {
|
||||
client: client.clone(),
|
||||
client,
|
||||
package: format!("{crate_name}-{version}-{target}"),
|
||||
target,
|
||||
data: data.clone(),
|
||||
target_data,
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -68,7 +70,7 @@ impl super::Fetcher for QuickInstall {
|
|||
}
|
||||
|
||||
fn target_meta(&self) -> PkgMeta {
|
||||
let mut meta = self.data.meta.clone();
|
||||
let mut meta = self.target_data.meta.clone();
|
||||
meta.pkg_fmt = Some(self.pkg_fmt());
|
||||
meta.bin_dir = Some("{ bin }{ binary-ext }".to_string());
|
||||
meta
|
||||
|
@ -87,7 +89,7 @@ impl super::Fetcher for QuickInstall {
|
|||
}
|
||||
|
||||
fn target(&self) -> &str {
|
||||
&self.target
|
||||
&self.target_data.target
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue