Skip to content

Commit 42216fd

Browse files
committed
a faster hash for ActivationsKey
1 parent f2b4998 commit 42216fd

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/cargo/core/resolver/types.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ impl ResolveOpts {
167167
/// A key that when stord in a hash map ensures that there is only one
168168
/// semver compatible version of each crate.
169169
/// Find the activated version of a crate based on the name, source, and semver compatibility.
170-
#[derive(Clone, PartialEq, Eq, Debug, Ord, PartialOrd, Hash)]
170+
#[derive(Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
171171
pub struct ActivationsKey(InternedString, SemverCompatibility, SourceId);
172172

173173
impl ActivationsKey {
@@ -176,6 +176,14 @@ impl ActivationsKey {
176176
}
177177
}
178178

179+
impl std::hash::Hash for ActivationsKey {
180+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
181+
self.0.fast_hash(state);
182+
self.1.hash(state);
183+
// self.2.hash(state); // Packages that only differ by SourceId are rare enough to not be worth hashing
184+
}
185+
}
186+
179187
/// A type that represents when cargo treats two Versions as compatible.
180188
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
181189
/// same.

src/cargo/util/interning.rs

+6
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ impl InternedString {
9090
pub fn as_str(&self) -> &'static str {
9191
self.inner
9292
}
93+
94+
/// A faster implementation of hash that is completely compatible with HashMap,
95+
/// but does not have a stable value between runs of the program.
96+
pub fn fast_hash<H: std::hash::Hasher>(&self, state: &mut H) {
97+
std::ptr::NonNull::from(self.inner).hash(state);
98+
}
9399
}
94100

95101
impl Deref for InternedString {

0 commit comments

Comments
 (0)