Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 61a6981

Browse files
committedNov 22, 2024
Stop cloning Context so much
1 parent 717f5df commit 61a6981

File tree

7 files changed

+154
-120
lines changed

7 files changed

+154
-120
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

+64-49
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;
@@ -1890,65 +1890,74 @@ pub struct IdMap {
18901890
}
18911891

18921892
// 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();
1893+
static DEFAULT_ID_MAP: OnceLock<FxHashSet<Cow<'static, str>>> = OnceLock::new();
18941894

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

19491949
impl IdMap {
19501950
pub fn new() -> Self {
1951-
IdMap { map: DEFAULT_ID_MAP.get_or_init(init_id_map).clone(), existing_footnotes: 0 }
1951+
let mut id_map = IdMap { map: FxHashMap::default(), existing_footnotes: 0 };
1952+
id_map.init_map();
1953+
id_map
1954+
}
1955+
1956+
#[allow(rustc::potential_query_instability)]
1957+
fn init_map(&mut self) {
1958+
for key in DEFAULT_ID_MAP.get_or_init(init_id_map).iter() {
1959+
self.map.insert(key.clone(), 1);
1960+
}
19521961
}
19531962

19541963
pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
@@ -1973,4 +1982,10 @@ impl IdMap {
19731982
closure(self, &mut existing_footnotes);
19741983
self.existing_footnotes = existing_footnotes;
19751984
}
1985+
1986+
pub(crate) fn clear(&mut self) {
1987+
self.map.clear();
1988+
self.init_map();
1989+
self.existing_footnotes = 0;
1990+
}
19761991
}
There was a problem loading the remainder of the diff.

0 commit comments

Comments
 (0)
Failed to load comments.