Skip to content

Commit

Permalink
a faster hash for ActivationsKey
Browse files Browse the repository at this point in the history
  • Loading branch information
Eh2406 committed Dec 10, 2024
1 parent f2b4998 commit 91e6bf8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/cargo/core/resolver/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ impl ResolveOpts {
/// A key that when stord in a hash map ensures that there is only one
/// semver compatible version of each crate.
/// Find the activated version of a crate based on the name, source, and semver compatibility.
#[derive(Clone, PartialEq, Eq, Debug, Ord, PartialOrd, Hash)]
#[derive(Clone, PartialEq, Eq, Debug, Ord, PartialOrd)]
pub struct ActivationsKey(InternedString, SemverCompatibility, SourceId);

impl ActivationsKey {
Expand All @@ -176,6 +176,14 @@ impl ActivationsKey {
}
}

impl std::hash::Hash for ActivationsKey {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.0.fast_hash(state);
self.1.hash(state);
// self.2.hash(state); // Packages that only differ by SourceId are rare enough to not be worth hashing
}
}

/// A type that represents when cargo treats two Versions as compatible.
/// Versions `a` and `b` are compatible if their left-most nonzero digit is the
/// same.
Expand Down
6 changes: 6 additions & 0 deletions src/cargo/util/interning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ impl InternedString {
pub fn as_str(&self) -> &'static str {
self.inner
}

/// A faster implementation of hash that is completely compatible with HashMap,
/// but does not have a stable value between runs of the program.
pub fn fast_hash<H: std::hash::Hasher>(&self, state: &mut H) {
std::ptr::NonNull::from(self.inner).hash(state);
}
}

impl Deref for InternedString {
Expand Down

0 comments on commit 91e6bf8

Please sign in to comment.