Skip to content

Commit f096d7d

Browse files
committed
Auto merge of rust-lang#133345 - GuillaumeGomez:stop-cloning-context, r=<try>
Stop cloning `Context` so much This is a first step for rust-lang#82381. It's already big enough so I'll continue in a follow-up once this PR is merged. Next step will be to get rid of `SharedContext` by inlining it directly into `Context`. cc `@camelid` r? `@notriddle`
2 parents f2abf82 + 1697c6f commit f096d7d

File tree

9 files changed

+322
-285
lines changed

9 files changed

+322
-285
lines changed

src/librustdoc/formats/renderer.rs

+40-32
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
use rustc_data_structures::profiling::SelfProfilerRef;
12
use rustc_middle::ty::TyCtxt;
2-
use tracing::debug;
33

44
use crate::clean;
55
use crate::config::RenderOptions;
@@ -17,6 +17,7 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
1717
///
1818
/// This is true for html, and false for json. See #80664
1919
const RUN_ON_MODULE: bool;
20+
type InfoType;
2021

2122
/// Sets up any state required for the renderer. When this is called the cache has already been
2223
/// populated.
@@ -28,7 +29,8 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
2829
) -> Result<(Self, clean::Crate), Error>;
2930

3031
/// Make a new renderer to render a child of the item currently being rendered.
31-
fn make_child_renderer(&self) -> Self;
32+
fn make_child_renderer(&mut self) -> Self::InfoType;
33+
fn set_back_info(&mut self, _info: Self::InfoType);
3234

3335
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
3436
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
@@ -47,6 +49,40 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
4749
fn cache(&self) -> &Cache;
4850
}
4951

52+
fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
53+
cx: &mut T,
54+
item: clean::Item,
55+
prof: &SelfProfilerRef,
56+
) -> Result<(), Error> {
57+
if item.is_mod() && T::RUN_ON_MODULE {
58+
// modules are special because they add a namespace. We also need to
59+
// recurse into the items of the module as well.
60+
let _timer =
61+
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
62+
63+
cx.mod_item_in(&item)?;
64+
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) =
65+
item.inner.kind
66+
else {
67+
unreachable!()
68+
};
69+
for it in module.items {
70+
let info = cx.make_child_renderer();
71+
run_format_inner(cx, it, prof)?;
72+
cx.set_back_info(info);
73+
}
74+
75+
cx.mod_item_out()?;
76+
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
77+
// cases. Use an explicit match instead.
78+
} else if let Some(item_name) = item.name
79+
&& !item.is_extern_crate()
80+
{
81+
prof.generic_activity_with_arg("render_item", item_name.as_str()).run(|| cx.item(item))?;
82+
}
83+
Ok(())
84+
}
85+
5086
/// Main method for rendering a crate.
5187
pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
5288
krate: clean::Crate,
@@ -66,36 +102,8 @@ pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
66102
}
67103

68104
// Render the crate documentation
69-
let mut work = vec![(format_renderer.make_child_renderer(), krate.module)];
70-
71-
while let Some((mut cx, item)) = work.pop() {
72-
if item.is_mod() && T::RUN_ON_MODULE {
73-
// modules are special because they add a namespace. We also need to
74-
// recurse into the items of the module as well.
75-
let _timer =
76-
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
77-
78-
cx.mod_item_in(&item)?;
79-
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) =
80-
item.inner.kind
81-
else {
82-
unreachable!()
83-
};
84-
for it in module.items {
85-
debug!("Adding {:?} to worklist", it.name);
86-
work.push((cx.make_child_renderer(), it));
87-
}
88-
89-
cx.mod_item_out()?;
90-
// FIXME: checking `item.name.is_some()` is very implicit and leads to lots of special
91-
// cases. Use an explicit match instead.
92-
} else if let Some(item_name) = item.name
93-
&& !item.is_extern_crate()
94-
{
95-
prof.generic_activity_with_arg("render_item", item_name.as_str())
96-
.run(|| cx.item(item))?;
97-
}
98-
}
105+
run_format_inner(&mut format_renderer, krate.module, prof)?;
106+
99107
prof.verbose_generic_activity_with_arg("renderer_after_krate", T::descr())
100108
.run(|| format_renderer.after_krate())
101109
}

src/librustdoc/html/markdown.rs

+66-51
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use std::sync::OnceLock;
3737
use pulldown_cmark::{
3838
BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
3939
};
40-
use rustc_data_structures::fx::FxHashMap;
40+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4141
use rustc_errors::{Diag, DiagMessage};
4242
use rustc_hir::def_id::LocalDefId;
4343
use rustc_middle::ty::TyCtxt;
@@ -1889,71 +1889,81 @@ pub struct IdMap {
18891889
existing_footnotes: usize,
18901890
}
18911891

1892-
// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
1893-
static DEFAULT_ID_MAP: OnceLock<FxHashMap<Cow<'static, str>, usize>> = OnceLock::new();
1892+
// The map is pre-initialized and then can be used as is to prevent cloning it for each item
1893+
// (in the sidebar rendering).
1894+
static DEFAULT_ID_MAP: OnceLock<FxHashSet<&'static str>> = OnceLock::new();
18941895

1895-
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
1896-
let mut map = FxHashMap::default();
1896+
fn init_id_map() -> FxHashSet<&'static str> {
1897+
let mut map = FxHashSet::default();
18971898
// This is the list of IDs used in JavaScript.
1898-
map.insert("help".into(), 1);
1899-
map.insert("settings".into(), 1);
1900-
map.insert("not-displayed".into(), 1);
1901-
map.insert("alternative-display".into(), 1);
1902-
map.insert("search".into(), 1);
1903-
map.insert("crate-search".into(), 1);
1904-
map.insert("crate-search-div".into(), 1);
1899+
map.insert("help");
1900+
map.insert("settings");
1901+
map.insert("not-displayed");
1902+
map.insert("alternative-display");
1903+
map.insert("search");
1904+
map.insert("crate-search");
1905+
map.insert("crate-search-div");
19051906
// This is the list of IDs used in HTML generated in Rust (including the ones
19061907
// used in tera template files).
1907-
map.insert("themeStyle".into(), 1);
1908-
map.insert("settings-menu".into(), 1);
1909-
map.insert("help-button".into(), 1);
1910-
map.insert("sidebar-button".into(), 1);
1911-
map.insert("main-content".into(), 1);
1912-
map.insert("toggle-all-docs".into(), 1);
1913-
map.insert("all-types".into(), 1);
1914-
map.insert("default-settings".into(), 1);
1915-
map.insert("sidebar-vars".into(), 1);
1916-
map.insert("copy-path".into(), 1);
1917-
map.insert("rustdoc-toc".into(), 1);
1918-
map.insert("rustdoc-modnav".into(), 1);
1908+
map.insert("themeStyle");
1909+
map.insert("settings-menu");
1910+
map.insert("help-button");
1911+
map.insert("sidebar-button");
1912+
map.insert("main-content");
1913+
map.insert("toggle-all-docs");
1914+
map.insert("all-types");
1915+
map.insert("default-settings");
1916+
map.insert("sidebar-vars");
1917+
map.insert("copy-path");
1918+
map.insert("rustdoc-toc");
1919+
map.insert("rustdoc-modnav");
19191920
// This is the list of IDs used by rustdoc sections (but still generated by
19201921
// rustdoc).
1921-
map.insert("fields".into(), 1);
1922-
map.insert("variants".into(), 1);
1923-
map.insert("implementors-list".into(), 1);
1924-
map.insert("synthetic-implementors-list".into(), 1);
1925-
map.insert("foreign-impls".into(), 1);
1926-
map.insert("implementations".into(), 1);
1927-
map.insert("trait-implementations".into(), 1);
1928-
map.insert("synthetic-implementations".into(), 1);
1929-
map.insert("blanket-implementations".into(), 1);
1930-
map.insert("required-associated-types".into(), 1);
1931-
map.insert("provided-associated-types".into(), 1);
1932-
map.insert("provided-associated-consts".into(), 1);
1933-
map.insert("required-associated-consts".into(), 1);
1934-
map.insert("required-methods".into(), 1);
1935-
map.insert("provided-methods".into(), 1);
1936-
map.insert("dyn-compatibility".into(), 1);
1937-
map.insert("implementors".into(), 1);
1938-
map.insert("synthetic-implementors".into(), 1);
1939-
map.insert("implementations-list".into(), 1);
1940-
map.insert("trait-implementations-list".into(), 1);
1941-
map.insert("synthetic-implementations-list".into(), 1);
1942-
map.insert("blanket-implementations-list".into(), 1);
1943-
map.insert("deref-methods".into(), 1);
1944-
map.insert("layout".into(), 1);
1945-
map.insert("aliased-type".into(), 1);
1922+
map.insert("fields");
1923+
map.insert("variants");
1924+
map.insert("implementors-list");
1925+
map.insert("synthetic-implementors-list");
1926+
map.insert("foreign-impls");
1927+
map.insert("implementations");
1928+
map.insert("trait-implementations");
1929+
map.insert("synthetic-implementations");
1930+
map.insert("blanket-implementations");
1931+
map.insert("required-associated-types");
1932+
map.insert("provided-associated-types");
1933+
map.insert("provided-associated-consts");
1934+
map.insert("required-associated-consts");
1935+
map.insert("required-methods");
1936+
map.insert("provided-methods");
1937+
map.insert("dyn-compatibility");
1938+
map.insert("implementors");
1939+
map.insert("synthetic-implementors");
1940+
map.insert("implementations-list");
1941+
map.insert("trait-implementations-list");
1942+
map.insert("synthetic-implementations-list");
1943+
map.insert("blanket-implementations-list");
1944+
map.insert("deref-methods");
1945+
map.insert("layout");
1946+
map.insert("aliased-type");
19461947
map
19471948
}
19481949

19491950
impl IdMap {
19501951
pub fn new() -> Self {
1951-
IdMap { map: DEFAULT_ID_MAP.get_or_init(init_id_map).clone(), existing_footnotes: 0 }
1952+
IdMap { map: FxHashMap::default(), existing_footnotes: 0 }
19521953
}
19531954

19541955
pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
19551956
let id = match self.map.get_mut(candidate.as_ref()) {
1956-
None => candidate.to_string(),
1957+
None => {
1958+
let candidate = candidate.to_string();
1959+
if DEFAULT_ID_MAP.get_or_init(init_id_map).contains(candidate.as_str()) {
1960+
let id = format!("{}-{}", candidate, 1);
1961+
self.map.insert(candidate.into(), 2);
1962+
id
1963+
} else {
1964+
candidate
1965+
}
1966+
}
19571967
Some(a) => {
19581968
let id = format!("{}-{}", candidate.as_ref(), *a);
19591969
*a += 1;
@@ -1973,4 +1983,9 @@ impl IdMap {
19731983
closure(self, &mut existing_footnotes);
19741984
self.existing_footnotes = existing_footnotes;
19751985
}
1986+
1987+
pub(crate) fn clear(&mut self) {
1988+
self.map.clear();
1989+
self.existing_footnotes = 0;
1990+
}
19761991
}

0 commit comments

Comments
 (0)