forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
105 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
use std::hash::Hash; | ||
|
||
use crate::data_structures::{HashMap, HashSet}; | ||
|
||
const CACHE_CUTOFF: u32 = 16; | ||
|
||
/// A hashmap which only starts hashing after ignoring the first few inputs. | ||
/// | ||
/// This is used in type folders asin nearly all cases caching is not worth it | ||
/// as nearly all folded types are tiny. However, there are very rare incredibly | ||
/// large types for which caching is necessary to avoid hangs. | ||
#[derive(Debug)] | ||
pub struct DelayedMap<K, V> { | ||
cache: HashMap<K, V>, | ||
count: u32, | ||
} | ||
|
||
impl<K, V> Default for DelayedMap<K, V> { | ||
fn default() -> Self { | ||
DelayedMap { cache: Default::default(), count: 0 } | ||
} | ||
} | ||
|
||
impl<K: Hash + Eq, V> DelayedMap<K, V> { | ||
#[inline] | ||
pub fn insert(&mut self, key: K, value: V) -> bool { | ||
if self.count >= CACHE_CUTOFF { | ||
self.cache.insert(key, value).is_none() | ||
} else { | ||
self.count += 1; | ||
true | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn get(&self, key: &K) -> Option<&V> { | ||
self.cache.get(key) | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct DelayedSet<T> { | ||
cache: HashSet<T>, | ||
count: u32, | ||
} | ||
|
||
impl<T> Default for DelayedSet<T> { | ||
fn default() -> Self { | ||
DelayedSet { cache: Default::default(), count: 0 } | ||
} | ||
} | ||
|
||
impl<T: Hash + Eq> DelayedSet<T> { | ||
#[inline] | ||
pub fn insert(&mut self, value: T) -> bool { | ||
if self.count >= CACHE_CUTOFF { | ||
self.cache.insert(value) | ||
} else { | ||
self.count += 1; | ||
true | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn contains(&self, value: &T) -> bool { | ||
self.cache.contains(value) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters