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"
version = "0.0.0"
dependencies = [
"bitflags",
"libloading",
"odht",
"rustc_ast",

View file

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

View file

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

View file

@ -1,6 +1,7 @@
use crate::creader::{CStore, LoadedMacro};
use crate::foreign_modules;
use crate::native_libs;
use crate::rmeta::AttrFlags;
use rustc_ast as ast;
use rustc_attr::Deprecation;
@ -338,6 +339,7 @@ fn into_args(self) -> (DefId, SimplifiedType) {
crate_extern_paths => { cdata.source().paths().cloned().collect() }
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) }
is_doc_hidden => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
}
pub(in crate::rmeta) fn provide(providers: &mut Providers) {
@ -425,7 +427,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
return;
}
if ty::util::is_doc_hidden(tcx, parent) {
if tcx.is_doc_hidden(parent) {
fallback_map.push((def_id, parent));
return;
}
@ -631,7 +633,9 @@ pub fn associated_item_def_ids_untracked<'a>(
}
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 @@ fn encode_attrs(&mut self, def_id: LocalDefId) {
let tcx = self.tcx;
let mut is_public: Option<bool> = None;
let mut attrs = tcx
.hir()
.attrs(tcx.hir().local_def_id_to_hir_id(def_id))
let hir_attrs = tcx.hir().attrs(tcx.hir().local_def_id_to_hir_id(def_id));
let mut attrs = hir_attrs
.iter()
.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());
let mut attr_flags = AttrFlags::empty();
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 @@ fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
def_path_hashes: Table<DefIndex, DefPathHash>,
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
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>>,
assoc_container: Table<DefIndex, ty::AssocItemContainer>,
// Slot is full when macro is macro_rules.
@ -418,6 +418,13 @@ struct VariantData {
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:
const TAG_VALID_SPAN_LOCAL: u8 = 0;
const TAG_VALID_SPAN_FOREIGN: u8 = 1;
@ -440,4 +447,5 @@ pub fn provide(providers: &mut Providers) {
IncoherentImpls,
CrateRoot,
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<()> {
type ByteArray = [u8; 1];

View file

@ -1157,6 +1157,7 @@
/// Determines whether an item is annotated with `doc(hidden)`.
query is_doc_hidden(def_id: DefId) -> bool {
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)`.

View file

@ -1310,7 +1310,8 @@ pub fn reveal_opaque_types_in_bounds<'tcx>(
}
/// 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)
.filter_map(|attr| attr.meta_item_list())
.any(|items| items.iter().any(|item| item.has_name(sym::hidden)))

View file

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