Skip to content

Commit 5aab823

Browse files
authored
optimize Id::hash (#974)
1 parent 918d35d commit 5aab823

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/id.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use std::fmt::Debug;
2-
use std::hash::Hash;
2+
use std::hash::{Hash, Hasher};
33
use std::num::NonZeroU32;
44

55
use crate::zalsa::Zalsa;
@@ -17,7 +17,7 @@ use crate::zalsa::Zalsa;
1717
///
1818
/// As an end-user of `Salsa` you will generally not use `Id` directly,
1919
/// it is wrapped in new types.
20-
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
20+
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
2121
#[cfg_attr(feature = "persistence", derive(serde::Serialize, serde::Deserialize))]
2222
pub struct Id {
2323
index: NonZeroU32,
@@ -126,6 +126,13 @@ impl Id {
126126
}
127127
}
128128

129+
impl Hash for Id {
130+
fn hash<H: Hasher>(&self, state: &mut H) {
131+
// Convert to a `u64` to avoid dispatching multiple calls to `H::write`.
132+
state.write_u64(self.as_bits());
133+
}
134+
}
135+
129136
impl Debug for Id {
130137
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
131138
if self.generation() == 0 {

src/key.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::fmt;
1+
use std::fmt;
22

33
use crate::function::{VerifyCycleHeads, VerifyResult};
44
use crate::zalsa::{IngredientIndex, Zalsa};

0 commit comments

Comments
 (0)