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 count", &self.cache, &&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]
26    pub fn len(&self) -> usize {
27        self.cache.len()
28    }
29
30    #[inline]
31    pub fn capacity(&self) -> usize {
32        self.cache.capacity()
33    }
34
35    #[inline(always)]
36    pub fn insert(&mut self, key: K, value: V) -> bool {
37        if self.count >= CACHE_CUTOFF {
38            self.cold_insert(key, value)
39        } else {
40            self.count += 1;
41            true
42        }
43    }
44
45    #[cold]
46    #[inline(never)]
47    fn cold_insert(&mut self, key: K, value: V) -> bool {
48        self.cache.insert(key, value).is_none()
49    }
50
51    #[inline(always)]
52    pub fn get(&self, key: &K) -> Option<&V> {
53        if self.cache.is_empty() { None } else { self.cold_get(key) }
54    }
55
56    #[cold]
57    #[inline(never)]
58    fn cold_get(&self, key: &K) -> Option<&V> {
59        self.cache.get(key)
60    }
61
62    #[inline]
63    pub fn clear(&mut self) {
64        self.cache.clear();
65        self.count = 0;
66    }
67}
68
69#[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 count", &self.cache, &&self.count)
    }
}Debug)]
70pub struct DelayedSet<T> {
71    cache: HashSet<T>,
72    count: u32,
73}
74
75impl<T> Default for DelayedSet<T> {
76    fn default() -> Self {
77        DelayedSet { cache: Default::default(), count: 0 }
78    }
79}
80
81impl<T: Hash + Eq> DelayedSet<T> {
82    #[inline(always)]
83    pub fn insert(&mut self, value: T) -> bool {
84        if self.count >= CACHE_CUTOFF {
85            self.cold_insert(value)
86        } else {
87            self.count += 1;
88            true
89        }
90    }
91
92    #[cold]
93    #[inline(never)]
94    fn cold_insert(&mut self, value: T) -> bool {
95        self.cache.insert(value)
96    }
97
98    #[inline(always)]
99    pub fn contains(&self, value: &T) -> bool {
100        !self.cache.is_empty() && self.cold_contains(value)
101    }
102
103    #[cold]
104    #[inline(never)]
105    fn cold_contains(&self, value: &T) -> bool {
106        self.cache.contains(value)
107    }
108
109    #[inline]
110    pub fn clear(&mut self) {
111        self.cache.clear();
112        self.count = 0;
113    }
114}