]> git.lizzy.rs Git - rust.git/blobdiff - src/librustc_data_structures/sharded.rs
Auto merge of #67885 - tobithiel:fix_group_lint_allow_override, r=Mark-Simulacrum
[rust.git] / src / librustc_data_structures / sharded.rs
index a28a5e0f0415a575a8c32683f36153c7674b3b9f..15d1e2dd0b644c08086e60a9c8b82586fda0589d 100644 (file)
@@ -1,10 +1,10 @@
-use std::hash::{Hasher, Hash};
-use std::mem;
+use crate::fx::{FxHashMap, FxHasher};
+use crate::sync::{Lock, LockGuard};
+use smallvec::SmallVec;
 use std::borrow::Borrow;
 use std::collections::hash_map::RawEntryMut;
-use smallvec::SmallVec;
-use crate::fx::{FxHasher, FxHashMap};
-use crate::sync::{Lock, LockGuard};
+use std::hash::{Hash, Hasher};
+use std::mem;
 
 #[derive(Clone, Default)]
 #[cfg_attr(parallel_compiler, repr(align(64)))]
@@ -38,9 +38,8 @@ impl<T> Sharded<T> {
     #[inline]
     pub fn new(mut value: impl FnMut() -> T) -> Self {
         // Create a vector of the values we want
-        let mut values: SmallVec<[_; SHARDS]> = (0..SHARDS).map(|_| {
-            CacheAligned(Lock::new(value()))
-        }).collect();
+        let mut values: SmallVec<[_; SHARDS]> =
+            (0..SHARDS).map(|_| CacheAligned(Lock::new(value()))).collect();
 
         // Create an unintialized array
         let mut shards: mem::MaybeUninit<[CacheAligned<Lock<T>>; SHARDS]> =
@@ -54,20 +53,14 @@ pub fn new(mut value: impl FnMut() -> T) -> Self {
             // Ignore the content of the vector
             values.set_len(0);
 
-            Sharded {
-                shards: shards.assume_init(),
-            }
+            Sharded { shards: shards.assume_init() }
         }
     }
 
     /// The shard is selected by hashing `val` with `FxHasher`.
     #[inline]
     pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
-        if SHARDS == 1 {
-            &self.shards[0].0
-        } else {
-            self.get_shard_by_hash(make_hash(val))
-        }
+        if SHARDS == 1 { &self.shards[0].0 } else { self.get_shard_by_hash(make_hash(val)) }
     }
 
     /// Get a shard with a pre-computed hash value. If `get_shard_by_value` is
@@ -76,12 +69,21 @@ pub fn get_shard_by_value<K: Hash + ?Sized>(&self, val: &K) -> &Lock<T> {
     /// `hash` can be computed with any hasher, so long as that hasher is used
     /// consistently for each `Sharded` instance.
     #[inline]
-    pub fn get_shard_by_hash(&self, hash: u64) -> &Lock<T> {
+    pub fn get_shard_index_by_hash(&self, hash: u64) -> usize {
         let hash_len = mem::size_of::<usize>();
         // Ignore the top 7 bits as hashbrown uses these and get the next SHARD_BITS highest bits.
         // hashbrown also uses the lowest bits, so we can't use those
         let bits = (hash >> (hash_len * 8 - 7 - SHARD_BITS)) as usize;
-        let i = bits % SHARDS;
+        bits % SHARDS
+    }
+
+    #[inline]
+    pub fn get_shard_by_hash(&self, hash: u64) -> &Lock<T> {
+        &self.shards[self.get_shard_index_by_hash(hash)].0
+    }
+
+    #[inline]
+    pub fn get_shard_by_index(&self, i: usize) -> &Lock<T> {
         &self.shards[i].0
     }
 
@@ -105,8 +107,9 @@ pub fn len(&self) -> usize {
 impl<K: Eq + Hash + Copy> ShardedHashMap<K, ()> {
     #[inline]
     pub fn intern_ref<Q: ?Sized>(&self, value: &Q, make: impl FnOnce() -> K) -> K
-        where K: Borrow<Q>,
-              Q: Hash + Eq
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
     {
         let hash = make_hash(value);
         let mut shard = self.get_shard_by_hash(hash).lock();
@@ -124,8 +127,9 @@ pub fn intern_ref<Q: ?Sized>(&self, value: &Q, make: impl FnOnce() -> K) -> K
 
     #[inline]
     pub fn intern<Q>(&self, value: Q, make: impl FnOnce(Q) -> K) -> K
-        where K: Borrow<Q>,
-              Q: Hash + Eq
+    where
+        K: Borrow<Q>,
+        Q: Hash + Eq,
     {
         let hash = make_hash(&value);
         let mut shard = self.get_shard_by_hash(hash).lock();
@@ -142,6 +146,20 @@ pub fn intern<Q>(&self, value: Q, make: impl FnOnce(Q) -> K) -> K
     }
 }
 
+pub trait IntoPointer {
+    /// Returns a pointer which outlives `self`.
+    fn into_pointer(&self) -> *const ();
+}
+
+impl<K: Eq + Hash + Copy + IntoPointer> ShardedHashMap<K, ()> {
+    pub fn contains_pointer_to<T: Hash + IntoPointer>(&self, value: &T) -> bool {
+        let hash = make_hash(&value);
+        let shard = self.get_shard_by_hash(hash).lock();
+        let value = value.into_pointer();
+        shard.raw_entry().from_hash(hash, |entry| entry.into_pointer() == value).is_some()
+    }
+}
+
 #[inline]
 fn make_hash<K: Hash + ?Sized>(val: &K) -> u64 {
     let mut state = FxHasher::default();