Skip to content

Commit 38c41d0

Browse files
committed
Auto merge of #142335 - nnethercote:rustdoc-json-allocations, r=aDotInTheVoid
rustdoc_json: reduce allocations These commits reduce the number of allocations done for rustdoc_json, mostly by avoiding unnecessary clones. Best reviewed one commit at a time. r? `@aDotInTheVoid`
2 parents 49a8ba0 + 278f4b2 commit 38c41d0

File tree

4 files changed

+159
-141
lines changed

4 files changed

+159
-141
lines changed

src/librustdoc/formats/renderer.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
5656
fn restore_module_data(&mut self, info: Self::ModuleData);
5757

5858
/// Renders a single non-module item. This means no recursive sub-item rendering is required.
59-
fn item(&mut self, item: clean::Item) -> Result<(), Error>;
59+
fn item(&mut self, item: &clean::Item) -> Result<(), Error>;
6060

6161
/// Renders a module (should not handle recursing into children).
6262
fn mod_item_in(&mut self, item: &clean::Item) -> Result<(), Error>;
@@ -67,14 +67,14 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
6767
}
6868

6969
/// Post processing hook for cleanup and dumping output to files.
70-
fn after_krate(&mut self) -> Result<(), Error>;
70+
fn after_krate(self) -> Result<(), Error>;
7171

7272
fn cache(&self) -> &Cache;
7373
}
7474

7575
fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
7676
cx: &mut T,
77-
item: clean::Item,
77+
item: &clean::Item,
7878
prof: &SelfProfilerRef,
7979
) -> Result<(), Error> {
8080
if item.is_mod() && T::RUN_ON_MODULE {
@@ -84,12 +84,12 @@ fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
8484
prof.generic_activity_with_arg("render_mod_item", item.name.unwrap().to_string());
8585

8686
cx.mod_item_in(&item)?;
87-
let (clean::StrippedItem(box clean::ModuleItem(module)) | clean::ModuleItem(module)) =
88-
item.inner.kind
87+
let (clean::StrippedItem(box clean::ModuleItem(ref module))
88+
| clean::ModuleItem(ref module)) = item.inner.kind
8989
else {
9090
unreachable!()
9191
};
92-
for it in module.items {
92+
for it in module.items.iter() {
9393
let info = cx.save_module_data();
9494
run_format_inner(cx, it, prof)?;
9595
cx.restore_module_data(info);
@@ -101,7 +101,7 @@ fn run_format_inner<'tcx, T: FormatRenderer<'tcx>>(
101101
} else if let Some(item_name) = item.name
102102
&& !item.is_extern_crate()
103103
{
104-
prof.generic_activity_with_arg("render_item", item_name.as_str()).run(|| cx.item(item))?;
104+
prof.generic_activity_with_arg("render_item", item_name.as_str()).run(|| cx.item(&item))?;
105105
}
106106
Ok(())
107107
}
@@ -125,7 +125,7 @@ pub(crate) fn run_format<'tcx, T: FormatRenderer<'tcx>>(
125125
}
126126

127127
// Render the crate documentation
128-
run_format_inner(&mut format_renderer, krate.module, prof)?;
128+
run_format_inner(&mut format_renderer, &krate.module, prof)?;
129129

130130
prof.verbose_generic_activity_with_arg("renderer_after_krate", T::descr())
131131
.run(|| format_renderer.after_krate())

src/librustdoc/html/render/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
609609
self.info = info;
610610
}
611611

612-
fn after_krate(&mut self) -> Result<(), Error> {
612+
fn after_krate(mut self) -> Result<(), Error> {
613613
let crate_name = self.tcx().crate_name(LOCAL_CRATE);
614614
let final_file = self.dst.join(crate_name.as_str()).join("all.html");
615615
let settings_file = self.dst.join("settings.html");
@@ -830,7 +830,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
830830
Ok(())
831831
}
832832

833-
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
833+
fn item(&mut self, item: &clean::Item) -> Result<(), Error> {
834834
// Stripped modules survive the rustdoc passes (i.e., `strip-private`)
835835
// if they contain impls for public types. These modules can also
836836
// contain items such as publicly re-exported structures.

0 commit comments

Comments
 (0)