Skip to content

Commit

Permalink
rustc_metadata: Encode doc(hidden) flag to metadata
Browse files Browse the repository at this point in the history
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 rust-lang#107054 because resolver cannot use memoization of query results yet.
  • Loading branch information
petrochenkov committed Jan 21, 2023
1 parent 005fc0f commit 415c141
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 18 deletions.
1 change: 1 addition & 0 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4326,6 +4326,7 @@ dependencies = [
name = "rustc_metadata"
version = "0.0.0"
dependencies = [
"bitflags",
"libloading",
"odht",
"rustc_ast",
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_metadata/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1594,8 +1594,8 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
})
}

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 {
Expand Down
8 changes: 6 additions & 2 deletions compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -338,6 +339,7 @@ provide! { tcx, def_id, other, cdata,
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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -631,7 +633,9 @@ impl CStore {
}

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)
}
}

Expand Down
19 changes: 15 additions & 4 deletions compiler/rustc_metadata/src/rmeta/encoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1111,15 +1111,26 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
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);
}
}

Expand Down
10 changes: 9 additions & 1 deletion compiler/rustc_metadata/src/rmeta/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ define_tables! {
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.
Expand All @@ -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;
Expand All @@ -440,4 +447,5 @@ trivially_parameterized_over_tcx! {
IncoherentImpls,
CrateRoot,
CrateDep,
AttrFlags,
}
14 changes: 14 additions & 0 deletions compiler/rustc_metadata/src/rmeta/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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];

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_middle/src/query/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,7 @@ rustc_queries! {
/// 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)`.
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/ty/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)))
Expand Down
9 changes: 1 addition & 8 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2498,14 +2498,7 @@ impl Import {
}

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))
}
}

Expand Down

0 comments on commit 415c141

Please sign in to comment.