Skip to content

Commit 3307929

Browse files
Store only the current depth
Previously we stored the entire current path which is a bit expensive and only ever accessed its length. This stores the length directly.
1 parent 0e079c2 commit 3307929

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/librustdoc/html/format.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc::hir;
1515

1616
use crate::clean::{self, PrimitiveType};
1717
use crate::html::item_type::ItemType;
18-
use crate::html::render::{self, cache, CURRENT_LOCATION_KEY};
18+
use crate::html::render::{self, cache, CURRENT_DEPTH};
1919

2020
/// Helper to render an optional visibility with a space after it (if the
2121
/// visibility is preset)
@@ -407,16 +407,16 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
407407
return None
408408
}
409409

410-
let loc = CURRENT_LOCATION_KEY.with(|l| l.borrow().clone());
410+
let depth = CURRENT_DEPTH.with(|l| l.get());
411411
let (fqp, shortty, mut url) = match cache.paths.get(&did) {
412412
Some(&(ref fqp, shortty)) => {
413-
(fqp, shortty, "../".repeat(loc.len()))
413+
(fqp, shortty, "../".repeat(depth))
414414
}
415415
None => {
416416
let &(ref fqp, shortty) = cache.external_paths.get(&did)?;
417417
(fqp, shortty, match cache.extern_locations[&did.krate] {
418418
(.., render::Remote(ref s)) => s.to_string(),
419-
(.., render::Local) => "../".repeat(loc.len()),
419+
(.., render::Local) => "../".repeat(depth),
420420
(.., render::Unknown) => return None,
421421
})
422422
}
@@ -479,7 +479,7 @@ fn primitive_link(f: &mut fmt::Formatter<'_>,
479479
if !f.alternate() {
480480
match m.primitive_locations.get(&prim) {
481481
Some(&def_id) if def_id.is_local() => {
482-
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
482+
let len = CURRENT_DEPTH.with(|s| s.get());
483483
let len = if len == 0 {0} else {len - 1};
484484
write!(f, "<a class=\"primitive\" href=\"{}primitive.{}.html\">",
485485
"../".repeat(len),
@@ -492,7 +492,7 @@ fn primitive_link(f: &mut fmt::Formatter<'_>,
492492
Some((cname, s.to_string()))
493493
}
494494
(ref cname, _, render::Local) => {
495-
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
495+
let len = CURRENT_DEPTH.with(|s| s.get());
496496
Some((cname, "../".repeat(len)))
497497
}
498498
(.., render::Unknown) => None,

src/librustdoc/html/render.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
pub use self::ExternalLocation::*;
2929

3030
use std::borrow::Cow;
31-
use std::cell::RefCell;
31+
use std::cell::{Cell, RefCell};
3232
use std::cmp::Ordering;
3333
use std::collections::{BTreeMap, VecDeque};
3434
use std::default::Default;
@@ -479,7 +479,7 @@ impl ToJson for IndexItemFunctionType {
479479
}
480480

481481
thread_local!(static CACHE_KEY: RefCell<Arc<Cache>> = Default::default());
482-
thread_local!(pub static CURRENT_LOCATION_KEY: RefCell<Vec<String>> = RefCell::new(Vec::new()));
482+
thread_local!(pub static CURRENT_DEPTH: Cell<usize> = Cell::new(0));
483483

484484
pub fn initial_ids() -> Vec<String> {
485485
[
@@ -695,7 +695,7 @@ pub fn run(mut krate: clean::Crate,
695695
// for future parallelization opportunities
696696
let cache = Arc::new(cache);
697697
CACHE_KEY.with(|v| *v.borrow_mut() = cache.clone());
698-
CURRENT_LOCATION_KEY.with(|s| s.borrow_mut().clear());
698+
CURRENT_DEPTH.with(|s| s.set(0));
699699

700700
// Write shared runs within a flock; disable thread dispatching of IO temporarily.
701701
Arc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
@@ -2003,8 +2003,8 @@ impl Context {
20032003
-> io::Result<()> {
20042004
// A little unfortunate that this is done like this, but it sure
20052005
// does make formatting *a lot* nicer.
2006-
CURRENT_LOCATION_KEY.with(|slot| {
2007-
*slot.borrow_mut() = self.current.clone();
2006+
CURRENT_DEPTH.with(|slot| {
2007+
slot.set(self.current.len());
20082008
});
20092009

20102010
let mut title = if it.is_primitive() || it.is_keyword() {

0 commit comments

Comments
 (0)