1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 // ignore-lexer-test FIXME #15883
15 use hash::{Hash, Hasher};
16 use iter::{Iterator, count};
17 use mem::{min_align_of, size_of};
19 use num::{CheckedMul, is_power_of_two};
20 use ops::{Deref, DerefMut, Drop};
21 use option::{Some, None, Option};
26 use rt::heap::{allocate, deallocate};
28 static EMPTY_BUCKET: u64 = 0u64;
30 /// The raw hashtable, providing safe-ish access to the unzipped and highly
31 /// optimized arrays of hashes, keys, and values.
33 /// This design uses less memory and is a lot faster than the naive
34 /// `Vec<Option<u64, K, V>>`, because we don't pay for the overhead of an
35 /// option on every element, and we get a generally more cache-aware design.
37 /// Key invariants of this structure:
39 /// - if hashes[i] == EMPTY_BUCKET, then keys[i] and vals[i] have
40 /// 'undefined' contents. Don't read from them. This invariant is
41 /// enforced outside this module with the `EmptyIndex`, `FullIndex`,
42 /// and `SafeHash` types.
44 /// - An `EmptyIndex` is only constructed for a bucket at an index with
45 /// a hash of EMPTY_BUCKET.
47 /// - A `FullIndex` is only constructed for a bucket at an index with a
48 /// non-EMPTY_BUCKET hash.
50 /// - A `SafeHash` is only constructed for non-`EMPTY_BUCKET` hash. We get
51 /// around hashes of zero by changing them to 0x8000_0000_0000_0000,
52 /// which will likely map to the same bucket, while not being confused
55 /// - All three "arrays represented by pointers" are the same length:
56 /// `capacity`. This is set at creation and never changes. The arrays
57 /// are unzipped to save space (we don't have to pay for the padding
58 /// between odd sized elements, such as in a map from u64 to u8), and
59 /// be more cache aware (scanning through 8 hashes brings in 2 cache
60 /// lines, since they're all right beside each other).
62 /// You can kind of think of this module/data structure as a safe wrapper
63 /// around just the "table" part of the hashtable. It enforces some
64 /// invariants at the type level and employs some performance trickery,
65 /// but in general is just a tricked out `Vec<Option<u64, K, V>>`.
69 /// Feb 11, 2014: This hashtable was just implemented, and, hard as I tried,
70 /// isn't yet totally safe. There's a "known exploit" that you can create
71 /// multiple FullIndexes for a bucket, `take` one, and then still `take`
72 /// the other causing undefined behavior. Currently, there's no story
73 /// for how to protect against this statically. Therefore, there are asserts
74 /// on `take`, `get`, `get_mut`, and `put` which check the bucket state.
75 /// With time, and when we're confident this works correctly, they should
76 /// be removed. Also, the bounds check in `peek` is especially painful,
77 /// as that's called in the innermost loops of the hashtable and has the
78 /// potential to be a major performance drain. Remove this too.
80 /// Or, better than remove, only enable these checks for debug builds.
81 /// There's currently no "debug-only" asserts in rust, so if you're reading
82 /// this and going "what? of course there are debug-only asserts!", then
83 /// please make this use them!
84 #[unsafe_no_drop_flag]
85 pub struct RawTable<K, V> {
91 /// A bucket that holds a reference to the table
92 pub trait BucketWithTable<M> {
93 /// A bucket that holds a reference to the table
94 fn table<'a>(&'a self) -> &'a M;
96 /// Move out the reference to the table.
97 fn into_table(self) -> M;
99 /// Get the raw index.
100 fn index(&self) -> uint;
103 struct RawBucket<K, V> {
109 pub struct Bucket<K, V, M> {
110 raw: RawBucket<K, V>,
115 pub struct EmptyBucket<K, V, M> {
116 raw: RawBucket<K, V>,
121 pub struct FullBucket<K, V, M> {
122 raw: RawBucket<K, V>,
127 pub type EmptyBucketImm<'table,K,V> = EmptyBucket<K, V, &'table RawTable<K,V>>;
128 pub type FullBucketImm<'table,K,V> = FullBucket<K, V, &'table RawTable<K,V>>;
130 pub type EmptyBucketMut<'table,K,V> = EmptyBucket<K, V, &'table mut RawTable<K,V>>;
131 pub type FullBucketMut<'table,K,V> = FullBucket<K, V, &'table mut RawTable<K,V>>;
133 struct GapThenFull<K, V, M> {
134 gap: EmptyBucket<K, V, ()>,
135 full: FullBucket<K, V, M>
138 impl<K, V, M: Deref<RawTable<K,V>>> GapThenFull<K, V, M> {
139 pub fn full<'a>(&'a self) -> &'a FullBucket<K, V, M> {
143 pub fn shift(mut self) -> Option<GapThenFull<K, V, M>> {
145 *self.gap.raw.hash = mem::replace(&mut *self.full.raw.hash, EMPTY_BUCKET);
146 mem::overwrite(self.gap.raw.key, ptr::read(self.full.raw.key as *const K));
147 mem::overwrite(self.gap.raw.val, ptr::read(self.full.raw.val as *const V));
150 let FullBucket { raw, idx, .. } = self.full;
152 match self.full.next().peek() {
159 self.full.idx &= self.full.table.capacity - 1;
167 impl<K, V> RawPtr<u64> for RawBucket<K, V> {
168 unsafe fn offset(self, count: int) -> RawBucket<K, V> {
170 hash: self.hash.offset(count),
171 key: self.key.offset(count),
172 val: self.val.offset(count),
176 fn null() -> RawBucket<K, V> {
178 hash: RawPtr::null(),
184 fn is_null(&self) -> bool {
188 fn to_uint(&self) -> uint {
192 unsafe fn to_option(&self) -> Option<&u64> {
193 self.hash.to_option()
197 impl<K, V, M: Deref<RawTable<K,V>>> EmptyBucket<K, V, M> {
198 pub fn next(self) -> Bucket<K, V, M> {
199 let mut bucket = self.into_bucket();
204 pub fn into_bucket(self) -> Bucket<K, V, M> {
212 pub fn gap_peek(self) -> Option<GapThenFull<K, V, M>> {
213 let gap = EmptyBucket {
219 match self.next().peek() {
231 impl<K, V, M: DerefMut<RawTable<K,V>>> EmptyBucket<K, V, M> {
232 pub fn put(mut self, hash: SafeHash, key: K, value: V)
233 -> FullBucket<K, V, M> {
235 *self.raw.hash = hash.inspect();
236 write(self.raw.key, key);
237 write(self.raw.val, value);
240 self.table.size += 1;
242 FullBucket { raw: self.raw, idx: self.idx, table: self.table }
246 impl<K, V, M: Deref<RawTable<K,V>>> FullBucket<K, V, M> {
247 pub fn next(self) -> Bucket<K, V, M> {
248 let mut bucket = self.into_bucket();
253 pub fn into_bucket(self) -> Bucket<K, V, M> {
261 pub fn distance(&self) -> uint {
262 (self.idx - self.hash().inspect() as uint) & (self.table.capacity() - 1)
265 pub fn hash(&self) -> SafeHash {
273 pub fn read<'a>(&'a self) -> (&'a K, &'a V) {
280 pub fn into_refs(self) -> (&K, &V) {
282 // debug_assert!(*self.raw.hash != EMPTY_BUCKET);
289 impl<K, V, M: DerefMut<RawTable<K,V>>> FullBucket<K, V, M> {
290 pub fn take(mut self) -> (EmptyBucket<K, V, M>, K, V) {
291 let key = self.raw.key as *const K;
292 let val = self.raw.val as *const V;
294 self.table.size -= 1;
297 *self.raw.hash = EMPTY_BUCKET;
310 pub fn replace(&mut self, h: SafeHash, k: K, v: V) -> (SafeHash, K, V) {
312 let old_hash = ptr::replace(self.raw.hash as *mut SafeHash, h);
313 let old_key = ptr::replace(self.raw.key, k);
314 let old_val = ptr::replace(self.raw.val, v);
316 (old_hash, old_key, old_val)
320 pub fn read_mut<'a>(&'a self) -> (&'a mut K, &'a mut V) {
322 // debug_assert!(*self.raw.hash != EMPTY_BUCKET);
328 pub fn into_mut_refs(self) -> (&mut K, &mut V) {
330 // debug_assert!(*self.raw.hash != EMPTY_BUCKET);
337 impl<K, V, M: Deref<RawTable<K,V>>> Bucket<K, V, M> {
338 pub fn new(table: M, hash: &SafeHash) -> Bucket<K, V, M> {
339 let ib_index = (hash.inspect() as uint) & (table.capacity() - 1);
342 table.as_mut_ptrs().offset(ib_index as int)
349 pub fn at_index(table: M, ib_index: uint) -> Bucket<K, V, M> {
350 let ib_index = ib_index & (table.capacity() - 1);
353 table.as_mut_ptrs().offset(ib_index as int)
360 pub fn first(table: M) -> Bucket<K, V, M> {
362 raw: table.as_mut_ptrs(),
368 pub fn peek(self) -> BucketState<K, V, M> {
369 match unsafe { *self.raw.hash } {
385 pub fn next(&mut self) {
388 let dist = if self.idx == self.table.capacity() {
389 -(self.table.capacity() as int - 1)
395 self.raw = self.raw.offset(dist);
400 impl<K, V, M> BucketWithTable<M> for FullBucket<K, V, M> {
401 fn table<'a>(&'a self) -> &'a M {
405 fn into_table(self) -> M {
409 fn index(&self) -> uint {
414 impl<K, V, M> BucketWithTable<M> for EmptyBucket<K, V, M> {
415 fn table<'a>(&'a self) -> &'a M {
419 fn into_table(self) -> M {
423 fn index(&self) -> uint {
428 impl<K, V, M> BucketWithTable<M> for Bucket<K, V, M> {
429 fn table<'a>(&'a self) -> &'a M {
433 fn into_table(self) -> M {
437 fn index(&self) -> uint {
442 impl<'table,K,V> Deref<RawTable<K,V>> for &'table RawTable<K,V> {
443 fn deref<'a>(&'a self) -> &'a RawTable<K,V> {
448 impl<'table,K,V> Deref<RawTable<K,V>> for &'table mut RawTable<K,V> {
449 fn deref<'a>(&'a self) -> &'a RawTable<K,V> {
454 impl<'table,K,V> DerefMut<RawTable<K,V>> for &'table mut RawTable<K,V> {
455 fn deref_mut<'a>(&'a mut self) -> &'a mut RawTable<K,V> {
460 pub enum BucketState<K, V, M> {
461 Empty(EmptyBucket<K, V, M>),
462 Full(FullBucket<K, V, M>),
465 /// A hash that is not zero, since we use a hash of zero to represent empty
467 #[deriving(PartialEq)]
468 pub struct SafeHash {
473 /// Peek at the hash value, which is guaranteed to be non-zero.
475 pub fn inspect(&self) -> u64 { self.hash }
478 /// We need to remove hashes of 0. That's reserved for empty buckets.
479 /// This function wraps up `hash_keyed` to be the only way outside this
480 /// module to generate a SafeHash.
481 pub fn make_hash<T: Hash<S>, S, H: Hasher<S>>(hasher: &H, t: &T) -> SafeHash {
482 match hasher.hash(t) {
483 // This constant is exceedingly likely to hash to the same
484 // bucket, but it won't be counted as empty!
485 EMPTY_BUCKET => SafeHash { hash: 0x8000_0000_0000_0000 },
486 h => SafeHash { hash: h },
490 fn round_up_to_next(unrounded: uint, target_alignment: uint) -> uint {
491 assert!(is_power_of_two(target_alignment));
492 (unrounded + target_alignment - 1) & !(target_alignment - 1)
497 assert_eq!(round_up_to_next(0, 4), 0);
498 assert_eq!(round_up_to_next(1, 4), 4);
499 assert_eq!(round_up_to_next(2, 4), 4);
500 assert_eq!(round_up_to_next(3, 4), 4);
501 assert_eq!(round_up_to_next(4, 4), 4);
502 assert_eq!(round_up_to_next(5, 4), 8);
505 // Returns a tuple of (minimum required malloc alignment, hash_offset,
506 // key_offset, val_offset, array_size), from the start of a mallocated array.
507 fn calculate_offsets(
508 hash_size: uint, hash_align: uint,
509 keys_size: uint, keys_align: uint,
510 vals_size: uint, vals_align: uint) -> (uint, uint, uint, uint, uint) {
513 let end_of_hashes = hash_offset + hash_size;
515 let keys_offset = round_up_to_next(end_of_hashes, keys_align);
516 let end_of_keys = keys_offset + keys_size;
518 let vals_offset = round_up_to_next(end_of_keys, vals_align);
519 let end_of_vals = vals_offset + vals_size;
521 let min_align = cmp::max(hash_align, cmp::max(keys_align, vals_align));
523 (min_align, hash_offset, keys_offset, vals_offset, end_of_vals)
527 fn test_offset_calculation() {
528 assert_eq!(calculate_offsets(128, 8, 15, 1, 4, 4 ), (8, 0, 128, 144, 148));
529 assert_eq!(calculate_offsets(3, 1, 2, 1, 1, 1 ), (1, 0, 3, 5, 6));
530 assert_eq!(calculate_offsets(6, 2, 12, 4, 24, 8), (8, 0, 8, 24, 48));
533 impl<K, V> RawTable<K, V> {
535 /// Does not initialize the buckets. The caller should ensure they,
536 /// at the very least, set every hash to EMPTY_BUCKET.
537 unsafe fn new_uninitialized(capacity: uint) -> RawTable<K, V> {
542 hashes: 0 as *mut u64,
545 let hashes_size = capacity.checked_mul(&size_of::<u64>())
546 .expect("capacity overflow");
547 let keys_size = capacity.checked_mul(&size_of::< K >())
548 .expect("capacity overflow");
549 let vals_size = capacity.checked_mul(&size_of::< V >())
550 .expect("capacity overflow");
552 // Allocating hashmaps is a little tricky. We need to allocate three
553 // arrays, but since we know their sizes and alignments up front,
554 // we just allocate a single array, and then have the subarrays
557 // This is great in theory, but in practice getting the alignment
558 // right is a little subtle. Therefore, calculating offsets has been
559 // factored out into a different function.
560 let (malloc_alignment, hash_offset, _, _, size) =
562 hashes_size, min_align_of::<u64>(),
563 keys_size, min_align_of::< K >(),
564 vals_size, min_align_of::< V >());
566 let buffer = allocate(size, malloc_alignment);
568 let hashes = buffer.offset(hash_offset as int) as *mut u64;
577 fn as_mut_ptrs(&self) -> RawBucket<K, V> {
578 let hashes_size = self.capacity * size_of::<u64>();
579 let keys_size = self.capacity * size_of::<K>();
581 let keys_offset = (hashes_size + min_align_of::< K >() - 1) & !(min_align_of::< K >() - 1);
582 let end_of_keys = keys_offset + keys_size;
584 let vals_offset = (end_of_keys + min_align_of::< V >() - 1) & !(min_align_of::< V >() - 1);
586 let buffer = self.hashes as *mut u8;
591 key: buffer.offset(keys_offset as int) as *mut K,
592 val: buffer.offset(vals_offset as int) as *mut V
597 /// Creates a new raw table from a given capacity. All buckets are
599 #[allow(experimental)]
600 pub fn new(capacity: uint) -> RawTable<K, V> {
602 let ret = RawTable::new_uninitialized(capacity);
603 set_memory(ret.hashes, 0u8, capacity);
608 /// The hashtable's capacity, similar to a vector's.
609 pub fn capacity(&self) -> uint {
613 /// The number of elements ever `put` in the hashtable, minus the number
614 /// of elements ever `take`n.
615 pub fn size(&self) -> uint {
619 fn ptrs<'a>(&'a self) -> RawBuckets<'a, K, V> {
621 raw: self.as_mut_ptrs(),
623 self.hashes.offset(self.capacity as int)
628 pub fn iter<'a>(&'a self) -> Entries<'a, K, V> {
631 elems_left: self.size(),
635 pub fn mut_iter<'a>(&'a mut self) -> MutEntries<'a, K, V> {
638 elems_left: self.size(),
642 pub fn move_iter(self) -> MoveEntries<K, V> {
649 pub fn rev_move_buckets<'a>(&'a mut self) -> RevMoveBuckets<'a, K, V> {
650 let raw_bucket = self.as_mut_ptrs();
653 raw: raw_bucket.offset(self.capacity as int),
654 hashes_end: raw_bucket.hash,
655 elems_left: self.size
661 pub struct RawBuckets<'a, K, V> {
662 raw: RawBucket<K, V>,
666 impl<'a, K, V> Iterator<RawBucket<K, V>> for RawBuckets<'a, K, V> {
667 fn next(&mut self) -> Option<RawBucket<K, V>> {
668 while self.raw.hash != self.hashes_end {
670 let prev = ptr::replace(&mut self.raw, self.raw.offset(1));
671 if *prev.hash != EMPTY_BUCKET {
681 pub struct RevMoveBuckets<'a, K, V> {
682 raw: RawBucket<K, V>,
683 hashes_end: *mut u64,
687 impl<'a, K, V> Iterator<(K, V)> for RevMoveBuckets<'a, K, V> {
688 fn next(&mut self) -> Option<(K, V)> {
689 if self.elems_left == 0 {
694 debug_assert!(self.raw.hash != self.hashes_end);
697 self.raw = self.raw.offset(-1);
699 if *self.raw.hash != EMPTY_BUCKET {
700 self.elems_left -= 1;
702 ptr::read(self.raw.key as *const K),
703 ptr::read(self.raw.val as *const V)
711 // `read_all_mut` casts a `*u64` to a `*SafeHash`. Since we statically
712 // ensure that a `FullIndex` points to an index with a non-zero hash,
713 // and a `SafeHash` is just a `u64` with a different name, this is
716 // This test ensures that a `SafeHash` really IS the same size as a
717 // `u64`. If you need to change the size of `SafeHash` (and
718 // consequently made this test fail), `read_all_mut` needs to be
719 // modified to no longer assume this.
721 fn can_alias_safehash_as_u64() {
722 assert_eq!(size_of::<SafeHash>(), size_of::<u64>())
725 /// Note: stage0-specific version that lacks bound.
727 pub struct Entries<'a, K, V> {
728 iter: RawBuckets<'a, K, V>,
732 /// Iterator over shared references to entries in a table.
734 pub struct Entries<'a, K: 'a, V: 'a> {
735 iter: RawBuckets<'a, K, V>,
739 /// Note: stage0-specific version that lacks bound.
741 pub struct MutEntries<'a, K, V> {
742 iter: RawBuckets<'a, K, V>,
746 /// Iterator over mutable references to entries in a table.
748 pub struct MutEntries<'a, K: 'a, V: 'a> {
749 iter: RawBuckets<'a, K, V>,
753 /// Iterator over the entries in a table, consuming the table.
754 pub struct MoveEntries<K, V> {
755 table: RawTable<K, V>,
756 iter: RawBuckets<'static, K, V>
759 impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> {
760 fn next(&mut self) -> Option<(&'a K, &'a V)> {
761 self.iter.next().map(|bucket| {
762 self.elems_left -= 1;
770 fn size_hint(&self) -> (uint, Option<uint>) {
771 (self.elems_left, Some(self.elems_left))
775 impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {
776 fn next(&mut self) -> Option<(&'a K, &'a mut V)> {
777 self.iter.next().map(|bucket| {
778 self.elems_left -= 1;
786 fn size_hint(&self) -> (uint, Option<uint>) {
787 (self.elems_left, Some(self.elems_left))
791 impl<K, V> Iterator<(SafeHash, K, V)> for MoveEntries<K, V> {
792 fn next(&mut self) -> Option<(SafeHash, K, V)> {
793 self.iter.next().map(|bucket| {
794 self.table.size -= 1;
800 ptr::read(bucket.key as *const K),
801 ptr::read(bucket.val as *const V)
807 fn size_hint(&self) -> (uint, Option<uint>) {
808 let size = self.table.size();
813 impl<K: Clone, V: Clone> Clone for RawTable<K, V> {
814 fn clone(&self) -> RawTable<K, V> {
816 let mut new_ht = RawTable::new_uninitialized(self.capacity());
819 let cap = self.capacity();
820 let mut new_buckets = Bucket::first(&mut new_ht);
821 let mut buckets = Bucket::first(self);
822 while buckets.index() != cap {
823 match buckets.peek() {
826 let (k, v) = full.read();
827 (full.hash(), k.clone(), v.clone())
829 *new_buckets.raw.hash = h.inspect();
830 mem::overwrite(new_buckets.raw.key, k);
831 mem::overwrite(new_buckets.raw.val, v);
834 *new_buckets.raw.hash = EMPTY_BUCKET;
842 new_ht.size = self.size();
850 impl<K, V> Drop for RawTable<K, V> {
852 if self.hashes.is_null() {
855 // This is in reverse because we're likely to have partially taken
856 // some elements out with `.move_iter()` from the front.
857 // Check if the size is 0, so we don't do a useless scan when
858 // dropping empty tables such as on resize.
859 // Avoid double free of elements already moved out.
860 for _ in self.rev_move_buckets() {}
862 let hashes_size = self.capacity * size_of::<u64>();
863 let keys_size = self.capacity * size_of::<K>();
864 let vals_size = self.capacity * size_of::<V>();
865 let (align, _, _, _, size) = calculate_offsets(hashes_size, min_align_of::<u64>(),
866 keys_size, min_align_of::<K>(),
867 vals_size, min_align_of::<V>());
870 deallocate(self.hashes as *mut u8, size, align);
871 // Remember how everything was allocated out of one buffer
872 // during initialization? We only need one call to free here.
875 self.hashes = RawPtr::null();