Optimizations plus bug fix (#541)

* Optimize `Download::and_extract`: Avoid dup monomorphization
* Increase buffer size for binstall_crates_v1 to `4096 * 5`
* Optimize `opts::resolve`: Avoid unnecessary `clone`s
* Fix reserve in `opts::resolve`: Do not over-reserve
* Rename field `opts::Options::resolver` => `resolvers`
* Refactor: Extract new type `resolve::PackageInfo`
    - which makes `opts::resolve_inner` easier to understand
    - reduce number of parameters required for `download_extract_and_verify` and
      `collect_bin_files`
    - reducing size of future returned by `opts::resolve_inner` by dropping
      `cargo_toml::{Manifest, Package}` as early as possible since
      `Manifest` is 3000 Bytes large while `Package` is 600 Bytes large.
* Optimize `fetchers::Data`: Use `CompactString` for field name & version
   since they are usually small enough to fit in inlined version of
   `CompactString`.
* Optimize `gh_crate_meta`: Avoid unnecessary allocation
   in `RepositoryHost::get_default_pkg_url_template`.
* Refacator: Use `Itertools::cartesian_product` in `apply_filenames_to_paths`
* Optimize `ops::resolve`: Avoid unnecessary `clone` & reduce future size
   by calling `fetcher.target_meta()` to obtain final metadata after
   downloaded and extracted the binaries.
* Optimize `ops::resolve`: Avoid unnecessary allocation
   in `download_extract_and_verify`: Replace `Itertools::join` with
   `Itertools::format` to avoid allocating the string.
* Fix disabling cargo-install fallback
* Simplify `BinFile::from_product`: Takes `&str` instead of `&Product`
   since we only need `product.name`
* Rename `BinFile::from_product` => `BinFile::new`
* Refactor: Create newtype `ops::resolve::Bin`
   so that we don't need to `unwrap()` on `Product::name`
   and reduce memory usage.

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
Jiahao XU 2022-11-18 10:59:35 +11:00 committed by GitHub
parent bb1f51a739
commit 325cb5cc19
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 220 additions and 160 deletions

View file

@ -263,6 +263,7 @@ mod test {
use crate::manifests::cargo_toml_binstall::{PkgFmt, PkgMeta};
use super::{super::Data, Context};
use compact_str::ToCompactString;
use url::Url;
const DEFAULT_PKG_URL: &str = "{ repo }/releases/download/v{ version }/{ name }-{ target }-v{ version }.{ archive-format }";
@ -275,9 +276,9 @@ mod test {
fn defaults() {
let meta = PkgMeta::default();
let data = Data {
name: "cargo-binstall".to_string(),
name: "cargo-binstall".to_compact_string(),
target: "x86_64-unknown-linux-gnu".to_string(),
version: "1.2.3".to_string(),
version: "1.2.3".to_compact_string(),
repo: Some("https://github.com/ryankurte/cargo-binstall".to_string()),
meta,
};
@ -294,9 +295,9 @@ mod test {
fn no_repo() {
let meta = PkgMeta::default();
let data = Data {
name: "cargo-binstall".to_string(),
name: "cargo-binstall".to_compact_string(),
target: "x86_64-unknown-linux-gnu".to_string(),
version: "1.2.3".to_string(),
version: "1.2.3".to_compact_string(),
repo: None,
meta,
};
@ -314,9 +315,9 @@ mod test {
};
let data = Data {
name: "cargo-binstall".to_string(),
name: "cargo-binstall".to_compact_string(),
target: "x86_64-unknown-linux-gnu".to_string(),
version: "1.2.3".to_string(),
version: "1.2.3".to_compact_string(),
repo: None,
meta,
};
@ -338,9 +339,9 @@ mod test {
};
let data = Data {
name: "radio-sx128x".to_string(),
name: "radio-sx128x".to_compact_string(),
target: "x86_64-unknown-linux-gnu".to_string(),
version: "0.14.1-alpha.5".to_string(),
version: "0.14.1-alpha.5".to_compact_string(),
repo: Some("https://github.com/rust-iot/rust-radio-sx128x".to_string()),
meta,
};
@ -360,9 +361,9 @@ mod test {
};
let data = Data {
name: "radio-sx128x".to_string(),
name: "radio-sx128x".to_compact_string(),
target: "x86_64-unknown-linux-gnu".to_string(),
version: "0.14.1-alpha.5".to_string(),
version: "0.14.1-alpha.5".to_compact_string(),
repo: Some("https://github.com/rust-iot/rust-radio-sx128x".to_string()),
meta,
};
@ -386,9 +387,9 @@ mod test {
};
let data = Data {
name: "cargo-watch".to_string(),
name: "cargo-watch".to_compact_string(),
target: "aarch64-apple-darwin".to_string(),
version: "9.0.0".to_string(),
version: "9.0.0".to_compact_string(),
repo: Some("https://github.com/watchexec/cargo-watch".to_string()),
meta,
};
@ -409,9 +410,9 @@ mod test {
};
let data = Data {
name: "cargo-watch".to_string(),
name: "cargo-watch".to_compact_string(),
target: "aarch64-pc-windows-msvc".to_string(),
version: "9.0.0".to_string(),
version: "9.0.0".to_compact_string(),
repo: Some("https://github.com/watchexec/cargo-watch".to_string()),
meta,
};

View file

@ -1,3 +1,4 @@
use itertools::Itertools;
use url::Url;
use crate::errors::BinstallError;
@ -53,6 +54,7 @@ impl RepositoryHost {
"{ repo }/releases/download/v{ version }",
],
&[FULL_FILENAMES, NOVERSION_FILENAMES],
"",
)),
GitLab => Some(apply_filenames_to_paths(
&[
@ -60,32 +62,31 @@ impl RepositoryHost {
"{ repo }/-/releases/v{ version }/downloads/binaries",
],
&[FULL_FILENAMES, NOVERSION_FILENAMES],
"",
)),
BitBucket => Some(apply_filenames_to_paths(
&["{ repo }/downloads"],
&[FULL_FILENAMES],
"",
)),
SourceForge => Some(apply_filenames_to_paths(
&[
"{ repo }/files/binaries/{ version }",
"{ repo }/files/binaries/v{ version }",
],
&[FULL_FILENAMES, NOVERSION_FILENAMES],
"/download",
)),
SourceForge => Some(
apply_filenames_to_paths(
&[
"{ repo }/files/binaries/{ version }",
"{ repo }/files/binaries/v{ version }",
],
&[FULL_FILENAMES, NOVERSION_FILENAMES],
)
.into_iter()
.map(|url| format!("{url}/download"))
.collect(),
),
Unknown => None,
}
}
}
fn apply_filenames_to_paths(paths: &[&str], filenames: &[&[&str]]) -> Vec<String> {
fn apply_filenames_to_paths(paths: &[&str], filenames: &[&[&str]], suffix: &str) -> Vec<String> {
filenames
.iter()
.flat_map(|fs| fs.iter())
.flat_map(|filename| paths.iter().map(move |path| format!("{path}/{filename}")))
.cartesian_product(paths.iter())
.map(|(filename, path)| format!("{path}/{filename}{suffix}"))
.collect()
}