提交 e25e15ce 编写于 作者: G Guillaume Gomez

Store tcx and cache when they are used multiple times instead of calling functions every time

上级 0cde8552
......@@ -855,10 +855,12 @@ fn render_impls(
traits: &[&&Impl],
containing_item: &clean::Item,
) {
let cache = cx.cache();
let tcx = cx.tcx();
let mut impls = traits
.iter()
.map(|i| {
let did = i.trait_did_full(cx.cache()).unwrap();
let did = i.trait_did_full(cache).unwrap();
let assoc_link = AssocItemLink::GotoSource(did, &i.inner_impl().provided_trait_methods);
let mut buffer = if w.is_for_html() { Buffer::html() } else { Buffer::new() };
render_impl(
......@@ -868,8 +870,8 @@ fn render_impls(
containing_item,
assoc_link,
RenderMode::Normal,
containing_item.stable_since(cx.tcx()).as_deref(),
containing_item.const_stable_since(cx.tcx()).as_deref(),
containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(),
true,
None,
false,
......@@ -911,14 +913,16 @@ fn assoc_const(
extra: &str,
cx: &Context<'_>,
) {
let cache = cx.cache();
let tcx = cx.tcx();
write!(
w,
"{}{}const <a href=\"{}\" class=\"constant\"><b>{}</b></a>: {}",
extra,
it.visibility.print_with_space(cx.tcx(), it.def_id, cx.cache()),
naive_assoc_href(it, link, cx.cache()),
it.visibility.print_with_space(tcx, it.def_id, cache),
naive_assoc_href(it, link, cache),
it.name.as_ref().unwrap(),
ty.print(cx.cache(), cx.tcx())
ty.print(cache, tcx)
);
}
......@@ -993,6 +997,8 @@ fn method(
parent: ItemType,
cx: &Context<'_>,
) {
let cache = cx.cache();
let tcx = cx.tcx();
let name = meth.name.as_ref().unwrap();
let anchor = format!("#{}.{}", meth.type_(), name);
let href = match link {
......@@ -1007,18 +1013,17 @@ fn method(
ItemType::TyMethod
};
href(did, cx.cache()).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
href(did, cache).map(|p| format!("{}#{}.{}", p.0, ty, name)).unwrap_or(anchor)
}
};
let tcx = cx.tcx();
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cx.cache()).to_string();
let vis = meth.visibility.print_with_space(tcx, meth.def_id, cache).to_string();
let constness = header.constness.print_with_space();
let asyncness = header.asyncness.print_with_space();
let unsafety = header.unsafety.print_with_space();
let defaultness = print_default_space(meth.is_default());
let abi = print_abi_with_space(header.abi).to_string();
// NOTE: `{:#}` does not print HTML formatting, `{}` does. So `g.print` can't be reused between the length calculation and `write!`.
let generics_len = format!("{:#}", g.print(cx.cache(), tcx)).len();
let generics_len = format!("{:#}", g.print(cache, tcx)).len();
let mut header_len = "fn ".len()
+ vis.len()
+ constness.len()
......@@ -1050,10 +1055,10 @@ fn method(
abi,
href = href,
name = name,
generics = g.print(cx.cache(), cx.tcx()),
decl = d.full_print(cx.cache(), cx.tcx(), header_len, indent, header.asyncness),
spotlight = spotlight_decl(&d, cx.cache(), cx.tcx()),
where_clause = print_where_clause(g, cx.cache(), cx.tcx(), indent, end_newline),
generics = g.print(cache, tcx),
decl = d.full_print(cache, tcx, header_len, indent, header.asyncness),
spotlight = spotlight_decl(&d, cache, tcx),
where_clause = print_where_clause(g, cache, tcx, indent, end_newline),
)
}
match *item.kind {
......@@ -1156,6 +1161,8 @@ fn render_assoc_items(
Some(v) => v,
None => return,
};
let tcx = cx.tcx();
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 {
......@@ -1170,12 +1177,10 @@ fn render_assoc_items(
AssocItemRender::DerefFor { trait_, type_, deref_mut_ } => {
let id = cx.derive_id(small_url_encode(format!(
"deref-methods-{:#}",
type_.print(cx.cache(), cx.tcx())
type_.print(cache, tcx)
)));
debug!("Adding {} to deref id map", type_.print(cx.cache(), cx.tcx()));
cx.deref_id_map
.borrow_mut()
.insert(type_.def_id_full(cx.cache()).unwrap(), id.clone());
debug!("Adding {} to deref id map", type_.print(cache, tcx));
cx.deref_id_map.borrow_mut().insert(type_.def_id_full(cache).unwrap(), id.clone());
write!(
w,
"<h2 id=\"{id}\" class=\"small-section-header\">\
......@@ -1183,8 +1188,8 @@ fn render_assoc_items(
<a href=\"#{id}\" class=\"anchor\"></a>\
</h2>",
id = id,
trait_ = trait_.print(cx.cache(), cx.tcx()),
type_ = type_.print(cx.cache(), cx.tcx()),
trait_ = trait_.print(cache, tcx),
type_ = type_.print(cache, tcx),
);
RenderMode::ForDeref { mut_: deref_mut_ }
}
......@@ -1197,8 +1202,8 @@ fn render_assoc_items(
containing_item,
AssocItemLink::Anchor(None),
render_mode,
containing_item.stable_since(cx.tcx()).as_deref(),
containing_item.const_stable_since(cx.tcx()).as_deref(),
containing_item.stable_since(tcx).as_deref(),
containing_item.const_stable_since(tcx).as_deref(),
true,
None,
false,
......@@ -1210,11 +1215,11 @@ fn render_assoc_items(
if !traits.is_empty() {
let deref_impl = traits
.iter()
.find(|t| t.inner_impl().trait_.def_id_full(cx.cache()) == cx.cache.deref_trait_did);
.find(|t| t.inner_impl().trait_.def_id_full(cache) == cx.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(cx.cache()) == cx.cache.deref_mut_trait_did
});
let has_deref_mut = traits
.iter()
.any(|t| t.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_mut_trait_did);
render_deref_methods(w, cx, impl_, containing_item, has_deref_mut);
}
......@@ -1415,18 +1420,17 @@ fn render_impl(
aliases: &[String],
) {
let traits = &cx.cache.traits;
let trait_ = i.trait_did_full(cx.cache()).map(|did| &traits[&did]);
let tcx = cx.tcx();
let cache = cx.cache();
let trait_ = i.trait_did_full(cache).map(|did| &traits[&did]);
if render_mode == RenderMode::Normal {
let id = cx.derive_id(match i.inner_impl().trait_ {
Some(ref t) => {
if is_on_foreign_type {
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cx.cache(), cx.tcx())
get_id_for_impl_on_foreign_type(&i.inner_impl().for_, t, cache, tcx)
} else {
format!(
"impl-{}",
small_url_encode(format!("{:#}", t.print(cx.cache(), cx.tcx())))
)
format!("impl-{}", small_url_encode(format!("{:#}", t.print(cache, tcx))))
}
}
None => "impl".to_string(),
......@@ -1438,7 +1442,7 @@ fn render_impl(
};
if let Some(use_absolute) = use_absolute {
write!(w, "<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">", id, aliases);
write!(w, "{}", i.inner_impl().print(cx.cache(), use_absolute, cx.tcx()));
write!(w, "{}", i.inner_impl().print(cache, use_absolute, tcx));
if show_def_docs {
for it in &i.inner_impl().items {
if let clean::TypedefItem(ref tydef, _) = *it.kind {
......@@ -1450,8 +1454,8 @@ fn render_impl(
Some(&tydef.type_),
AssocItemLink::Anchor(None),
"",
cx.cache(),
cx.tcx(),
cache,
tcx,
);
w.write_str(";</span>");
}
......@@ -1464,14 +1468,14 @@ fn render_impl(
"<h3 id=\"{}\" class=\"impl\"{}><code class=\"in-band\">{}</code>",
id,
aliases,
i.inner_impl().print(cx.cache(), false, cx.tcx())
i.inner_impl().print(cache, false, tcx)
);
}
write!(w, "<a href=\"#{}\" class=\"anchor\"></a>", id);
render_stability_since_raw(
w,
i.impl_item.stable_since(cx.tcx()).as_deref(),
i.impl_item.const_stable_since(cx.tcx()).as_deref(),
i.impl_item.stable_since(tcx).as_deref(),
i.impl_item.const_stable_since(tcx).as_deref(),
outer_version,
outer_const_version,
);
......@@ -1517,6 +1521,7 @@ fn doc_impl_item(
) {
let item_type = item.type_();
let name = item.name.as_ref().unwrap();
let tcx = cx.tcx();
let render_method_item = match render_mode {
RenderMode::Normal => true,
......@@ -1544,8 +1549,8 @@ fn doc_impl_item(
w.write_str("</code>");
render_stability_since_raw(
w,
item.stable_since(cx.tcx()).as_deref(),
item.const_stable_since(cx.tcx()).as_deref(),
item.stable_since(tcx).as_deref(),
item.const_stable_since(tcx).as_deref(),
outer_version,
outer_const_version,
);
......@@ -1564,7 +1569,7 @@ fn doc_impl_item(
link.anchor(&id),
"",
cx.cache(),
cx.tcx(),
tcx,
);
w.write_str("</code></h4>");
}
......@@ -1575,8 +1580,8 @@ fn doc_impl_item(
w.write_str("</code>");
render_stability_since_raw(
w,
item.stable_since(cx.tcx()).as_deref(),
item.const_stable_since(cx.tcx()).as_deref(),
item.stable_since(tcx).as_deref(),
item.const_stable_since(tcx).as_deref(),
outer_version,
outer_const_version,
);
......@@ -1594,7 +1599,7 @@ fn doc_impl_item(
link.anchor(&id),
"",
cx.cache(),
cx.tcx(),
tcx,
);
w.write_str("</code></h4>");
}
......@@ -1898,6 +1903,8 @@ fn small_url_encode(s: String) -> String {
fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
if let Some(v) = cx.cache.impls.get(&it.def_id) {
let mut used_links = FxHashSet::default();
let tcx = cx.tcx();
let cache = cx.cache();
{
let used_links_bor = &mut used_links;
......@@ -1927,7 +1934,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(cx.cache()) == cx.cache.deref_trait_did)
.find(|i| i.inner_impl().trait_.def_id_full(cache) == cx.cache.deref_trait_did)
{
sidebar_deref_methods(cx, out, impl_, v);
}
......@@ -1938,10 +1945,9 @@ fn sidebar_assoc_items(cx: &Context<'_>, out: &mut Buffer, it: &clean::Item) {
.iter()
.filter_map(|it| {
if let Some(ref i) = it.inner_impl().trait_ {
let i_display = format!("{:#}", i.print(cx.cache(), cx.tcx()));
let i_display = format!("{:#}", i.print(cache, tcx));
let out = Escape(&i_display);
let encoded =
small_url_encode(format!("{:#}", i.print(cx.cache(), cx.tcx())));
let encoded = small_url_encode(format!("{:#}", i.print(cache, tcx)));
let generated = format!(
"<a href=\"#impl-{}\">{}{}</a>",
encoded,
......@@ -2018,8 +2024,8 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
})
{
debug!("found target, real_target: {:?} {:?}", target, real_target);
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(c) {
if let Some(type_did) = impl_.inner_impl().for_.def_id_full(c) {
// `impl Deref<Target = S> for S`
if did == type_did {
// Avoid infinite cycles
......@@ -2030,9 +2036,9 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
let deref_mut = v
.iter()
.filter(|i| i.inner_impl().trait_.is_some())
.any(|i| i.inner_impl().trait_.def_id_full(cx.cache()) == c.deref_mut_trait_did);
.any(|i| i.inner_impl().trait_.def_id_full(c) == c.deref_mut_trait_did);
let inner_impl = target
.def_id_full(cx.cache())
.def_id_full(c)
.or_else(|| {
target.primitive_type().and_then(|prim| c.primitive_locations.get(&prim).cloned())
})
......@@ -2048,7 +2054,7 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
if !ret.is_empty() {
let deref_id_map = cx.deref_id_map.borrow();
let id = deref_id_map
.get(&real_target.def_id_full(cx.cache()).unwrap())
.get(&real_target.def_id_full(c).unwrap())
.expect("Deref section without derived id");
write!(
out,
......@@ -2071,12 +2077,12 @@ fn sidebar_deref_methods(cx: &Context<'_>, out: &mut Buffer, impl_: &Impl, v: &V
}
// Recurse into any further impls that might exist for `target`
if let Some(target_did) = target.def_id_full(cx.cache()) {
if let Some(target_did) = target.def_id_full(c) {
if let Some(target_impls) = c.impls.get(&target_did) {
if let Some(target_deref_impl) = target_impls
.iter()
.filter(|i| i.inner_impl().trait_.is_some())
.find(|i| i.inner_impl().trait_.def_id_full(cx.cache()) == c.deref_trait_did)
.find(|i| i.inner_impl().trait_.def_id_full(c) == c.deref_trait_did)
{
sidebar_deref_methods(cx, out, target_deref_impl, target_impls);
}
......@@ -2214,15 +2220,17 @@ fn print_sidebar_section(
);
if let Some(implementors) = cx.cache.implementors.get(&it.def_id) {
let cache = cx.cache();
let tcx = cx.tcx();
let mut res = implementors
.iter()
.filter(|i| {
i.inner_impl()
.for_
.def_id_full(cx.cache())
.def_id_full(cache)
.map_or(false, |d| !cx.cache.paths.contains_key(&d))
})
.filter_map(|i| extract_for_impl_name(&i.impl_item, cx.cache(), cx.tcx()))
.filter_map(|i| extract_for_impl_name(&i.impl_item, cache, tcx))
.collect::<Vec<_>>();
if !res.is_empty() {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册