From 90ac1ab26af51a08f2434b0201b23519013e9160 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Sun, 22 Aug 2021 03:36:37 +0000 Subject: [PATCH] Move Cache from Context to SharedContext --- src/librustdoc/html/render/context.rs | 29 +++++++----------- src/librustdoc/html/render/mod.rs | 35 +++++++++++----------- src/librustdoc/html/render/print_item.rs | 12 ++++---- src/librustdoc/html/render/write_shared.rs | 11 +++---- 4 files changed, 38 insertions(+), 49 deletions(-) diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index 6ce0828e159..733bedfdde9 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -62,16 +62,6 @@ crate struct Context<'tcx> { /// /// [#82381]: https://github.com/rust-lang/rust/issues/82381 crate shared: Rc>, - /// 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, /// 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`. @@ -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> { @@ -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, + /// The [`Cache`] used during rendering. + crate cache: Cache, } impl SharedContext<'_> { @@ -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 { @@ -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); @@ -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())) @@ -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 @@ -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, }; @@ -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); @@ -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, } } @@ -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!( "

Crate {}

\
\ @@ -722,7 +713,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { } fn cache(&self) -> &Cache { - &self.cache + &self.shared.cache } } diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index fd2e18a8be7..7704abc9a72 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -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 { @@ -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>) = @@ -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() @@ -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 for S` if did == type_did { // Avoid infinite cycles @@ -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); } } @@ -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()) } }; @@ -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, "
\ @@ -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::>(); if !ret.is_empty() { // We want links' order to be reproducible so we don't use unstable sort. @@ -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); } @@ -2117,15 +2116,15 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean "
", ); - 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::>(); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index 96cc67ce97c..8f4857a6939 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -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 = FxHashMap::default(); @@ -712,10 +713,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra } let (local, foreign) = implementors.iter().partition::, _>(|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>) = @@ -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("
"); @@ -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_(), diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index c16769c474a..99cd98f7eae 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -518,7 +518,8 @@ 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 @@ -526,9 +527,9 @@ pub(super) fn write_shared( // // 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, }, @@ -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), }) } }) @@ -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; }