Skip to content

Commit 66abf1b

Browse files
authored
More mionr optimizations (rust-lang#553)
* Optimize `BinFile::preview_bin`: Use `Path::display` instead of `to_string_lossy` to avoid allocation. * Refactor: Rm useless `AsRef::as_ref` call * Optimize `GhCrateMeta::find`: Reduce fut size by converting `Url` to `String` `Url` is much larger than `String`. * Refactor `PackageInfo::resolve`: Destruct `Meta::binstall` * Optimize `resolve::load_manifest_path`: Avoid allocation Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
1 parent 707b173 commit 66abf1b

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

crates/binstalk/src/bins.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl BinFile {
142142
pub fn preview_bin(&self) -> impl fmt::Display + '_ {
143143
LazyFormat {
144144
base_name: &self.base_name,
145-
source: self.source.file_name().unwrap().to_string_lossy(),
145+
source: Path::new(self.source.file_name().unwrap()).display(),
146146
dest: self.dest.display(),
147147
}
148148
}
@@ -249,21 +249,21 @@ impl<'c> Context<'c> {
249249
}
250250
}
251251

252-
struct LazyFormat<'a, S: fmt::Display> {
252+
struct LazyFormat<'a> {
253253
base_name: &'a str,
254-
source: S,
254+
source: path::Display<'a>,
255255
dest: path::Display<'a>,
256256
}
257257

258-
impl<S: fmt::Display> fmt::Display for LazyFormat<'_, S> {
258+
impl fmt::Display for LazyFormat<'_> {
259259
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
260260
write!(f, "{} ({} -> {})", self.base_name, self.source, self.dest)
261261
}
262262
}
263263

264-
struct OptionalLazyFormat<'a, S: fmt::Display>(Option<LazyFormat<'a, S>>);
264+
struct OptionalLazyFormat<'a>(Option<LazyFormat<'a>>);
265265

266-
impl<S: fmt::Display> fmt::Display for OptionalLazyFormat<'_, S> {
266+
impl fmt::Display for OptionalLazyFormat<'_> {
267267
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
268268
if let Some(lazy_format) = self.0.as_ref() {
269269
fmt::Display::fmt(lazy_format, f)

crates/binstalk/src/drivers/crates_io.rs

+8-7
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,14 @@ pub async fn fetch_crate_cratesio(
3333
debug!("Looking up crate information");
3434

3535
// Fetch online crate information
36-
let base_info = crates_io_api_client
37-
.get_crate(name.as_ref())
38-
.await
39-
.map_err(|err| BinstallError::CratesIoApi {
40-
crate_name: name.into(),
41-
err: Box::new(err),
42-
})?;
36+
let base_info =
37+
crates_io_api_client
38+
.get_crate(name)
39+
.await
40+
.map_err(|err| BinstallError::CratesIoApi {
41+
crate_name: name.into(),
42+
err: Box::new(err),
43+
})?;
4344

4445
// Locate matching version
4546
let version_iter = base_info.versions.iter().filter(|v| !v.yanked);

crates/binstalk/src/fetchers/gh_crate_meta.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ impl super::Fetcher for GhCrateMeta {
124124
return Ok(false);
125125
};
126126

127-
let repo = repo.as_ref().map(|u| u.as_str().trim_end_matches('/'));
127+
// Convert Option<Url> to Option<String> to reduce size of future.
128+
let repo = repo.map(String::from);
129+
let repo = repo.as_deref().map(|u| u.trim_end_matches('/'));
130+
128131
let launch_baseline_find_tasks = |pkg_fmt| {
129132
match &pkg_urls {
130133
Either::Left(pkg_url) => Either::Left(iter::once(*pkg_url)),

crates/binstalk/src/ops/resolve.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,10 @@ async fn resolve_inner(
239239
}
240240
}
241241

242-
/// * `fetcher` - `fetcher.find()` must return `Ok(true)`.
242+
/// * `fetcher` - `fetcher.find()` must have returned `Ok(true)`.
243+
///
244+
/// Can return empty Vec if all `BinFile` is optional and does not exist
245+
/// in the archive downloaded.
243246
async fn download_extract_and_verify(
244247
fetcher: &dyn Fetcher,
245248
bin_path: &Path,
@@ -278,7 +281,7 @@ async fn download_extract_and_verify(
278281
}
279282
}
280283

281-
// Verify that all the bin_files exist
284+
// Verify that all non-optional bin_files exist
282285
block_in_place(|| {
283286
let bin_files = collect_bin_files(
284287
fetcher,
@@ -430,7 +433,7 @@ impl PackageInfo {
430433
package
431434
.metadata
432435
.take()
433-
.and_then(|mut m| m.binstall.take())
436+
.and_then(|m| m.binstall)
434437
.unwrap_or_default(),
435438
manifest
436439
.bin
@@ -465,12 +468,13 @@ impl PackageInfo {
465468
pub fn load_manifest_path<P: AsRef<Path>>(
466469
manifest_path: P,
467470
) -> Result<Manifest<Meta>, BinstallError> {
471+
let manifest_path = manifest_path.as_ref();
472+
468473
block_in_place(|| {
469-
let manifest_path = manifest_path.as_ref();
470474
let manifest_path = if manifest_path.is_dir() {
471-
manifest_path.join("Cargo.toml")
475+
Cow::Owned(manifest_path.join("Cargo.toml"))
472476
} else if manifest_path.is_file() {
473-
manifest_path.into()
477+
Cow::Borrowed(manifest_path)
474478
} else {
475479
return Err(BinstallError::CargoManifestPath);
476480
};

0 commit comments

Comments
 (0)