Skip to main content

rustc_middle/ty/
impls_ty.rs

1//! This module contains `HashStable` implementations for various data types
2//! from `rustc_middle::ty` in no particular order.
3
4use std::cell::RefCell;
5use std::ptr;
6
7use rustc_data_structures::fingerprint::Fingerprint;
8use rustc_data_structures::fx::FxHashMap;
9use rustc_data_structures::stable_hasher::{
10    HashStable, HashingControls, StableHasher, ToStableHashKey,
11};
12use rustc_query_system::ich::StableHashingContext;
13use tracing::trace;
14
15use crate::middle::region;
16use crate::{mir, ty};
17
18impl<'a, 'tcx, H, T> HashStable<StableHashingContext<'a>> for &'tcx ty::list::RawList<H, T>
19where
20    T: HashStable<StableHashingContext<'a>>,
21{
22    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
23        // Note: this cache makes an *enormous* performance difference on certain benchmarks. E.g.
24        // without it, compiling `diesel-2.2.10` can be 74% slower, and compiling
25        // `deeply-nested-multi` can be ~4,000x slower(!)
26        const CACHE:
    ::std::thread::LocalKey<RefCell<FxHashMap<(*const (), HashingControls),
    Fingerprint>>> =
    {
        #[inline]
        fn __rust_std_internal_init_fn()
            -> RefCell<FxHashMap<(*const (), HashingControls), Fingerprint>> {
            RefCell::new(Default::default())
        }
        unsafe {
            ::std::thread::LocalKey::new(const {
                        if ::std::mem::needs_drop::<RefCell<FxHashMap<(*const (),
                                    HashingControls), Fingerprint>>>() {
                            |__rust_std_internal_init|
                                {
                                    #[thread_local]
                                    static __RUST_STD_INTERNAL_VAL:
                                        ::std::thread::local_impl::LazyStorage<RefCell<FxHashMap<(*const (),
                                        HashingControls), Fingerprint>>, ()> =
                                        ::std::thread::local_impl::LazyStorage::new();
                                    __RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init,
                                        __rust_std_internal_init_fn)
                                }
                        } else {
                            |__rust_std_internal_init|
                                {
                                    #[thread_local]
                                    static __RUST_STD_INTERNAL_VAL:
                                        ::std::thread::local_impl::LazyStorage<RefCell<FxHashMap<(*const (),
                                        HashingControls), Fingerprint>>, !> =
                                        ::std::thread::local_impl::LazyStorage::new();
                                    __RUST_STD_INTERNAL_VAL.get_or_init(__rust_std_internal_init,
                                        __rust_std_internal_init_fn)
                                }
                        }
                    })
        }
    };thread_local! {
27            static CACHE: RefCell<FxHashMap<(*const (), HashingControls), Fingerprint>> =
28                RefCell::new(Default::default());
29        }
30
31        let hash = CACHE.with(|cache| {
32            let key = (ptr::from_ref(*self).cast::<()>(), hcx.hashing_controls());
33            if let Some(&hash) = cache.borrow().get(&key) {
34                return hash;
35            }
36
37            let mut hasher = StableHasher::new();
38            self[..].hash_stable(hcx, &mut hasher);
39
40            let hash: Fingerprint = hasher.finish();
41            cache.borrow_mut().insert(key, hash);
42            hash
43        });
44
45        hash.hash_stable(hcx, hasher);
46    }
47}
48
49impl<'a, 'tcx, H, T> ToStableHashKey<StableHashingContext<'a>> for &'tcx ty::list::RawList<H, T>
50where
51    T: HashStable<StableHashingContext<'a>>,
52{
53    type KeyType = Fingerprint;
54
55    #[inline]
56    fn to_stable_hash_key(&self, hcx: &StableHashingContext<'a>) -> Fingerprint {
57        let mut hasher = StableHasher::new();
58        let mut hcx: StableHashingContext<'a> = hcx.clone();
59        self.hash_stable(&mut hcx, &mut hasher);
60        hasher.finish()
61    }
62}
63
64impl<'a, 'tcx> HashStable<StableHashingContext<'a>> for ty::GenericArg<'tcx> {
65    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
66        self.kind().hash_stable(hcx, hasher);
67    }
68}
69
70// AllocIds get resolved to whatever they point to (to be stable)
71impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::AllocId {
72    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
73        ty::tls::with_opt(|tcx| {
74            {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_middle/src/ty/impls_ty.rs:74",
                        "rustc_middle::ty::impls_ty", ::tracing::Level::TRACE,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_middle/src/ty/impls_ty.rs"),
                        ::tracing_core::__macro_support::Option::Some(74u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_middle::ty::impls_ty"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("hashing {0:?}",
                                                    *self) as &dyn Value))])
            });
    } else { ; }
};trace!("hashing {:?}", *self);
75            let tcx = tcx.expect("can't hash AllocIds during hir lowering");
76            tcx.try_get_global_alloc(*self).hash_stable(hcx, hasher);
77        });
78    }
79}
80
81impl<'a> HashStable<StableHashingContext<'a>> for mir::interpret::CtfeProvenance {
82    fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
83        self.into_parts().hash_stable(hcx, hasher);
84    }
85}
86
87impl<'a> ToStableHashKey<StableHashingContext<'a>> for region::Scope {
88    type KeyType = region::Scope;
89
90    #[inline]
91    fn to_stable_hash_key(&self, _: &StableHashingContext<'a>) -> region::Scope {
92        *self
93    }
94}