Skip to content

Commit e25e15c

Browse files
Store tcx and cache when they are used multiple times instead of calling functions every time
1 parent 0cde855 commit e25e15c

File tree

1 file changed

+66
-58
lines changed
  • src/librustdoc/html/render

1 file changed

+66
-58
lines changed

src/librustdoc/html/render/mod.rs

Lines changed: 66 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -855,10 +855,12 @@ fn render_impls(
855855
traits: &[&&Impl],
856856
containing_item: &clean::Item,
857857
) {
858+
let cache = cx.cache();
859+
let tcx = cx.tcx();
858860
let mut impls = traits
859861
.iter()
860862
.map(|i| {
861-
let did = i.trait_did_full(cx.cache()).unwrap();
863+
let did = i.trait_did_full(cache).unwrap();
862864
let assoc_link = AssocItemLink::GotoSource(did, &i.inner_impl().provided_trait_methods);
863865
let mut buffer = if w.is_for_html() { Buffer::html() } else { Buffer::new() };
864866
render_impl(
@@ -868,8 +870,8 @@ fn render_impls(
868870
containing_item,
869871
assoc_link,
870872
RenderMode::Normal,
871-
containing_item.stable_since(cx.tcx()).as_deref(),
872-
containing_item.const_stable_since(cx.tcx()).as_deref(),
873+
containing_item.stable_since(tcx).as_deref(),
874+
containing_item.const_stable_since(tcx).as_deref(),
873875
true,
874876
None,
875877
false,
@@ -911,14 +913,16 @@ fn assoc_const(
911913
extra: &str,
912914
cx: &Context<'_>,
913915
) {
916+
let cache = cx.cache();
917+
let tcx = cx.tcx();
914918
write!(
915919
w,
916920
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
917921
extra,
918-
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
919-
naive_assoc_href(it, link, cx.cache()),
922+
it.visibility.print_with_space(tcx, it.def_id, cache),
923+
naive_assoc_href(it, link, cache),
920924
it.name.as_ref().unwrap(),
921-
ty.print(cx.cache(), cx.tcx())
925+
ty.print(cache, tcx)
922926
);
923927
}
924928

@@ -993,6 +997,8 @@ fn render_assoc_item(
993997
parent: ItemType,
994998
cx: &Context<'_>,
995999
) {
1000+
let cache = cx.cache();
1001+
let tcx = cx.tcx();
9961002
let name = meth.name.as_ref().unwrap();
9971003
let anchor = format!("#{}.{}", meth.type_(), name);
9981004
let href = match link {
@@ -1007,18 +1013,17 @@ fn render_assoc_item(
10071013
ItemType::TyMethod
10081014
};
10091015

1010-
href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
1016+
href(did, cache).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
10111017
}
10121018
};
1013-
let tcx = cx.tcx();
1014-
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cx.cache()).to_string();
1019+
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cache).to_string();
10151020
let constness = header.constness.print_with_space();
10161021
let asyncness = header.asyncness.print_with_space();
10171022
let unsafety = header.unsafety.print_with_space();
10181023
let defaultness = print_default_space(meth.is_default());
10191024
let abi = print_abi_with_space(header.abi).to_string();
10201025
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
1021-
let generics_len = format!("{:#}", g.print(cx.cache(), tcx)).len();
1026+
let generics_len = format!("{:#}", g.print(cache, tcx)).len();
10221027
let mut header_len = "fn ".len()
10231028
+ vis.len()
10241029
+ constness.len()
@@ -1050,10 +1055,10 @@ fn render_assoc_item(
10501055
abi,
10511056
href = href,
10521057
name = name,
1053-
generics = g.print(cx.cache(), cx.tcx()),
1054-
decl = d.full_print(cx.cache(), cx.tcx(), header_len, indent, header.asyncness),
1055-
spotlight = spotlight_decl(&d, cx.cache(), cx.tcx()),
1056-
where_clause = print_where_clause(g, cx.cache(), cx.tcx(), indent, end_newline),
1058+
generics = g.print(cache, tcx),
1059+
decl = d.full_print(cache, tcx, header_len, indent, header.asyncness),
1060+
spotlight = spotlight_decl(&d, cache, tcx),
1061+
where_clause = print_where_clause(g, cache, tcx, indent, end_newline),
10571062
)
10581063
}
10591064
match *item.kind {
@@ -1156,6 +1161,8 @@ fn render_assoc_items(
11561161
Some(v) => v,
11571162
None => return,
11581163
};
1164+
let tcx = cx.tcx();
1165+
let cache = cx.cache();
11591166
let (non_trait, traits): (Vec<_>, _) = v.iter().partition(|i| i.inner_impl().trait_.is_none());
11601167
if !non_trait.is_empty() {
11611168
let render_mode = match what {
@@ -1170,21 +1177,19 @@ fn render_assoc_items(
11701177
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
11711178
let id = cx.derive_id(small_url_encode(format!(
11721179
"deref-methods-{:#}",
1173-
type_.print(cx.cache(), cx.tcx())
1180+
type_.print(cache, tcx)
11741181
)));
1175-
debug!("Adding {} to deref id map", type_.print(cx.cache(), cx.tcx()));
1176-
cx.deref_id_map
1177-
.borrow_mut()
1178-
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
1182+
debug!("Adding {} to deref id map", type_.print(cache, tcx));
1183+
cx.deref_id_map.borrow_mut().insert(type_.def_id_full(cache).unwrap(), id.clone());
11791184
write!(
11801185
w,
11811186
"<h2 id=\"{id}\" class=\"small-section-header\">\
11821187
Methods from {trait_}&lt;Target = {type_}&gt;\
11831188
<a href=\"#{id}\" class=\"anchor\"></a>\
11841189
</h2>",
11851190
id = id,
1186-
trait_ = trait_.print(cx.cache(), cx.tcx()),
1187-
type_ = type_.print(cx.cache(), cx.tcx()),
1191+
trait_ = trait_.print(cache, tcx),
1192+
type_ = type_.print(cache, tcx),
11881193
);
11891194
RenderMode::ForDeref { mut_: deref_mut_ }
11901195
}
@@ -1197,8 +1202,8 @@ fn render_assoc_items(
11971202
containing_item,
11981203
AssocItemLink::Anchor(None),
11991204
render_mode,
1200-
containing_item.stable_since(cx.tcx()).as_deref(),
1201-
containing_item.const_stable_since(cx.tcx()).as_deref(),
1205+
containing_item.stable_since(tcx).as_deref(),
1206+
containing_item.const_stable_since(tcx).as_deref(),
12021207
true,
12031208
None,
12041209
false,
@@ -1210,11 +1215,11 @@ fn render_assoc_items(
12101215
if !traits.is_empty() {
12111216
let deref_impl = traits
12121217
.iter()
1213-
.find(|t| t.inner_impl().trait_.def_id_full(cx.cache()) == cx.cache.deref_trait_did);
1218+
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did);
12141219
if let Some(impl_) = deref_impl {
1215-
let has_deref_mut = traits.iter().any(|t| {
1216-
t.inner_impl().trait_.def_id_full(cx.cache()) == cx.cache.deref_mut_trait_did
1217-
});
1220+
let has_deref_mut = traits
1221+
.iter()
1222+
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_mut_trait_did);
12181223
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
12191224
}
12201225

@@ -1415,18 +1420,17 @@ fn render_impl(
14151420
aliases: &[String],
14161421
) {
14171422
let traits = &cx.cache.traits;
1418-
let trait_ = i.trait_did_full(cx.cache()).map(|did| &traits[&did]);
1423+
let tcx = cx.tcx();
1424+
let cache = cx.cache();
1425+
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
14191426

14201427
if render_mode == RenderMode::Normal {
14211428
let id = cx.derive_id(match i.inner_impl().trait_ {
14221429
Some(ref t) => {
14231430
if is_on_foreign_type {
1424-
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx.cache(), cx.tcx())
1431+
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cache, tcx)
14251432
} else {
1426-
format!(
1427-
"impl-{}",
1428-
small_url_encode(format!("{:#}", t.print(cx.cache(), cx.tcx())))
1429-
)
1433+
format!("impl-{}", small_url_encode(format!("{:#}", t.print(cache, tcx))))
14301434
}
14311435
}
14321436
None => "impl".to_string(),
@@ -1438,7 +1442,7 @@ fn render_impl(
14381442
};
14391443
if let Some(use_absolute) = use_absolute {
14401444
write!(w, "<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">", id, aliases);
1441-
write!(w, "{}", i.inner_impl().print(cx.cache(), use_absolute, cx.tcx()));
1445+
write!(w, "{}", i.inner_impl().print(cache, use_absolute, tcx));
14421446
if show_def_docs {
14431447
for it in &i.inner_impl().items {
14441448
if let clean::TypedefItem(ref tydef, _) = *it.kind {
@@ -1450,8 +1454,8 @@ fn render_impl(
14501454
Some(&tydef.type_),
14511455
AssocItemLink::Anchor(None),
14521456
"",
1453-
cx.cache(),
1454-
cx.tcx(),
1457+
cache,
1458+
tcx,
14551459
);
14561460
w.write_str(";</span>");
14571461
}
@@ -1464,14 +1468,14 @@ fn render_impl(
14641468
"<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
14651469
id,
14661470
aliases,
1467-
i.inner_impl().print(cx.cache(), false, cx.tcx())
1471+
i.inner_impl().print(cache, false, tcx)
14681472
);
14691473
}
14701474
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
14711475
render_stability_since_raw(
14721476
w,
1473-
i.impl_item.stable_since(cx.tcx()).as_deref(),
1474-
i.impl_item.const_stable_since(cx.tcx()).as_deref(),
1477+
i.impl_item.stable_since(tcx).as_deref(),
1478+
i.impl_item.const_stable_since(tcx).as_deref(),
14751479
outer_version,
14761480
outer_const_version,
14771481
);
@@ -1517,6 +1521,7 @@ fn render_impl(
15171521
) {
15181522
let item_type = item.type_();
15191523
let name = item.name.as_ref().unwrap();
1524+
let tcx = cx.tcx();
15201525

15211526
let render_method_item = match render_mode {
15221527
RenderMode::Normal => true,
@@ -1544,8 +1549,8 @@ fn render_impl(
15441549
w.write_str("</code>");
15451550
render_stability_since_raw(
15461551
w,
1547-
item.stable_since(cx.tcx()).as_deref(),
1548-
item.const_stable_since(cx.tcx()).as_deref(),
1552+
item.stable_since(tcx).as_deref(),
1553+
item.const_stable_since(tcx).as_deref(),
15491554
outer_version,
15501555
outer_const_version,
15511556
);
@@ -1564,7 +1569,7 @@ fn render_impl(
15641569
link.anchor(&id),
15651570
"",
15661571
cx.cache(),
1567-
cx.tcx(),
1572+
tcx,
15681573
);
15691574
w.write_str("</code></h4>");
15701575
}
@@ -1575,8 +1580,8 @@ fn render_impl(
15751580
w.write_str("</code>");
15761581
render_stability_since_raw(
15771582
w,
1578-
item.stable_since(cx.tcx()).as_deref(),
1579-
item.const_stable_since(cx.tcx()).as_deref(),
1583+
item.stable_since(tcx).as_deref(),
1584+
item.const_stable_since(tcx).as_deref(),
15801585
outer_version,
15811586
outer_const_version,
15821587
);
@@ -1594,7 +1599,7 @@ fn render_impl(
15941599
link.anchor(&id),
15951600
"",
15961601
cx.cache(),
1597-
cx.tcx(),
1602+
tcx,
15981603
);
15991604
w.write_str("</code></h4>");
16001605
}
@@ -1898,6 +1903,8 @@ fn small_url_encode(s: String) -> String {
18981903
fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
18991904
if let Some(v) = cx.cache.impls.get(&it.def_id) {
19001905
let mut used_links = FxHashSet::default();
1906+
let tcx = cx.tcx();
1907+
let cache = cx.cache();
19011908

19021909
{
19031910
let used_links_bor = &mut used_links;
@@ -1927,7 +1934,7 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
19271934
if let Some(impl_) = v
19281935
.iter()
19291936
.filter(|i| i.inner_impl().trait_.is_some())
1930-
.find(|i| i.inner_impl().trait_.def_id_full(cx.cache()) == cx.cache.deref_trait_did)
1937+
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did)
19311938
{
19321939
sidebar_deref_methods(cx, out, impl_, v);
19331940
}
@@ -1938,10 +1945,9 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
19381945
.iter()
19391946
.filter_map(|it| {
19401947
if let Some(ref i) = it.inner_impl().trait_ {
1941-
let i_display = format!("{:#}", i.print(cx.cache(), cx.tcx()));
1948+
let i_display = format!("{:#}", i.print(cache, tcx));
19421949
let out = Escape(&i_display);
1943-
let encoded =
1944-
small_url_encode(format!("{:#}", i.print(cx.cache(), cx.tcx())));
1950+
let encoded = small_url_encode(format!("{:#}", i.print(cache, tcx)));
19451951
let generated = format!(
19461952
"<a href=\"#impl-{}\">{}{}</a>",
19471953
encoded,
@@ -2018,8 +2024,8 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20182024
})
20192025
{
20202026
debug!("found target, real_target: {:?} {:?}", target, real_target);
2021-
if let Some(did) = target.def_id_full(cx.cache()) {
2022-
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(cx.cache()) {
2027+
if let Some(did) = target.def_id_full(c) {
2028+
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(c) {
20232029
// `impl Deref<Target = S> for S`
20242030
if did == type_did {
20252031
// Avoid infinite cycles
@@ -2030,9 +2036,9 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20302036
let deref_mut = v
20312037
.iter()
20322038
.filter(|i| i.inner_impl().trait_.is_some())
2033-
.any(|i| i.inner_impl().trait_.def_id_full(cx.cache()) == c.deref_mut_trait_did);
2039+
.any(|i| i.inner_impl().trait_.def_id_full(c) == c.deref_mut_trait_did);
20342040
let inner_impl = target
2035-
.def_id_full(cx.cache())
2041+
.def_id_full(c)
20362042
.or_else(|| {
20372043
target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned())
20382044
})
@@ -2048,7 +2054,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20482054
if !ret.is_empty() {
20492055
let deref_id_map = cx.deref_id_map.borrow();
20502056
let id = deref_id_map
2051-
.get(&real_target.def_id_full(cx.cache()).unwrap())
2057+
.get(&real_target.def_id_full(c).unwrap())
20522058
.expect("Deref section without derived id");
20532059
write!(
20542060
out,
@@ -2071,12 +2077,12 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
20712077
}
20722078

20732079
// Recurse into any further impls that might exist for `target`
2074-
if let Some(target_did) = target.def_id_full(cx.cache()) {
2080+
if let Some(target_did) = target.def_id_full(c) {
20752081
if let Some(target_impls) = c.impls.get(&target_did) {
20762082
if let Some(target_deref_impl) = target_impls
20772083
.iter()
20782084
.filter(|i| i.inner_impl().trait_.is_some())
2079-
.find(|i| i.inner_impl().trait_.def_id_full(cx.cache()) == c.deref_trait_did)
2085+
.find(|i| i.inner_impl().trait_.def_id_full(c) == c.deref_trait_did)
20802086
{
20812087
sidebar_deref_methods(cx, out, target_deref_impl, target_impls);
20822088
}
@@ -2214,15 +2220,17 @@ fn sidebar_trait(cx: &Context<'_>, buf: &mut Buffer, it: &clean::Item, t: &clean
22142220
);
22152221

22162222
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
2223+
let cache = cx.cache();
2224+
let tcx = cx.tcx();
22172225
let mut res = implementors
22182226
.iter()
22192227
.filter(|i| {
22202228
i.inner_impl()
22212229
.for_
2222-
.def_id_full(cx.cache())
2230+
.def_id_full(cache)
22232231
.map_or(false, |d| !cx.cache.paths.contains_key(&d))
22242232
})
2225-
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx.cache(), cx.tcx()))
2233+
.filter_map(|i| extract_for_impl_name(&i.impl_item, cache, tcx))
22262234
.collect::<Vec<_>>();
22272235

22282236
if !res.is_empty() {

0 commit comments

Comments
 (0)