Skip to content

Commit f9be536

Browse files
committed
feat!: Reduce heap-allocations CrateVersion type and sub-types. (#26)
This improves performance slightly when dealing with a lot of versions, like when all versions are obtained from the beginning of time.
1 parent 7e9d3cd commit f9be536

File tree

4 files changed

+36
-15
lines changed

4 files changed

+36
-15
lines changed

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ test = false
1717
[dependencies]
1818
git-repository = { version = "0.28.0", default-features = false, features = ["max-performance-safe", "blocking-network-client", "blocking-http-transport"] }
1919
serde = { version = "1", features = ["std", "derive"] }
20+
hex = "0.4.3"
21+
smartstring = "1.0.1"
2022
serde_json = "1"
2123
bstr = "1.0.1"
2224
thiserror = "1.0.32"

Makefile

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,6 @@ CARGO = $(shell command -v cargo)
77
##@ Development
88

99
test: ## run all tests with cargo
10-
RUST_BACKTRACE=1 cargo test
11-
10+
RUST_BACKTRACE=1 cargo test --test crates-index-diff
11+
RUST_BACKTRACE=1 cargo test --test baseline --release
12+

src/types.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::collections::HashMap;
22

33
use git_repository as git;
4+
use smartstring::alias::String as SmolString;
45
use std::fmt;
56
use std::hash::{Hash, Hasher};
67

@@ -72,19 +73,33 @@ impl fmt::Display for Change {
7273
}
7374
}
7475

76+
/// Section in which a dependency was defined in.
77+
#[derive(
78+
Debug, Copy, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Hash, Ord, PartialOrd,
79+
)]
80+
#[serde(rename_all = "lowercase")]
81+
pub enum DependencyKind {
82+
/// Used for production builds.
83+
Normal,
84+
/// Used only for tests and examples.
85+
Dev,
86+
/// Used in build scripts.
87+
Build,
88+
}
89+
7590
/// Pack all information we know about a change made to a version of a crate.
7691
#[derive(Default, Clone, serde::Serialize, serde::Deserialize, Eq, PartialEq, Debug)]
7792
pub struct CrateVersion {
7893
/// The crate name, i.e. `clap`.
79-
pub name: String,
94+
pub name: SmolString,
8095
/// is the release yanked?
8196
pub yanked: bool,
8297
/// The semantic version of the crate.
8398
#[serde(rename = "vers")]
84-
pub version: String,
99+
pub version: SmolString,
85100
/// The checksum over the crate archive
86-
#[serde(rename = "cksum")]
87-
pub checksum: String,
101+
#[serde(rename = "cksum", with = "hex")]
102+
pub checksum: [u8; 32],
88103
/// All cargo features
89104
pub features: HashMap<String, Vec<String>>,
90105
/// All crate dependencies
@@ -109,20 +124,23 @@ impl CrateVersion {
109124
)]
110125
pub struct Dependency {
111126
/// The crate name
112-
pub name: String,
127+
pub name: SmolString,
113128
/// The version the parent crate requires of this dependency
114129
#[serde(rename = "req")]
115-
pub required_version: String,
130+
pub required_version: SmolString,
116131
/// All cargo features configured by the parent crate
117132
pub features: Vec<String>,
118133
/// True if this is an optional dependency
119134
pub optional: bool,
120135
/// True if default features are enabled
121136
pub default_features: bool,
122137
/// The name of the build target
123-
pub target: Option<String>,
138+
#[serde(skip_serializing_if = "Option::is_none")]
139+
pub target: Option<SmolString>,
124140
/// The kind of dependency, usually 'normal' or 'dev'
125-
pub kind: Option<String>,
141+
#[serde(skip_serializing_if = "Option::is_none")]
142+
pub kind: Option<DependencyKind>,
126143
/// The package this crate is contained in
127-
pub package: Option<String>,
144+
#[serde(skip_serializing_if = "Option::is_none")]
145+
pub package: Option<SmolString>,
128146
}

tests/version/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ fn parse_crate_version() {
77
r#"{
88
"name": "test",
99
"vers": "1.0.0",
10-
"cksum": "cksum",
10+
"cksum": "0000000000000000000000000000000000000000000000000000000000000000",
1111
"features" : {},
1212
"deps" : [],
1313
"yanked": true
@@ -17,12 +17,12 @@ fn parse_crate_version() {
1717
assert_eq!(
1818
c,
1919
CrateVersion {
20-
name: "test".to_string(),
20+
name: "test".into(),
2121
yanked: true,
22-
version: "1.0.0".to_string(),
22+
version: "1.0.0".into(),
2323
dependencies: Vec::new(),
2424
features: HashMap::new(),
25-
checksum: "cksum".into()
25+
checksum: Default::default()
2626
}
2727
);
2828
}

0 commit comments

Comments
 (0)