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 79b3c08

Browse files
committedJun 11, 2025
Avoid more clones in rustdoc JSON output.
By making `JsonRenderer::item` take `&clean::Item` instead of a `clean::Item`. This required also changing `FromClean` and `IntoJson` methods to take references, which required a lot of follow-on sigil wrangling that is mostly tedious.
1 parent 195c985 commit 79b3c08

File tree

4 files changed

+133
-125
lines changed

4 files changed

+133
-125
lines changed
 

‎src/librustdoc/formats/renderer.rs‎

Lines changed: 7 additions & 7 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>;
@@ -74,7 +74,7 @@ pub(crate) trait FormatRenderer<'tcx>: Sized {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -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.

‎src/librustdoc/json/conversions.rs‎

Lines changed: 120 additions & 112 deletions
Large diffs are not rendered by default.

‎src/librustdoc/json/mod.rs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'tcx> JsonRenderer<'tcx> {
6464
.iter()
6565
.map(|i| {
6666
let item = &i.impl_item;
67-
self.item(item.clone()).unwrap();
67+
self.item(item).unwrap();
6868
self.id_from_item(item)
6969
})
7070
.collect()
@@ -95,7 +95,7 @@ impl<'tcx> JsonRenderer<'tcx> {
9595
}
9696

9797
if item.item_id.is_local() || is_primitive_impl {
98-
self.item(item.clone()).unwrap();
98+
self.item(item).unwrap();
9999
Some(self.id_from_item(item))
100100
} else {
101101
None
@@ -216,19 +216,19 @@ impl<'tcx> FormatRenderer<'tcx> for JsonRenderer<'tcx> {
216216
/// Inserts an item into the index. This should be used rather than directly calling insert on
217217
/// the hashmap because certain items (traits and types) need to have their mappings for trait
218218
/// implementations filled out before they're inserted.
219-
fn item(&mut self, item: clean::Item) -> Result<(), Error> {
219+
fn item(&mut self, item: &clean::Item) -> Result<(), Error> {
220220
let item_type = item.type_();
221221
let item_name = item.name;
222222
trace!("rendering {item_type} {item_name:?}");
223223

224224
// Flatten items that recursively store other items. We include orphaned items from
225225
// stripped modules and etc that are otherwise reachable.
226226
if let ItemKind::StrippedItem(inner) = &item.kind {
227-
inner.inner_items().for_each(|i| self.item(i.clone()).unwrap());
227+
inner.inner_items().for_each(|i| self.item(i).unwrap());
228228
}
229229

230230
// Flatten items that recursively store other items
231-
item.kind.inner_items().for_each(|i| self.item(i.clone()).unwrap());
231+
item.kind.inner_items().for_each(|i| self.item(i).unwrap());
232232

233233
let item_id = item.item_id;
234234
if let Some(mut new_item) = self.convert_item(item) {

0 commit comments

Comments
 (0)
Please sign in to comment.