Skip to content

Commit

Permalink
Auto merge of rust-lang#90391 - camelid:crate-size, r=jyn514
Browse files Browse the repository at this point in the history
rustdoc: Compute some fields of `clean::Crate` on-demand to reduce size

`clean::Crate` is frequently moved by-value -- for example, in `DocFolder`
implementations -- so reducing its size should improve performance.

This PR reduces the size of `clean::Crate` from 168 bytes to 104 bytes.

r? `@jyn514`
  • Loading branch information
bors committed Oct 31, 2021
2 parents 0a09858 + a58e214 commit 38b01d9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
19 changes: 17 additions & 2 deletions src/librustdoc/clean/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,9 @@ impl From<DefId> for ItemId {
}
}

/// The crate currently being documented.
#[derive(Clone, Debug)]
crate struct Crate {
crate name: Symbol,
crate src: FileName,
crate module: Item,
crate externs: Vec<ExternalCrate>,
crate primitives: ThinVec<(DefId, PrimitiveType)>,
Expand All @@ -125,6 +124,20 @@ crate struct Crate {
crate collapsed: bool,
}

// `Crate` is frequently moved by-value. Make sure it doesn't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
rustc_data_structures::static_assert_size!(Crate, 104);

impl Crate {
crate fn name(&self, tcx: TyCtxt<'_>) -> Symbol {
ExternalCrate::LOCAL.name(tcx)
}

crate fn src(&self, tcx: TyCtxt<'_>) -> FileName {
ExternalCrate::LOCAL.src(tcx)
}
}

/// This struct is used to wrap additional information added by rustdoc on a `trait` item.
#[derive(Clone, Debug)]
crate struct TraitWithExtraInfo {
Expand All @@ -138,6 +151,8 @@ crate struct ExternalCrate {
}

impl ExternalCrate {
const LOCAL: Self = Self { crate_num: LOCAL_CRATE };

#[inline]
crate fn def_id(&self) -> DefId {
DefId { krate: self.crate_num, index: CRATE_DEF_INDEX }
Expand Down
7 changes: 1 addition & 6 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,11 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
let module = crate::visit_ast::RustdocVisitor::new(cx).visit();

let mut externs = Vec::new();
for &cnum in cx.tcx.crates(()).iter() {
for &cnum in cx.tcx.crates(()) {
externs.push(ExternalCrate { crate_num: cnum });
// Analyze doc-reachability for extern items
LibEmbargoVisitor::new(cx).visit_lib(cnum);
}
externs.sort_unstable_by_key(|e| e.crate_num);

// Clean the crate, translating the entire librustc_ast AST to one that is
// understood by rustdoc.
Expand All @@ -57,8 +56,6 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
}

let local_crate = ExternalCrate { crate_num: LOCAL_CRATE };
let src = local_crate.src(cx.tcx);
let name = local_crate.name(cx.tcx);
let primitives = local_crate.primitives(cx.tcx);
let keywords = local_crate.keywords(cx.tcx);
{
Expand All @@ -80,8 +77,6 @@ crate fn krate(cx: &mut DocContext<'_>) -> Crate {
}

Crate {
name,
src,
module,
externs,
primitives,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
// Collect the index into a string
format!(
r#""{}":{}"#,
krate.name,
krate.name(tcx),
serde_json::to_string(&CrateData {
doc: crate_doc,
items: crate_items,
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
..
} = options;

let src_root = match krate.src {
let src_root = match krate.src(tcx) {
FileName::Real(ref p) => match p.local_path_if_available().parent() {
Some(p) => p.to_path_buf(),
None => PathBuf::new(),
Expand All @@ -419,14 +419,14 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
let mut playground = None;
if let Some(url) = playground_url {
playground =
Some(markdown::Playground { crate_name: Some(krate.name.to_string()), url });
Some(markdown::Playground { crate_name: Some(krate.name(tcx).to_string()), url });
}
let mut layout = layout::Layout {
logo: String::new(),
favicon: String::new(),
external_html,
default_settings,
krate: krate.name.to_string(),
krate: krate.name(tcx).to_string(),
css_file_extension: extension_css,
generate_search_filter,
scrape_examples_extension: !call_locations.is_empty(),
Expand All @@ -447,7 +447,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}
(sym::html_playground_url, Some(s)) => {
playground = Some(markdown::Playground {
crate_name: Some(krate.name.to_string()),
crate_name: Some(krate.name(tcx).to_string()),
url: s.to_string(),
});
}
Expand Down
13 changes: 7 additions & 6 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,10 +456,10 @@ pub(super) fn write_shared(
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
let make_sources = || {
let (mut all_sources, _krates) =
try_err!(collect(&dst, &krate.name.as_str(), "sourcesIndex"), &dst);
try_err!(collect(&dst, &krate.name(cx.tcx()).as_str(), "sourcesIndex"), &dst);
all_sources.push(format!(
"sourcesIndex[\"{}\"] = {};",
&krate.name,
&krate.name(cx.tcx()),
hierarchy.to_json_string()
));
all_sources.sort();
Expand All @@ -474,9 +474,10 @@ pub(super) fn write_shared(

// Update the search index and crate list.
let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
let (mut all_indexes, mut krates) = try_err!(collect_json(&dst, &krate.name.as_str()), &dst);
let (mut all_indexes, mut krates) =
try_err!(collect_json(&dst, &krate.name(cx.tcx()).as_str()), &dst);
all_indexes.push(search_index);
krates.push(krate.name.to_string());
krates.push(krate.name(cx.tcx()).to_string());
krates.sort();

// Sort the indexes by crate so the file will be generated identically even
Expand Down Expand Up @@ -600,7 +601,7 @@ pub(super) fn write_shared(

let implementors = format!(
r#"implementors["{}"] = {};"#,
krate.name,
krate.name(cx.tcx()),
serde_json::to_string(&implementors).unwrap()
);

Expand All @@ -612,7 +613,7 @@ pub(super) fn write_shared(
mydst.push(&format!("{}.{}.js", remote_item_type, remote_path[remote_path.len() - 1]));

let (mut all_implementors, _) =
try_err!(collect(&mydst, &krate.name.as_str(), "implementors"), &mydst);
try_err!(collect(&mydst, &krate.name(cx.tcx()).as_str(), "implementors"), &mydst);
all_implementors.push(implementors);
// Sort the implementors by crate so the file will be generated
// identically even with rustdoc running in parallel.
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/sources.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::path::{Component, Path, PathBuf};

crate fn render(cx: &mut Context<'_>, krate: clean::Crate) -> Result<clean::Crate, Error> {
info!("emitting source files");
let dst = cx.dst.join("src").join(&*krate.name.as_str());
let dst = cx.dst.join("src").join(&*krate.name(cx.tcx()).as_str());
cx.shared.ensure_dir(&dst)?;
let mut folder = SourceCollector { dst, cx, emitted_local_sources: FxHashSet::default() };
Ok(folder.fold_crate(krate))
Expand Down

0 comments on commit 38b01d9

Please sign in to comment.