rustc_metadata: Encode doc(hidden) flag to metadata

To retrieve these flags rustdoc currently has to mass decode full attributes for items in the whole crate tree, so it's better to pre-compute it in advance.

This is especially for short-term performance of https://github.com/rust-lang/rust/pull/107054 because resolver cannot use memoization of query results yet.
This commit is contained in:
Vadim Petrochenkov 2023-01-21 00:59:19 +04:00
parent 005fc0f00f
commit 415c14129b
10 changed files with 52 additions and 18 deletions

View File

@ -4326,6 +4326,7 @@ dependencies = [
name = "rustc_metadata" name = "rustc_metadata"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"bitflags",
"libloading", "libloading",
"odht", "odht",
"rustc_ast", "rustc_ast",

View File

@ -6,6 +6,7 @@ edition = "2021"
[lib] [lib]
[dependencies] [dependencies]
bitflags = "1.2.1"
libloading = "0.7.1" libloading = "0.7.1"
odht = { version = "0.3.1", features = ["nightly"] } odht = { version = "0.3.1", features = ["nightly"] }
snap = "1" snap = "1"

View File

@ -1594,8 +1594,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
}) })
} }
fn get_may_have_doc_links(self, index: DefIndex) -> bool { fn get_attr_flags(self, index: DefIndex) -> AttrFlags {
self.root.tables.may_have_doc_links.get(self, index).is_some() self.root.tables.attr_flags.get(self, index).unwrap_or(AttrFlags::empty())
} }
fn get_is_intrinsic(self, index: DefIndex) -> bool { fn get_is_intrinsic(self, index: DefIndex) -> bool {

View File

@ -1,6 +1,7 @@
use crate::creader::{CStore, LoadedMacro}; use crate::creader::{CStore, LoadedMacro};
use crate::foreign_modules; use crate::foreign_modules;
use crate::native_libs; use crate::native_libs;
use crate::rmeta::AttrFlags;
use rustc_ast as ast; use rustc_ast as ast;
use rustc_attr::Deprecation; use rustc_attr::Deprecation;
@ -338,6 +339,7 @@ provide! { tcx, def_id, other, cdata,
crate_extern_paths => { cdata.source().paths().cloned().collect() } crate_extern_paths => { cdata.source().paths().cloned().collect() }
expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) } expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
generator_diagnostic_data => { cdata.get_generator_diagnostic_data(tcx, def_id.index) } generator_diagnostic_data => { cdata.get_generator_diagnostic_data(tcx, def_id.index) }
is_doc_hidden => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
} }
pub(in crate::rmeta) fn provide(providers: &mut Providers) { pub(in crate::rmeta) fn provide(providers: &mut Providers) {
@ -425,7 +427,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
return; return;
} }
if ty::util::is_doc_hidden(tcx, parent) { if tcx.is_doc_hidden(parent) {
fallback_map.push((def_id, parent)); fallback_map.push((def_id, parent));
return; return;
} }
@ -631,7 +633,9 @@ impl CStore {
} }
pub fn may_have_doc_links_untracked(&self, def_id: DefId) -> bool { pub fn may_have_doc_links_untracked(&self, def_id: DefId) -> bool {
self.get_crate_data(def_id.krate).get_may_have_doc_links(def_id.index) self.get_crate_data(def_id.krate)
.get_attr_flags(def_id.index)
.contains(AttrFlags::MAY_HAVE_DOC_LINKS)
} }
} }

View File

@ -1111,15 +1111,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
let tcx = self.tcx; let tcx = self.tcx;
let mut is_public: Option<bool> = None; let mut is_public: Option<bool> = None;
let mut attrs = tcx let hir_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
.hir() let mut attrs = hir_attrs
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
.iter() .iter()
.filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public)); .filter(move |attr| should_encode_attr(tcx, attr, def_id, &mut is_public));
record_array!(self.tables.attributes[def_id.to_def_id()] <- attrs.clone()); record_array!(self.tables.attributes[def_id.to_def_id()] <- attrs.clone());
let mut attr_flags = AttrFlags::empty();
if attrs.any(|attr| attr.may_have_doc_links()) { if attrs.any(|attr| attr.may_have_doc_links()) {
self.tables.may_have_doc_links.set(def_id.local_def_index, ()); attr_flags |= AttrFlags::MAY_HAVE_DOC_LINKS;
}
if hir_attrs
.iter()
.filter(|attr| attr.has_name(sym::doc))
.filter_map(|attr| attr.meta_item_list())
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))
{
attr_flags |= AttrFlags::IS_DOC_HIDDEN;
}
if !attr_flags.is_empty() {
self.tables.attr_flags.set(def_id.local_def_index, attr_flags);
} }
} }

View File

@ -395,7 +395,7 @@ define_tables! {
def_path_hashes: Table<DefIndex, DefPathHash>, def_path_hashes: Table<DefIndex, DefPathHash>,
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>, proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>, generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
may_have_doc_links: Table<DefIndex, ()>, attr_flags: Table<DefIndex, AttrFlags>,
variant_data: Table<DefIndex, LazyValue<VariantData>>, variant_data: Table<DefIndex, LazyValue<VariantData>>,
assoc_container: Table<DefIndex, ty::AssocItemContainer>, assoc_container: Table<DefIndex, ty::AssocItemContainer>,
// Slot is full when macro is macro_rules. // Slot is full when macro is macro_rules.
@ -418,6 +418,13 @@ struct VariantData {
is_non_exhaustive: bool, is_non_exhaustive: bool,
} }
bitflags::bitflags! {
pub struct AttrFlags: u8 {
const MAY_HAVE_DOC_LINKS = 1 << 0;
const IS_DOC_HIDDEN = 1 << 1;
}
}
// Tags used for encoding Spans: // Tags used for encoding Spans:
const TAG_VALID_SPAN_LOCAL: u8 = 0; const TAG_VALID_SPAN_LOCAL: u8 = 0;
const TAG_VALID_SPAN_FOREIGN: u8 = 1; const TAG_VALID_SPAN_FOREIGN: u8 = 1;
@ -440,4 +447,5 @@ trivially_parameterized_over_tcx! {
IncoherentImpls, IncoherentImpls,
CrateRoot, CrateRoot,
CrateDep, CrateDep,
AttrFlags,
} }

View File

@ -199,6 +199,20 @@ impl FixedSizeEncoding for Option<RawDefId> {
} }
} }
impl FixedSizeEncoding for Option<AttrFlags> {
type ByteArray = [u8; 1];
#[inline]
fn from_bytes(b: &[u8; 1]) -> Self {
(b[0] != 0).then(|| AttrFlags::from_bits_truncate(b[0]))
}
#[inline]
fn write_to_bytes(self, b: &mut [u8; 1]) {
b[0] = self.map_or(0, |flags| flags.bits())
}
}
impl FixedSizeEncoding for Option<()> { impl FixedSizeEncoding for Option<()> {
type ByteArray = [u8; 1]; type ByteArray = [u8; 1];

View File

@ -1157,6 +1157,7 @@ rustc_queries! {
/// Determines whether an item is annotated with `doc(hidden)`. /// Determines whether an item is annotated with `doc(hidden)`.
query is_doc_hidden(def_id: DefId) -> bool { query is_doc_hidden(def_id: DefId) -> bool {
desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) } desc { |tcx| "checking whether `{}` is `doc(hidden)`", tcx.def_path_str(def_id) }
separate_provide_extern
} }
/// Determines whether an item is annotated with `doc(notable_trait)`. /// Determines whether an item is annotated with `doc(notable_trait)`.

View File

@ -1310,7 +1310,8 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
} }
/// Determines whether an item is annotated with `doc(hidden)`. /// Determines whether an item is annotated with `doc(hidden)`.
pub fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool { fn is_doc_hidden(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
assert!(def_id.is_local());
tcx.get_attrs(def_id, sym::doc) tcx.get_attrs(def_id, sym::doc)
.filter_map(|attr| attr.meta_item_list()) .filter_map(|attr| attr.meta_item_list())
.any(|items| items.iter().any(|item| item.has_name(sym::hidden))) .any(|items| items.iter().any(|item| item.has_name(sym::hidden)))

View File

@ -2498,14 +2498,7 @@ impl Import {
} }
pub(crate) fn imported_item_is_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool { pub(crate) fn imported_item_is_doc_hidden(&self, tcx: TyCtxt<'_>) -> bool {
match self.source.did { self.source.did.map_or(false, |did| tcx.is_doc_hidden(did))
Some(did) => tcx
.get_attrs(did, sym::doc)
.filter_map(ast::Attribute::meta_item_list)
.flatten()
.has_word(sym::hidden),
None => false,
}
} }
} }