rustc_type_ir/data_structures/
delayed_map.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
use std::hash::Hash;

use crate::data_structures::{HashMap, HashSet};

const CACHE_CUTOFF: u32 = 32;

/// A hashmap which only starts hashing after ignoring the first few inputs.
///
/// This is used in type folders as in 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(always)]
    pub fn insert(&mut self, key: K, value: V) -> bool {
        if self.count >= CACHE_CUTOFF {
            self.cold_insert(key, value)
        } else {
            self.count += 1;
            true
        }
    }

    #[cold]
    #[inline(never)]
    fn cold_insert(&mut self, key: K, value: V) -> bool {
        self.cache.insert(key, value).is_none()
    }

    #[inline(always)]
    pub fn get(&self, key: &K) -> Option<&V> {
        if self.cache.is_empty() { None } else { self.cold_get(key) }
    }

    #[cold]
    #[inline(never)]
    fn cold_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(always)]
    pub fn insert(&mut self, value: T) -> bool {
        if self.count >= CACHE_CUTOFF {
            self.cold_insert(value)
        } else {
            self.count += 1;
            true
        }
    }

    #[cold]
    #[inline(never)]
    fn cold_insert(&mut self, value: T) -> bool {
        self.cache.insert(value)
    }

    #[inline(always)]
    pub fn contains(&self, value: &T) -> bool {
        !self.cache.is_empty() && self.cold_contains(value)
    }

    #[cold]
    #[inline(never)]
    fn cold_contains(&self, value: &T) -> bool {
        self.cache.contains(value)
    }
}