Skip to content

rustdoc: Compute some fields of clean::Crate on-demand to reduce size #90391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Oct 31, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>,
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note to self: remove this field as well.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: In order to remove this field, Cache::populate will need to take a DocContext, which I think is possible but will require some changes. I'm also working on that as part of #90365, so I'll save removing this field for a future PR.

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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice idea to add this check! 👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks :)


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 @@ -405,7 +405,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 @@ -416,14 +416,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 @@ -444,7 +444,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 @@ -455,10 +455,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 @@ -473,9 +473,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 @@ -599,7 +600,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 @@ -611,7 +612,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