Skip to content

Commit 6fceb0f

Browse files
committed
Improve `Allocation::hash
Exhaustively destructure and ignore `()`
1 parent 5a90de8 commit 6fceb0f

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

compiler/rustc_middle/src/mir/interpret/allocation.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -109,26 +109,34 @@ const MAX_HASHED_BUFFER_LEN: usize = 2 * MAX_BYTES_TO_HASH;
109109
// large.
110110
impl hash::Hash for Allocation {
111111
fn hash<H: hash::Hasher>(&self, state: &mut H) {
112+
let Self {
113+
bytes,
114+
provenance,
115+
init_mask,
116+
align,
117+
mutability,
118+
extra: _, // don't bother hashing ()
119+
} = self;
120+
112121
// Partially hash the `bytes` buffer when it is large. To limit collisions with common
113122
// prefixes and suffixes, we hash the length and some slices of the buffer.
114-
let byte_count = self.bytes.len();
123+
let byte_count = bytes.len();
115124
if byte_count > MAX_HASHED_BUFFER_LEN {
116125
// Hash the buffer's length.
117126
byte_count.hash(state);
118127

119128
// And its head and tail.
120-
self.bytes[..MAX_BYTES_TO_HASH].hash(state);
121-
self.bytes[byte_count - MAX_BYTES_TO_HASH..].hash(state);
129+
bytes[..MAX_BYTES_TO_HASH].hash(state);
130+
bytes[byte_count - MAX_BYTES_TO_HASH..].hash(state);
122131
} else {
123-
self.bytes.hash(state);
132+
bytes.hash(state);
124133
}
125134

126135
// Hash the other fields as usual.
127-
self.provenance.hash(state);
128-
self.init_mask.hash(state);
129-
self.align.hash(state);
130-
self.mutability.hash(state);
131-
self.extra.hash(state);
136+
provenance.hash(state);
137+
init_mask.hash(state);
138+
align.hash(state);
139+
mutability.hash(state);
132140
}
133141
}
134142

0 commit comments

Comments
 (0)