Skip to content

Commit 3b3ce37

Browse files
committed
Auto merge of #88222 - jyn514:shared-cache, r=GuillaumeGomez
rustdoc: Move Cache from Context to SharedContext r? `@camelid`
2 parents 76e755c + 90ac1ab commit 3b3ce37

File tree

4 files changed

+38
-49
lines changed

4 files changed

+38
-49
lines changed

Diff for: src/librustdoc/html/render/context.rs

+10-19
Original file line numberDiff line numberDiff line change
@@ -62,16 +62,6 @@ crate struct Context<'tcx> {
6262
///
6363
/// [#82381]: https://github.com/rust-lang/rust/issues/82381
6464
crate shared: Rc<SharedContext<'tcx>>,
65-
/// The [`Cache`] used during rendering.
66-
///
67-
/// Ideally the cache would be in [`SharedContext`], but it's mutated
68-
/// between when the `SharedContext` is created and when `Context`
69-
/// is created, so more refactoring would be needed.
70-
///
71-
/// It's immutable once in `Context`, so it's not as bad that it's not in
72-
/// `SharedContext`.
73-
// FIXME: move `cache` to `SharedContext`
74-
crate cache: Rc<Cache>,
7565
/// This flag indicates whether `[src]` links should be generated or not. If
7666
/// the source files are present in the html rendering, then this will be
7767
/// `true`.
@@ -80,7 +70,7 @@ crate struct Context<'tcx> {
8070

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

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

137129
impl SharedContext<'_> {
@@ -162,7 +154,7 @@ impl<'tcx> Context<'tcx> {
162154
}
163155

164156
pub(crate) fn cache(&self) -> &Cache {
165-
&self.cache
157+
&self.shared.cache
166158
}
167159

168160
pub(super) fn sess(&self) -> &'tcx Session {
@@ -237,7 +229,7 @@ impl<'tcx> Context<'tcx> {
237229
&self.shared.style_files,
238230
)
239231
} else {
240-
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id.expect_def_id()) {
232+
if let Some(&(ref names, ty)) = self.cache().paths.get(&it.def_id.expect_def_id()) {
241233
let mut path = String::new();
242234
for name in &names[..names.len() - 1] {
243235
path.push_str(name);
@@ -326,7 +318,7 @@ impl<'tcx> Context<'tcx> {
326318
return None;
327319
}
328320
} else {
329-
let (krate, src_root) = match *self.cache.extern_locations.get(&cnum)? {
321+
let (krate, src_root) = match *self.cache().extern_locations.get(&cnum)? {
330322
ExternalLocation::Local => {
331323
let e = ExternalCrate { crate_num: cnum };
332324
(e.name(self.tcx()), e.src_root(self.tcx()))
@@ -487,6 +479,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
487479
show_type_layout,
488480
templates,
489481
span_correspondance_map: matches,
482+
cache,
490483
};
491484

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

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

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

525517
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
526518
Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
@@ -536,7 +528,6 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
536528
render_redirect_pages: self.render_redirect_pages,
537529
id_map: RefCell::new(IdMap::new()),
538530
shared: Rc::clone(&self.shared),
539-
cache: Rc::clone(&self.cache),
540531
include_sources: self.include_sources,
541532
}
542533
}
@@ -561,7 +552,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
561552
extra_scripts: &[],
562553
static_extra_scripts: &[],
563554
};
564-
let sidebar = if let Some(ref version) = self.cache.crate_version {
555+
let sidebar = if let Some(ref version) = self.shared.cache.crate_version {
565556
format!(
566557
"<h2 class=\"location\">Crate {}</h2>\
567558
<div class=\"block version\">\
@@ -722,7 +713,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
722713
}
723714

724715
fn cache(&self) -> &Cache {
725-
&self.cache
716+
&self.shared.cache
726717
}
727718
}
728719

Diff for: src/librustdoc/html/render/mod.rs

+17-18
Original file line numberDiff line numberDiff line change
@@ -1012,11 +1012,11 @@ fn render_assoc_items(
10121012
what: AssocItemRender<'_>,
10131013
) {
10141014
info!("Documenting associated items of {:?}", containing_item.name);
1015-
let v = match cx.cache.impls.get(&it) {
1015+
let cache = cx.cache();
1016+
let v = match cache.impls.get(&it) {
10161017
Some(v) => v,
10171018
None => return,
10181019
};
1019-
let cache = cx.cache();
10201020
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
10211021
if !non_trait.is_empty() {
10221022
let render_mode = match what {
@@ -1063,11 +1063,11 @@ fn render_assoc_items(
10631063
if !traits.is_empty() {
10641064
let deref_impl = traits
10651065
.iter()
1066-
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did);
1066+
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cache.deref_trait_did);
10671067
if let Some(impl_) = deref_impl {
10681068
let has_deref_mut = traits
10691069
.iter()
1070-
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_mut_trait_did);
1070+
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cache.deref_mut_trait_did);
10711071
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
10721072
}
10731073
let (synthetic, concrete): (Vec<&&Impl>, Vec<&&Impl>) =
@@ -1122,6 +1122,7 @@ fn render_deref_methods(
11221122
container_item: &clean::Item,
11231123
deref_mut: bool,
11241124
) {
1125+
let cache = cx.cache();
11251126
let deref_type = impl_.inner_impl().trait_.as_ref().unwrap();
11261127
let (target, real_target) = impl_
11271128
.inner_impl()
@@ -1138,8 +1139,8 @@ fn render_deref_methods(
11381139
debug!("Render deref methods for {:#?}, target {:#?}", impl_.inner_impl().for_, target);
11391140
let what =
11401141
AssocItemRender::DerefFor { trait_: deref_type, type_: real_target, deref_mut_: deref_mut };
1141-
if let Some(did) = target.def_id_full(cx.cache()) {
1142-
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cx.cache()) {
1142+
if let Some(did) = target.def_id_full(cache) {
1143+
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cache) {
11431144
// `impl Deref<Target = S> for S`
11441145
if did == type_did {
11451146
// Avoid infinite cycles
@@ -1149,7 +1150,7 @@ fn render_deref_methods(
11491150
render_assoc_items(w, cx, container_item, did, what);
11501151
} else {
11511152
if let Some(prim) = target.primitive_type() {
1152-
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
1153+
if let Some(&did) = cache.primitive_locations.get(&prim) {
11531154
render_assoc_items(w, cx, container_item, did, what);
11541155
}
11551156
}
@@ -1286,7 +1287,7 @@ fn render_impl(
12861287
let render_method_item = match render_mode {
12871288
RenderMode::Normal => true,
12881289
RenderMode::ForDeref { mut_: deref_mut_ } => {
1289-
should_render_item(&item, deref_mut_, &cx.cache)
1290+
should_render_item(&item, deref_mut_, cx.cache())
12901291
}
12911292
};
12921293

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

16801681
if it.is_crate() {
1681-
if let Some(ref version) = cx.cache.crate_version {
1682+
if let Some(ref version) = cx.cache().crate_version {
16821683
write!(
16831684
buffer,
16841685
"<div class=\"block version\">\
@@ -1825,18 +1826,16 @@ fn small_url_encode(s: String) -> String {
18251826

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

18321833
{
18331834
let used_links_bor = &mut used_links;
18341835
let mut ret = v
18351836
.iter()
18361837
.filter(|i| i.inner_impl().trait_.is_none())
1837-
.flat_map(move |i| {
1838-
get_methods(i.inner_impl(), false, used_links_bor, false, &cx.cache)
1839-
})
1838+
.flat_map(move |i| get_methods(i.inner_impl(), false, used_links_bor, false, cache))
18401839
.collect::<Vec<_>>();
18411840
if !ret.is_empty() {
18421841
// 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) {
18571856
if let Some(impl_) = v
18581857
.iter()
18591858
.filter(|i| i.inner_impl().trait_.is_some())
1860-
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did)
1859+
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cache.deref_trait_did)
18611860
{
18621861
sidebar_deref_methods(cx, out, impl_, v);
18631862
}
@@ -2117,15 +2116,15 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
21172116
"</div>",
21182117
);
21192118

2120-
if let Some(implementors) = cx.cache.implementors.get(&it.def_id.expect_def_id()) {
2121-
let cache = cx.cache();
2119+
let cache = cx.cache();
2120+
if let Some(implementors) = cache.implementors.get(&it.def_id.expect_def_id()) {
21222121
let mut res = implementors
21232122
.iter()
21242123
.filter(|i| {
21252124
i.inner_impl()
21262125
.for_
21272126
.def_id_full(cache)
2128-
.map_or(false, |d| !cx.cache.paths.contains_key(&d))
2127+
.map_or(false, |d| !cache.paths.contains_key(&d))
21292128
})
21302129
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx))
21312130
.collect::<Vec<_>>();

Diff for: src/librustdoc/html/render/print_item.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,8 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
690690
// If there are methods directly on this trait object, render them here.
691691
render_assoc_items(w, cx, it, it.def_id.expect_def_id(), AssocItemRender::All);
692692

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

714715
let (local, foreign) = implementors.iter().partition::<Vec<_>, _>(|i| {
715-
i.inner_impl()
716-
.for_
717-
.def_id_full(cx.cache())
718-
.map_or(true, |d| cx.cache.paths.contains_key(&d))
716+
i.inner_impl().for_.def_id_full(cache).map_or(true, |d| cache.paths.contains_key(&d))
719717
});
720718

721719
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
772770
it,
773771
w,
774772
&implementor_dups,
775-
&collect_paths_for_type(implementor.inner_impl().for_.clone(), &cx.cache),
773+
&collect_paths_for_type(implementor.inner_impl().for_.clone(), cache),
776774
);
777775
}
778776
w.write_str("</div>");
@@ -806,7 +804,7 @@ fn item_trait(w: &mut Buffer, cx: &Context<'_>, it: &clean::Item, t: &clean::Tra
806804
path = if it.def_id.is_local() {
807805
cx.current.join("/")
808806
} else {
809-
let (ref path, _) = cx.cache.external_paths[&it.def_id.expect_def_id()];
807+
let (ref path, _) = cache.external_paths[&it.def_id.expect_def_id()];
810808
path[..path.len() - 1].join("/")
811809
},
812810
ty = it.type_(),

Diff for: src/librustdoc/html/render/write_shared.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -518,17 +518,18 @@ pub(super) fn write_shared(
518518

519519
// Update the list of all implementors for traits
520520
let dst = cx.dst.join("implementors");
521-
for (&did, imps) in &cx.cache.implementors {
521+
let cache = cx.cache();
522+
for (&did, imps) in &cache.implementors {
522523
// Private modules can leak through to this phase of rustdoc, which
523524
// could contain implementations for otherwise private types. In some
524525
// rare cases we could find an implementation for an item which wasn't
525526
// indexed, so we just skip this step in that case.
526527
//
527528
// FIXME: this is a vague explanation for why this can't be a `get`, in
528529
// theory it should be...
529-
let &(ref remote_path, remote_item_type) = match cx.cache.paths.get(&did) {
530+
let &(ref remote_path, remote_item_type) = match cache.paths.get(&did) {
530531
Some(p) => p,
531-
None => match cx.cache.external_paths.get(&did) {
532+
None => match cache.external_paths.get(&did) {
532533
Some(p) => p,
533534
None => continue,
534535
},
@@ -557,7 +558,7 @@ pub(super) fn write_shared(
557558
Some(Implementor {
558559
text: imp.inner_impl().print(false, cx).to_string(),
559560
synthetic: imp.inner_impl().synthetic,
560-
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cx.cache()),
561+
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
561562
})
562563
}
563564
})
@@ -566,7 +567,7 @@ pub(super) fn write_shared(
566567
// Only create a js file if we have impls to add to it. If the trait is
567568
// documented locally though we always create the file to avoid dead
568569
// links.
569-
if implementors.is_empty() && !cx.cache.paths.contains_key(&did) {
570+
if implementors.is_empty() && !cache.paths.contains_key(&did) {
570571
continue;
571572
}
572573

0 commit comments

Comments
 (0)