Skip to main content

rustc_type_ir/data_structures/
delayed_map.rs

1use std::hash::Hash;
2
3use crate::data_structures::{HashMap, HashSet};
4
5const CACHE_CUTOFF: u32 = 32;
6
7/// A hashmap which only starts hashing after ignoring the first few inputs.
8///
9/// This is used in type folders as in nearly all cases caching is not worth it
10/// as nearly all folded types are tiny. However, there are very rare incredibly
11/// large types for which caching is necessary to avoid hangs.
12#[derive(#[automatically_derived]
impl<K: ::core::fmt::Debug, V: ::core::fmt::Debug> ::core::fmt::Debug for
    DelayedMap<K, V> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "DelayedMap",
            "cache", &self.cache, "count", &&self.count)
    }
}Debug)]
13pub struct DelayedMap<K, V> {
14    cache: HashMap<K, V>,
15    count: u32,
16}
17
18impl<K, V> Default for DelayedMap<K, V> {
19    fn default() -> Self {
20        DelayedMap { cache: Default::default(), count: 0 }
21    }
22}
23
24impl<K: Hash + Eq, V> DelayedMap<K, V> {
25    #[inline(always)]
26    pub fn insert(&mut self, key: K, value: V) -> bool {
27        if self.count >= CACHE_CUTOFF {
28            self.cold_insert(key, value)
29        } else {
30            self.count += 1;
31            true
32        }
33    }
34
35    #[cold]
36    #[inline(never)]
37    fn cold_insert(&mut self, key: K, value: V) -> bool {
38        self.cache.insert(key, value).is_none()
39    }
40
41    #[inline(always)]
42    pub fn get(&self, key: &K) -> Option<&V> {
43        if self.cache.is_empty() { None } else { self.cold_get(key) }
44    }
45
46    #[cold]
47    #[inline(never)]
48    fn cold_get(&self, key: &K) -> Option<&V> {
49        self.cache.get(key)
50    }
51
52    #[inline]
53    pub fn clear(&mut self) {
54        self.cache.clear();
55        self.count = 0;
56    }
57}
58
59#[derive(#[automatically_derived]
impl<T: ::core::fmt::Debug> ::core::fmt::Debug for DelayedSet<T> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "DelayedSet",
            "cache", &self.cache, "count", &&self.count)
    }
}Debug)]
60pub struct DelayedSet<T> {
61    cache: HashSet<T>,
62    count: u32,
63}
64
65impl<T> Default for DelayedSet<T> {
66    fn default() -> Self {
67        DelayedSet { cache: Default::default(), count: 0 }
68    }
69}
70
71impl<T: Hash + Eq> DelayedSet<T> {
72    #[inline(always)]
73    pub fn insert(&mut self, value: T) -> bool {
74        if self.count >= CACHE_CUTOFF {
75            self.cold_insert(value)
76        } else {
77            self.count += 1;
78            true
79        }
80    }
81
82    #[cold]
83    #[inline(never)]
84    fn cold_insert(&mut self, value: T) -> bool {
85        self.cache.insert(value)
86    }
87
88    #[inline(always)]
89    pub fn contains(&self, value: &T) -> bool {
90        !self.cache.is_empty() && self.cold_contains(value)
91    }
92
93    #[cold]
94    #[inline(never)]
95    fn cold_contains(&self, value: &T) -> bool {
96        self.cache.contains(value)
97    }
98
99    #[inline]
100    pub fn clear(&mut self) {
101        self.cache.clear();
102        self.count = 0;
103    }
104}