Skip to content

Commit 5e8d407

Browse files
authored
Rollup merge of rust-lang#45263 - Manishearth:hashmap-clean, r=bluss
Do some cleanups for hashmaps @mystor noticed some things whilst reading through the hashmap RawTable code. Firstly, in RawTable we deal with this hash_offset value that is the offset of the list of hashes from the buffer start. This is always zero, and this isn't consistently used (which means that we would have bugs if we set it to something else). We should just remove this since it doesn't help us at all. Secondly, the probing length tag is not copied when cloning a raw table. This is minor and basically means we do a bit more work than we need on further inserts on a cloned hashmap. r? @gankro
2 parents 34a286b + e8e7715 commit 5e8d407

File tree

1 file changed

+16
-16
lines changed

1 file changed

+16
-16
lines changed

src/libstd/collections/hash/table.rs

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -717,26 +717,25 @@ fn calculate_offsets(hashes_size: usize,
717717
(pairs_offset, end_of_pairs, oflo)
718718
}
719719

720-
// Returns a tuple of (minimum required malloc alignment, hash_offset,
720+
// Returns a tuple of (minimum required malloc alignment,
721721
// array_size), from the start of a mallocated array.
722722
fn calculate_allocation(hash_size: usize,
723723
hash_align: usize,
724724
pairs_size: usize,
725725
pairs_align: usize)
726-
-> (usize, usize, usize, bool) {
727-
let hash_offset = 0;
726+
-> (usize, usize, bool) {
728727
let (_, end_of_pairs, oflo) = calculate_offsets(hash_size, pairs_size, pairs_align);
729728

730729
let align = cmp::max(hash_align, pairs_align);
731730

732-
(align, hash_offset, end_of_pairs, oflo)
731+
(align, end_of_pairs, oflo)
733732
}
734733

735734
#[test]
736735
fn test_offset_calculation() {
737-
assert_eq!(calculate_allocation(128, 8, 16, 8), (8, 0, 144, false));
738-
assert_eq!(calculate_allocation(3, 1, 2, 1), (1, 0, 5, false));
739-
assert_eq!(calculate_allocation(6, 2, 12, 4), (4, 0, 20, false));
736+
assert_eq!(calculate_allocation(128, 8, 16, 8), (8, 144, false));
737+
assert_eq!(calculate_allocation(3, 1, 2, 1), (1, 5, false));
738+
assert_eq!(calculate_allocation(6, 2, 12, 4), (4, 20, false));
740739
assert_eq!(calculate_offsets(128, 15, 4), (128, 143, false));
741740
assert_eq!(calculate_offsets(3, 2, 4), (4, 6, false));
742741
assert_eq!(calculate_offsets(6, 12, 4), (8, 20, false));
@@ -768,10 +767,10 @@ impl<K, V> RawTable<K, V> {
768767
// This is great in theory, but in practice getting the alignment
769768
// right is a little subtle. Therefore, calculating offsets has been
770769
// factored out into a different function.
771-
let (alignment, hash_offset, size, oflo) = calculate_allocation(hashes_size,
772-
align_of::<HashUint>(),
773-
pairs_size,
774-
align_of::<(K, V)>());
770+
let (alignment, size, oflo) = calculate_allocation(hashes_size,
771+
align_of::<HashUint>(),
772+
pairs_size,
773+
align_of::<(K, V)>());
775774
assert!(!oflo, "capacity overflow");
776775

777776
// One check for overflow that covers calculation and rounding of size.
@@ -784,7 +783,7 @@ impl<K, V> RawTable<K, V> {
784783
let buffer = Heap.alloc(Layout::from_size_align(size, alignment).unwrap())
785784
.unwrap_or_else(|e| Heap.oom(e));
786785

787-
let hashes = buffer.offset(hash_offset as isize) as *mut HashUint;
786+
let hashes = buffer as *mut HashUint;
788787

789788
RawTable {
790789
capacity_mask: capacity.wrapping_sub(1),
@@ -1157,6 +1156,7 @@ impl<K: Clone, V: Clone> Clone for RawTable<K, V> {
11571156
}
11581157

11591158
new_ht.size = self.size();
1159+
new_ht.set_tag(self.tag());
11601160

11611161
new_ht
11621162
}
@@ -1183,10 +1183,10 @@ unsafe impl<#[may_dangle] K, #[may_dangle] V> Drop for RawTable<K, V> {
11831183

11841184
let hashes_size = self.capacity() * size_of::<HashUint>();
11851185
let pairs_size = self.capacity() * size_of::<(K, V)>();
1186-
let (align, _, size, oflo) = calculate_allocation(hashes_size,
1187-
align_of::<HashUint>(),
1188-
pairs_size,
1189-
align_of::<(K, V)>());
1186+
let (align, size, oflo) = calculate_allocation(hashes_size,
1187+
align_of::<HashUint>(),
1188+
pairs_size,
1189+
align_of::<(K, V)>());
11901190

11911191
debug_assert!(!oflo, "should be impossible");
11921192

0 commit comments

Comments
 (0)