From 03c8295cd884b777ebc4be6f16a7494f41671709 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 26 Jul 2022 13:43:30 +1000 Subject: [PATCH 1/4] Optimize `CratesToml`: Use `Vec` instead of `BTreeSet` for storing bins. Signed-off-by: Jiahao XU --- src/binstall.rs | 3 +-- src/metafiles/v1.rs | 10 +++++----- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/binstall.rs b/src/binstall.rs index 63eb70b8..d763611d 100644 --- a/src/binstall.rs +++ b/src/binstall.rs @@ -1,4 +1,3 @@ -use std::collections::BTreeSet; use std::path::PathBuf; use crate::{metafiles, DesiredTargets, PkgOverride}; @@ -20,7 +19,7 @@ pub struct Options { /// MetaData required to update MetaFiles. pub struct MetaData { - pub bins: BTreeSet, + pub bins: Vec, pub cvs: metafiles::CrateVersionSource, pub version_req: String, pub target: String, diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index b91f715a..713c167d 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -1,5 +1,5 @@ use std::{ - collections::{BTreeMap, BTreeSet}, + collections::BTreeMap, fs::File, io::{self, Seek}, iter::IntoIterator, @@ -15,7 +15,7 @@ use crate::{cargo_home, create_if_not_exist, FileLock}; #[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct CratesToml { - v1: BTreeMap>, + v1: BTreeMap>, } impl CratesToml { @@ -38,7 +38,7 @@ impl CratesToml { Self::load_from_reader(file) } - pub fn insert(&mut self, cvs: &CrateVersionSource, bins: BTreeSet) { + pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec) { self.v1.insert(cvs.to_string(), bins); } @@ -70,7 +70,7 @@ impl CratesToml { iter: Iter, ) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator)>, { let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?; let mut c1 = Self::load_from_reader(&mut *file)?; @@ -87,7 +87,7 @@ impl CratesToml { pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator)>, { Self::append_to_path(Self::default_path()?, iter) } From 928cc657781317c23ab33eae9519a56b2bd13f1e Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 26 Jul 2022 13:44:50 +1000 Subject: [PATCH 2/4] Add new dep compact_str v0.5.2 Signed-off-by: Jiahao XU --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + 2 files changed, 22 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 565442da..a2ad1f05 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -129,6 +129,7 @@ dependencies = [ "bzip2", "cargo_toml", "clap", + "compact_str", "crates_io_api", "dirs", "embed-resource", @@ -173,6 +174,15 @@ dependencies = [ "toml", ] +[[package]] +name = "castaway" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a17ed5635fc8536268e5d4de1e22e81ac34419e5f052d4d51f4e01dcc263fcc" +dependencies = [ + "rustversion", +] + [[package]] name = "cc" version = "1.0.73" @@ -257,6 +267,17 @@ dependencies = [ "memchr", ] +[[package]] +name = "compact_str" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5857fc4f27cd0fa2cd7c4a4fa780c0da3d898ae27e66bf6a719343e219e9a559" +dependencies = [ + "castaway", + "itoa", + "ryu", +] + [[package]] name = "core-foundation" version = "0.9.3" diff --git a/Cargo.toml b/Cargo.toml index 7b8df205..cc6307f0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,6 +24,7 @@ bytes = "1.2.0" bzip2 = "0.4.3" cargo_toml = "0.11.5" clap = { version = "3.2.14", features = ["derive"] } +compact_str = "0.5.2" crates_io_api = { version = "0.8.0", default-features = false } dirs = "4.0.0" flate2 = { version = "1.0.24", default-features = false } From 834b8bb9c5b7098b9de1690cfa510a793a30a68a Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 26 Jul 2022 13:45:53 +1000 Subject: [PATCH 3/4] Enable feature serde of dep compact_str Signed-off-by: Jiahao XU --- Cargo.lock | 1 + Cargo.toml | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/Cargo.lock b/Cargo.lock index a2ad1f05..ba311b77 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -276,6 +276,7 @@ dependencies = [ "castaway", "itoa", "ryu", + "serde", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index cc6307f0..410d4a13 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -24,7 +24,7 @@ bytes = "1.2.0" bzip2 = "0.4.3" cargo_toml = "0.11.5" clap = { version = "3.2.14", features = ["derive"] } -compact_str = "0.5.2" +compact_str = { version = "0.5.2", features = ["serde"] } crates_io_api = { version = "0.8.0", default-features = false } dirs = "4.0.0" flate2 = { version = "1.0.24", default-features = false } From 2dc246c392f013525771398a1bd65d31dd994be2 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Tue, 26 Jul 2022 13:48:04 +1000 Subject: [PATCH 4/4] Optimize `CratesToml`: Use `CompactString` for bins Signed-off-by: Jiahao XU --- src/bins.rs | 5 +++-- src/binstall.rs | 4 +++- src/metafiles/v1.rs | 9 +++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/bins.rs b/src/bins.rs index d08cf15a..7240707a 100644 --- a/src/bins.rs +++ b/src/bins.rs @@ -1,13 +1,14 @@ use std::path::{Path, PathBuf}; use cargo_toml::Product; +use compact_str::CompactString; use log::debug; use serde::Serialize; use crate::{atomic_install, atomic_symlink_file, BinstallError, PkgFmt, PkgMeta, Template}; pub struct BinFile { - pub base_name: String, + pub base_name: CompactString, pub source: PathBuf, pub dest: PathBuf, pub link: PathBuf, @@ -15,7 +16,7 @@ pub struct BinFile { impl BinFile { pub fn from_product(data: &Data, product: &Product) -> Result { - let base_name = product.name.clone().unwrap(); + let base_name = CompactString::from(product.name.clone().unwrap()); let binary_ext = if data.target.contains("windows") { ".exe" diff --git a/src/binstall.rs b/src/binstall.rs index d763611d..f28c0e5d 100644 --- a/src/binstall.rs +++ b/src/binstall.rs @@ -1,5 +1,7 @@ use std::path::PathBuf; +use compact_str::CompactString; + use crate::{metafiles, DesiredTargets, PkgOverride}; mod resolve; @@ -19,7 +21,7 @@ pub struct Options { /// MetaData required to update MetaFiles. pub struct MetaData { - pub bins: Vec, + pub bins: Vec, pub cvs: metafiles::CrateVersionSource, pub version_req: String, pub target: String, diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index 713c167d..374188ba 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -6,6 +6,7 @@ use std::{ path::{Path, PathBuf}, }; +use compact_str::CompactString; use miette::Diagnostic; use serde::{Deserialize, Serialize}; use thiserror::Error; @@ -15,7 +16,7 @@ use crate::{cargo_home, create_if_not_exist, FileLock}; #[derive(Clone, Debug, Default, Deserialize, Serialize)] pub struct CratesToml { - v1: BTreeMap>, + v1: BTreeMap>, } impl CratesToml { @@ -38,7 +39,7 @@ impl CratesToml { Self::load_from_reader(file) } - pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec) { + pub fn insert(&mut self, cvs: &CrateVersionSource, bins: Vec) { self.v1.insert(cvs.to_string(), bins); } @@ -70,7 +71,7 @@ impl CratesToml { iter: Iter, ) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator)>, { let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?; let mut c1 = Self::load_from_reader(&mut *file)?; @@ -87,7 +88,7 @@ impl CratesToml { pub fn append<'a, Iter>(iter: Iter) -> Result<(), CratesTomlParseError> where - Iter: IntoIterator)>, + Iter: IntoIterator)>, { Self::append_to_path(Self::default_path()?, iter) }