Skip to content

rustdoc: Move Cache from Context to SharedContext #88222

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 1 commit into from
Aug 26, 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
29 changes: 10 additions & 19 deletions src/librustdoc/html/render/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,6 @@ crate struct Context<'tcx> {
///
/// [#82381]: https://github.com/rust-lang/rust/issues/82381
crate shared: Rc<SharedContext<'tcx>>,
/// The [`Cache`] used during rendering.
///
/// Ideally the cache would be in [`SharedContext`], but it's mutated
/// between when the `SharedContext` is created and when `Context`
/// is created, so more refactoring would be needed.
///
/// It's immutable once in `Context`, so it's not as bad that it's not in
/// `SharedContext`.
// FIXME: move `cache` to `SharedContext`
crate cache: Rc<Cache>,
/// This flag indicates whether `[src]` links should be generated or not. If
/// the source files are present in the html rendering, then this will be
/// `true`.
Expand All @@ -80,7 +70,7 @@ crate struct Context<'tcx> {

// `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(Context<'_>, 112);
rustc_data_structures::static_assert_size!(Context<'_>, 104);

/// Shared mutable state used in [`Context`] and elsewhere.
crate struct SharedContext<'tcx> {
Expand Down Expand Up @@ -132,6 +122,8 @@ crate struct SharedContext<'tcx> {
/// Correspondance map used to link types used in the source code pages to allow to click on
/// links to jump to the type's definition.
crate span_correspondance_map: FxHashMap<rustc_span::Span, LinkFromSrc>,
/// The [`Cache`] used during rendering.
crate cache: Cache,
}

impl SharedContext<'_> {
Expand Down Expand Up @@ -162,7 +154,7 @@ impl<'tcx> Context<'tcx> {
}

pub(crate) fn cache(&self) -> &Cache {
&self.cache
&self.shared.cache
}

pub(super) fn sess(&self) -> &'tcx Session {
Expand Down Expand Up @@ -237,7 +229,7 @@ impl<'tcx> Context<'tcx> {
&self.shared.style_files,
)
} else {
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_def_id()) {
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.def_id.expect_def_id()) {
let mut path = String::new();
for name in &names[..names.len() - 1] {
path.push_str(name);
Expand Down Expand Up @@ -326,7 +318,7 @@ impl<'tcx> Context<'tcx> {
return None;
}
} else {
let (krate, src_root) = match *self.cache.extern_locations.get(&cnum)? {
let (krate, src_root) = match *self.cache().extern_locations.get(&cnum)? {
ExternalLocation::Local => {
let e = ExternalCrate { crate_num: cnum };
(e.name(self.tcx()), e.src_root(self.tcx()))
Expand Down Expand Up @@ -487,6 +479,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
show_type_layout,
templates,
span_correspondance_map: matches,
cache,
};

// Add the default themes to the `Vec` of stylepaths
Expand All @@ -511,7 +504,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
render_redirect_pages: false,
id_map: RefCell::new(id_map),
shared: Rc::new(scx),
cache: Rc::new(cache),
include_sources,
};

Expand All @@ -520,7 +512,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}

// Build our search index
let index = build_index(&krate, Rc::get_mut(&mut cx.cache).unwrap(), tcx);
let index = build_index(&krate, &mut Rc::get_mut(&mut cx.shared).unwrap().cache, tcx);

// Write shared runs within a flock; disable thread dispatching of IO temporarily.
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
Expand All @@ -536,7 +528,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
render_redirect_pages: self.render_redirect_pages,
id_map: RefCell::new(IdMap::new()),
shared: Rc::clone(&self.shared),
cache: Rc::clone(&self.cache),
include_sources: self.include_sources,
}
}
Expand All @@ -561,7 +552,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
extra_scripts: &[],
static_extra_scripts: &[],
};
let sidebar = if let Some(ref version) = self.cache.crate_version {
let sidebar = if let Some(ref version) = self.shared.cache.crate_version {
format!(
"<h2 class=\"location\">Crate {}</h2>\
<div class=\"block version\">\
Expand Down Expand Up @@ -722,7 +713,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
}

fn cache(&self) -> &Cache {
&self.cache
&self.shared.cache
}
}

Expand Down
35 changes: 17 additions & 18 deletions src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1012,11 +1012,11 @@ fn render_assoc_items(
what: AssocItemRender<'_>,
) {
info!("Documenting associated items of {:?}", containing_item.name);
let v = match cx.cache.impls.get(&it) {
let cache = cx.cache();
let v = match cache.impls.get(&it) {
Some(v) => v,
None => return,
};
let cache = cx.cache();
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
if !non_trait.is_empty() {
let render_mode = match what {
Expand Down Expand Up @@ -1063,11 +1063,11 @@ fn render_assoc_items(
if !traits.is_empty() {
let deref_impl = traits
.iter()
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did);
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cache.deref_trait_did);
if let Some(impl_) = deref_impl {
let has_deref_mut = traits
.iter()
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_mut_trait_did);
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cache.deref_mut_trait_did);
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
}
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
Expand Down Expand Up @@ -1122,6 +1122,7 @@ fn render_deref_methods(
container_item: &clean::Item,
deref_mut: bool,
) {
let cache = cx.cache();
let deref_type = impl_.inner_impl().trait_.as_ref().unwrap();
let (target, real_target) = impl_
.inner_impl()
Expand All @@ -1138,8 +1139,8 @@ fn render_deref_methods(
debug!("Render deref methods for {:#?}, target {:#?}", impl_.inner_impl().for_, target);
let what =
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
if let Some(did) = target.def_id_full(cx.cache()) {
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cx.cache()) {
if let Some(did) = target.def_id_full(cache) {
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cache) {
// `impl Deref<Target = S> for S`
if did == type_did {
// Avoid infinite cycles
Expand All @@ -1149,7 +1150,7 @@ fn render_deref_methods(
render_assoc_items(w, cx, container_item, did, what);
} else {
if let Some(prim) = target.primitive_type() {
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
if let Some(&did) = cache.primitive_locations.get(&prim) {
render_assoc_items(w, cx, container_item, did, what);
}
}
Expand Down Expand Up @@ -1286,7 +1287,7 @@ fn render_impl(
let render_method_item = match render_mode {
RenderMode::Normal => true,
RenderMode::ForDeref { mut_: deref_mut_ } => {
should_render_item(&item, deref_mut_, &cx.cache)
should_render_item(&item, deref_mut_, cx.cache())
}
};

Expand Down Expand Up @@ -1678,7 +1679,7 @@ fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
}

if it.is_crate() {
if let Some(ref version) = cx.cache.crate_version {
if let Some(ref version) = cx.cache().crate_version {
write!(
buffer,
"<div class=\"block version\">\
Expand Down Expand Up @@ -1825,18 +1826,16 @@ fn small_url_encode(s: String) -> String {

fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
let did = it.def_id.expect_def_id();
if let Some(v) = cx.cache.impls.get(&did) {
let cache = cx.cache();
if let Some(v) = cache.impls.get(&did) {
let mut used_links = FxHashSet::default();
let cache = cx.cache();

{
let used_links_bor = &mut used_links;
let mut ret = v
.iter()
.filter(|i| i.inner_impl().trait_.is_none())
.flat_map(move |i| {
get_methods(i.inner_impl(), false, used_links_bor, false, &cx.cache)
})
.flat_map(move |i| get_methods(i.inner_impl(), false, used_links_bor, false, cache))
.collect::<Vec<_>>();
if !ret.is_empty() {
// We want links' order to be reproducible so we don't use unstable sort.
Expand All @@ -1857,7 +1856,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
if let Some(impl_) = v
.iter()
.filter(|i| i.inner_impl().trait_.is_some())
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did)
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cache.deref_trait_did)
{
sidebar_deref_methods(cx, out, impl_, v);
}
Expand Down Expand Up @@ -2117,15 +2116,15 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
"</div>",
);

if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) {
let cache = cx.cache();
let cache = cx.cache();
if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) {
let mut res = implementors
.iter()
.filter(|i| {
i.inner_impl()
.for_
.def_id_full(cache)
.map_or(false, |d| !cx.cache.paths.contains_key(&d))
.map_or(false, |d| !cache.paths.contains_key(&d))
})
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx))
.collect::<Vec<_>>();
Expand Down
12 changes: 5 additions & 7 deletions src/librustdoc/html/render/print_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -690,7 +690,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
// If there are methods directly on this trait object, render them here.
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All);

if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) {
let cache = cx.cache();
if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) {
// The DefId is for the first Type found with that name. The bool is
// if any Types with the same name but different DefId have been found.
let mut implementor_dups: FxHashMap<Symbol, (DefId, bool)> = FxHashMap::default();
Expand All @@ -712,10 +713,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
}

let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
i.inner_impl()
.for_
.def_id_full(cx.cache())
.map_or(true, |d| cx.cache.paths.contains_key(&d))
i.inner_impl().for_.def_id_full(cache).map_or(true, |d| cache.paths.contains_key(&d))
});

let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) =
Expand Down Expand Up @@ -772,7 +770,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
it,
w,
&implementor_dups,
&collect_paths_for_type(implementor.inner_impl().for_.clone(), &cx.cache),
&collect_paths_for_type(implementor.inner_impl().for_.clone(), cache),
);
}
w.write_str("</div>");
Expand Down Expand Up @@ -806,7 +804,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
path = if it.def_id.is_local() {
cx.current.join("/")
} else {
let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_def_id()];
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
path[..path.len() - 1].join("/")
},
ty = it.type_(),
Expand Down
11 changes: 6 additions & 5 deletions src/librustdoc/html/render/write_shared.rs
Original file line number Diff line number Diff line change
Expand Up @@ -518,17 +518,18 @@ pub(super) fn write_shared(

// Update the list of all implementors for traits
let dst = cx.dst.join("implementors");
for (&did, imps) in &cx.cache.implementors {
let cache = cx.cache();
for (&did, imps) in &cache.implementors {
// Private modules can leak through to this phase of rustdoc, which
// could contain implementations for otherwise private types. In some
// rare cases we could find an implementation for an item which wasn't
// indexed, so we just skip this step in that case.
//
// FIXME: this is a vague explanation for why this can't be a `get`, in
// theory it should be...
let &(ref remote_path, remote_item_type) = match cx.cache.paths.get(&did) {
let &(ref remote_path, remote_item_type) = match cache.paths.get(&did) {
Some(p) => p,
None => match cx.cache.external_paths.get(&did) {
None => match cache.external_paths.get(&did) {
Some(p) => p,
None => continue,
},
Expand Down Expand Up @@ -557,7 +558,7 @@ pub(super) fn write_shared(
Some(Implementor {
text: imp.inner_impl().print(false, cx).to_string(),
synthetic: imp.inner_impl().synthetic,
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cx.cache()),
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
})
}
})
Expand All @@ -566,7 +567,7 @@ pub(super) fn write_shared(
// Only create a js file if we have impls to add to it. If the trait is
// documented locally though we always create the file to avoid dead
// links.
if implementors.is_empty() && !cx.cache.paths.contains_key(&did) {
if implementors.is_empty() && !cache.paths.contains_key(&did) {
continue;
}

Expand Down