summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_middle/src/mir/graph_cyclic_cache.rs
blob: f97bf2883b3691b3f9b10f5899dd2b629f25899c (plain)
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
use rustc_data_structures::graph::{
    self, DirectedGraph, WithNumNodes, WithStartNode, WithSuccessors,
};
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_data_structures::sync::OnceCell;
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};

/// Helper type to cache the result of `graph::is_cyclic`.
#[derive(Clone, Debug)]
pub(super) struct GraphIsCyclicCache {
    cache: OnceCell<bool>,
}

impl GraphIsCyclicCache {
    #[inline]
    pub(super) fn new() -> Self {
        GraphIsCyclicCache { cache: OnceCell::new() }
    }

    pub(super) fn is_cyclic<G>(&self, graph: &G) -> bool
    where
        G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes,
    {
        *self.cache.get_or_init(|| graph::is_cyclic(graph))
    }

    /// Invalidates the cache.
    #[inline]
    pub(super) fn invalidate(&mut self) {
        // Invalidating the cache requires mutating the MIR, which in turn requires a unique
        // reference (`&mut`) to the `mir::Body`. Because of this, we can assume that all
        // callers of `invalidate` have a unique reference to the MIR and thus to the
        // cache. This means we never need to do synchronization when `invalidate` is called,
        // we can simply reinitialize the `OnceCell`.
        self.cache = OnceCell::new();
    }
}

impl<S: Encoder> Encodable<S> for GraphIsCyclicCache {
    #[inline]
    fn encode(&self, s: &mut S) {
        Encodable::encode(&(), s);
    }
}

impl<D: Decoder> Decodable<D> for GraphIsCyclicCache {
    #[inline]
    fn decode(d: &mut D) -> Self {
        let () = Decodable::decode(d);
        Self::new()
    }
}

impl<CTX> HashStable<CTX> for GraphIsCyclicCache {
    #[inline]
    fn hash_stable(&self, _: &mut CTX, _: &mut StableHasher) {
        // do nothing
    }
}

TrivialTypeTraversalAndLiftImpls! {
    GraphIsCyclicCache,
}