Skip to content

Commit b1e9070

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 a475551 + c4c6cb2 commit b1e9070

File tree

7 files changed

+153
-129
lines changed

7 files changed

+153
-129
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-53
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,11 @@ use std::iter::Peekable;
3232
use std::ops::{ControlFlow, Range};
3333
use std::path::PathBuf;
3434
use std::str::{self, CharIndices};
35-
use std::sync::OnceLock;
3635

3736
use pulldown_cmark::{
3837
BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
3938
};
40-
use rustc_data_structures::fx::FxHashMap;
39+
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
4140
use rustc_errors::{Diag, DiagMessage};
4241
use rustc_hir::def_id::LocalDefId;
4342
use rustc_middle::ty::TyCtxt;
@@ -1886,74 +1885,81 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
18861885
#[derive(Clone, Default, Debug)]
18871886
pub struct IdMap {
18881887
map: FxHashMap<Cow<'static, str>, usize>,
1888+
defined_ids: FxHashSet<&'static str>,
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();
1894-
1895-
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
1896-
let mut map = FxHashMap::default();
1892+
fn init_id_map() -> FxHashSet<&'static str> {
1893+
let mut map = FxHashSet::default();
18971894
// 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);
1895+
map.insert("help");
1896+
map.insert("settings");
1897+
map.insert("not-displayed");
1898+
map.insert("alternative-display");
1899+
map.insert("search");
1900+
map.insert("crate-search");
1901+
map.insert("crate-search-div");
19051902
// This is the list of IDs used in HTML generated in Rust (including the ones
19061903
// 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);
1904+
map.insert("themeStyle");
1905+
map.insert("settings-menu");
1906+
map.insert("help-button");
1907+
map.insert("sidebar-button");
1908+
map.insert("main-content");
1909+
map.insert("toggle-all-docs");
1910+
map.insert("all-types");
1911+
map.insert("default-settings");
1912+
map.insert("sidebar-vars");
1913+
map.insert("copy-path");
1914+
map.insert("rustdoc-toc");
1915+
map.insert("rustdoc-modnav");
19191916
// This is the list of IDs used by rustdoc sections (but still generated by
19201917
// 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);
1918+
map.insert("fields");
1919+
map.insert("variants");
1920+
map.insert("implementors-list");
1921+
map.insert("synthetic-implementors-list");
1922+
map.insert("foreign-impls");
1923+
map.insert("implementations");
1924+
map.insert("trait-implementations");
1925+
map.insert("synthetic-implementations");
1926+
map.insert("blanket-implementations");
1927+
map.insert("required-associated-types");
1928+
map.insert("provided-associated-types");
1929+
map.insert("provided-associated-consts");
1930+
map.insert("required-associated-consts");
1931+
map.insert("required-methods");
1932+
map.insert("provided-methods");
1933+
map.insert("dyn-compatibility");
1934+
map.insert("implementors");
1935+
map.insert("synthetic-implementors");
1936+
map.insert("implementations-list");
1937+
map.insert("trait-implementations-list");
1938+
map.insert("synthetic-implementations-list");
1939+
map.insert("blanket-implementations-list");
1940+
map.insert("deref-methods");
1941+
map.insert("layout");
1942+
map.insert("aliased-type");
19461943
map
19471944
}
19481945

19491946
impl IdMap {
19501947
pub fn new() -> Self {
1951-
IdMap { map: DEFAULT_ID_MAP.get_or_init(init_id_map).clone(), existing_footnotes: 0 }
1948+
IdMap { map: FxHashMap::default(), defined_ids: init_id_map(), existing_footnotes: 0 }
19521949
}
19531950

19541951
pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
19551952
let id = match self.map.get_mut(candidate.as_ref()) {
1956-
None => candidate.to_string(),
1953+
None => {
1954+
let candidate = candidate.to_string();
1955+
if self.defined_ids.contains(candidate.as_str()) {
1956+
let id = format!("{}-{}", candidate, 1);
1957+
self.map.insert(candidate.into(), 2);
1958+
id
1959+
} else {
1960+
candidate.to_string()
1961+
}
1962+
}
19571963
Some(a) => {
19581964
let id = format!("{}-{}", candidate.as_ref(), *a);
19591965
*a += 1;
@@ -1973,4 +1979,9 @@ impl IdMap {
19731979
closure(self, &mut existing_footnotes);
19741980
self.existing_footnotes = existing_footnotes;
19751981
}
1982+
1983+
pub(crate) fn clear(&mut self) {
1984+
self.map.clear();
1985+
self.existing_footnotes = 0;
1986+
}
19761987
}

0 commit comments

Comments
 (0)