@@ -40,6 +40,7 @@ use tracing::{debug, trace};
4040pub ( super ) struct EncodeContext < ' a , ' tcx > {
4141 opaque : opaque:: Encoder ,
4242 tcx : TyCtxt < ' tcx > ,
43+ feat : & ' tcx rustc_feature:: Features ,
4344
4445 tables : TableBuilders < ' tcx > ,
4546
@@ -1132,15 +1133,25 @@ impl EncodeContext<'a, 'tcx> {
11321133
11331134 fn encode_stability ( & mut self , def_id : DefId ) {
11341135 debug ! ( "EncodeContext::encode_stability({:?})" , def_id) ;
1135- if let Some ( stab) = self . tcx . lookup_stability ( def_id) {
1136- record ! ( self . tables. stability[ def_id] <- stab)
1136+
1137+ // The query lookup can take a measurable amount of time in crates with many items. Check if
1138+ // the stability attributes are even enabled before using their queries.
1139+ if self . feat . staged_api || self . tcx . sess . opts . debugging_opts . force_unstable_if_unmarked {
1140+ if let Some ( stab) = self . tcx . lookup_stability ( def_id) {
1141+ record ! ( self . tables. stability[ def_id] <- stab)
1142+ }
11371143 }
11381144 }
11391145
11401146 fn encode_const_stability ( & mut self , def_id : DefId ) {
11411147 debug ! ( "EncodeContext::encode_const_stability({:?})" , def_id) ;
1142- if let Some ( stab) = self . tcx . lookup_const_stability ( def_id) {
1143- record ! ( self . tables. const_stability[ def_id] <- stab)
1148+
1149+ // The query lookup can take a measurable amount of time in crates with many items. Check if
1150+ // the stability attributes are even enabled before using their queries.
1151+ if self . feat . staged_api || self . tcx . sess . opts . debugging_opts . force_unstable_if_unmarked {
1152+ if let Some ( stab) = self . tcx . lookup_const_stability ( def_id) {
1153+ record ! ( self . tables. const_stability[ def_id] <- stab)
1154+ }
11441155 }
11451156 }
11461157
@@ -1979,6 +1990,7 @@ fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
19791990 let mut ecx = EncodeContext {
19801991 opaque : encoder,
19811992 tcx,
1993+ feat : tcx. features ( ) ,
19821994 tables : Default :: default ( ) ,
19831995 lazy_state : LazyState :: NoNode ,
19841996 type_shorthands : Default :: default ( ) ,
0 commit comments