Skip to content

Commit 6e0b0d4

Browse files
Move cache into Context, avoid TLS
This doesn't move everything over as cache() is pretty annoying to remove fully, but it gets the ball rolling.
1 parent f5ed0fd commit 6e0b0d4

File tree

1 file changed

+26
-29
lines changed

1 file changed

+26
-29
lines changed

src/librustdoc/html/render.rs

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,7 @@ struct Context {
167167
/// The map used to ensure all generated 'id=' attributes are unique.
168168
id_map: Rc<RefCell<IdMap>>,
169169
pub shared: Arc<SharedContext>,
170+
pub cache: Arc<Cache>,
170171
}
171172

172173
crate struct SharedContext {
@@ -477,31 +478,31 @@ pub fn run(mut krate: clean::Crate,
477478
let dst = output;
478479
scx.ensure_dir(&dst)?;
479480
krate = sources::render(&dst, &mut scx, krate)?;
481+
let (new_crate, index, cache) = Cache::from_krate(
482+
renderinfo,
483+
&extern_html_root_urls,
484+
&dst,
485+
krate,
486+
);
487+
krate = new_crate;
488+
let cache = Arc::new(cache);
480489
let mut cx = Context {
481490
current: Vec::new(),
482491
dst,
483492
render_redirect_pages: false,
484493
id_map: Rc::new(RefCell::new(id_map)),
485494
shared: Arc::new(scx),
495+
cache: cache.clone(),
486496
};
487497

488-
let (new_crate, index, cache) = Cache::from_krate(
489-
renderinfo,
490-
&extern_html_root_urls,
491-
&cx.dst,
492-
krate,
493-
);
494-
krate = new_crate;
495-
496498
// Freeze the cache now that the index has been built. Put an Arc into TLS
497499
// for future parallelization opportunities
498-
let cache = Arc::new(cache);
499500
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
500501
CURRENT_DEPTH.with(|s| s.set(0));
501502

502503
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
503504
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
504-
write_shared(&cx, &krate, &*cache, index, &md_opts, diag)?;
505+
write_shared(&cx, &krate, index, &md_opts, diag)?;
505506
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
506507

507508
// And finally render the whole crate's documentation
@@ -519,7 +520,6 @@ pub fn run(mut krate: clean::Crate,
519520
fn write_shared(
520521
cx: &Context,
521522
krate: &clean::Crate,
522-
cache: &Cache,
523523
search_index: String,
524524
options: &RenderOptions,
525525
diag: &errors::Handler,
@@ -750,7 +750,7 @@ themePicker.onblur = handleThemeButtonsBlur;
750750
{
751751
let (mut all_aliases, _, _) = try_err!(collect(&dst, &krate.name, "ALIASES", false), &dst);
752752
let mut output = String::with_capacity(100);
753-
for (alias, items) in &cache.aliases {
753+
for (alias, items) in &cx.cache.aliases {
754754
if items.is_empty() {
755755
continue
756756
}
@@ -920,17 +920,17 @@ themePicker.onblur = handleThemeButtonsBlur;
920920

921921
// Update the list of all implementors for traits
922922
let dst = cx.dst.join("implementors");
923-
for (&did, imps) in &cache.implementors {
923+
for (&did, imps) in &cx.cache.implementors {
924924
// Private modules can leak through to this phase of rustdoc, which
925925
// could contain implementations for otherwise private types. In some
926926
// rare cases we could find an implementation for an item which wasn't
927927
// indexed, so we just skip this step in that case.
928928
//
929929
// FIXME: this is a vague explanation for why this can't be a `get`, in
930930
// theory it should be...
931-
let &(ref remote_path, remote_item_type) = match cache.paths.get(&did) {
931+
let &(ref remote_path, remote_item_type) = match cx.cache.paths.get(&did) {
932932
Some(p) => p,
933-
None => match cache.external_paths.get(&did) {
933+
None => match cx.cache.external_paths.get(&did) {
934934
Some(p) => p,
935935
None => continue,
936936
}
@@ -958,7 +958,7 @@ themePicker.onblur = handleThemeButtonsBlur;
958958
// Only create a js file if we have impls to add to it. If the trait is
959959
// documented locally though we always create the file to avoid dead
960960
// links.
961-
if !have_impls && !cache.paths.contains_key(&did) {
961+
if !have_impls && !cx.cache.paths.contains_key(&did) {
962962
continue;
963963
}
964964

@@ -1309,7 +1309,7 @@ impl Context {
13091309
extra_scripts: &[],
13101310
static_extra_scripts: &[],
13111311
};
1312-
let sidebar = if let Some(ref version) = cache().crate_version {
1312+
let sidebar = if let Some(ref version) = self.cache.crate_version {
13131313
format!("<p class='location'>Crate {}</p>\
13141314
<div class='block version'>\
13151315
<p>Version {}</p>\
@@ -1399,7 +1399,7 @@ impl Context {
13991399
&self.shared.themes)
14001400
} else {
14011401
let mut url = self.root_path();
1402-
if let Some(&(ref names, ty)) = cache().paths.get(&it.def_id) {
1402+
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id) {
14031403
for name in &names[..names.len() - 1] {
14041404
url.push_str(name);
14051405
url.push_str("/");
@@ -1549,7 +1549,6 @@ impl Context {
15491549
fn src_href(&self, item: &clean::Item) -> Option<String> {
15501550
let mut root = self.root_path();
15511551

1552-
let cache = cache();
15531552
let mut path = String::new();
15541553

15551554
// We can safely ignore macros from other libraries
@@ -1565,7 +1564,7 @@ impl Context {
15651564
return None;
15661565
}
15671566
} else {
1568-
let (krate, src_root) = match *cache.extern_locations.get(&item.def_id.krate)? {
1567+
let (krate, src_root) = match *self.cache.extern_locations.get(&item.def_id.krate)? {
15691568
(ref name, ref src, Local) => (name, src),
15701569
(ref name, ref src, Remote(ref s)) => {
15711570
root = s.to_string();
@@ -2475,11 +2474,9 @@ fn item_trait(
24752474
// If there are methods directly on this trait object, render them here.
24762475
render_assoc_items(w, cx, it, it.def_id, AssocItemRender::All);
24772476

2478-
let cache = cache();
2479-
24802477
let mut synthetic_types = Vec::new();
24812478

2482-
if let Some(implementors) = cache.implementors.get(&it.def_id) {
2479+
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
24832480
// The DefId is for the first Type found with that name. The bool is
24842481
// if any Types with the same name but different DefId have been found.
24852482
let mut implementor_dups: FxHashMap<&str, (DefId, bool)> = FxHashMap::default();
@@ -2502,7 +2499,7 @@ fn item_trait(
25022499

25032500
let (local, foreign) = implementors.iter()
25042501
.partition::<Vec<_>, _>(|i| i.inner_impl().for_.def_id()
2505-
.map_or(true, |d| cache.paths.contains_key(&d)));
2502+
.map_or(true, |d| cx.cache.paths.contains_key(&d)));
25062503

25072504

25082505
let (mut synthetic, mut concrete): (Vec<&&Impl>, Vec<&&Impl>) = local.iter()
@@ -2567,7 +2564,7 @@ fn item_trait(
25672564
path = if it.def_id.is_local() {
25682565
cx.current.join("/")
25692566
} else {
2570-
let (ref path, _) = cache.external_paths[&it.def_id];
2567+
let (ref path, _) = cx.cache.external_paths[&it.def_id];
25712568
path[..path.len() - 1].join("/")
25722569
},
25732570
ty = it.type_(),
@@ -3144,7 +3141,7 @@ fn render_assoc_items(w: &mut Buffer,
31443141
containing_item: &clean::Item,
31453142
it: DefId,
31463143
what: AssocItemRender<'_>) {
3147-
let c = cache();
3144+
let c = &cx.cache;
31483145
let v = match c.impls.get(&it) {
31493146
Some(v) => v,
31503147
None => return,
@@ -3250,7 +3247,7 @@ fn render_deref_methods(w: &mut Buffer, cx: &Context, impl_: &Impl,
32503247
render_assoc_items(w, cx, container_item, did, what)
32513248
} else {
32523249
if let Some(prim) = target.primitive_type() {
3253-
if let Some(&did) = cache().primitive_locations.get(&prim) {
3250+
if let Some(&did) = cx.cache.primitive_locations.get(&prim) {
32543251
render_assoc_items(w, cx, container_item, did, what);
32553252
}
32563253
}
@@ -3500,7 +3497,7 @@ fn render_impl(w: &mut Buffer, cx: &Context, i: &Impl, link: AssocItemLink<'_>,
35003497
}
35013498
}
35023499

3503-
let traits = &cache().traits;
3500+
let traits = &cx.cache.traits;
35043501
let trait_ = i.trait_did().map(|did| &traits[&did]);
35053502

35063503
write!(w, "<div class='impl-items'>");
@@ -3642,7 +3639,7 @@ fn print_sidebar(cx: &Context, it: &clean::Item, buffer: &mut Buffer) {
36423639
}
36433640

36443641
if it.is_crate() {
3645-
if let Some(ref version) = cache().crate_version {
3642+
if let Some(ref version) = cx.cache.crate_version {
36463643
write!(buffer,
36473644
"<div class='block version'>\
36483645
<p>Version {}</p>\

0 commit comments

Comments
 (0)