Skip to content

Commit dddc2fd

Browse files
committed
rustdoc: reduce the number of intermediate Strings allocated
1 parent 513cf86 commit dddc2fd

File tree

2 files changed

+22
-13
lines changed

2 files changed

+22
-13
lines changed

Diff for: src/librustdoc/html/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ impl Buffer {
152152
}
153153
}
154154

155-
fn comma_sep<T: fmt::Display>(
155+
pub(crate) fn comma_sep<T: fmt::Display>(
156156
items: impl Iterator<Item = T>,
157157
space_after_comma: bool,
158158
) -> impl fmt::Display {

Diff for: src/librustdoc/html/render/write_shared.rs

+21-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::ffi::OsStr;
2+
use std::fmt;
23
use std::fs::{self, File};
34
use std::io::prelude::*;
45
use std::io::{self, BufReader};
@@ -563,7 +564,7 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
563564
}
564565

565566
impl Implementor {
566-
fn to_js_string(&self) -> String {
567+
fn to_js_string(&self) -> impl fmt::Display + '_ {
567568
fn single_quote_string(s: &str) -> String {
568569
let mut result = String::with_capacity(s.len() + 2);
569570
result.push_str("'");
@@ -577,16 +578,21 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
577578
result.push_str("'");
578579
result
579580
}
580-
let text_esc = single_quote_string(&self.text);
581-
if self.synthetic {
582-
let types = self.types.iter().map(|type_| single_quote_string(type_)).join(",");
583-
// use `1` to represent a synthetic, because it's fewer bytes than `true`
584-
format!("[{text_esc},1,[{types}]]")
585-
} else {
586-
// The types list is only used for synthetic impls.
587-
// If this changes, `main.js` and `write_shared.rs` both need changed.
588-
format!("[{text_esc}]")
589-
}
581+
crate::html::format::display_fn(|f| {
582+
let text_esc = single_quote_string(&self.text);
583+
if self.synthetic {
584+
let types = crate::html::format::comma_sep(
585+
self.types.iter().map(|type_| single_quote_string(type_)),
586+
false,
587+
);
588+
// use `1` to represent a synthetic, because it's fewer bytes than `true`
589+
write!(f, "[{text_esc},1,[{types}]]")
590+
} else {
591+
// The types list is only used for synthetic impls.
592+
// If this changes, `main.js` and `write_shared.rs` both need changed.
593+
write!(f, "[{text_esc}]")
594+
}
595+
})
590596
}
591597
}
592598

@@ -622,7 +628,10 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
622628
let implementors = format!(
623629
r#""{}":[{}]"#,
624630
krate.name(cx.tcx()),
625-
implementors.iter().map(Implementor::to_js_string).join(",")
631+
crate::html::format::comma_sep(
632+
implementors.iter().map(Implementor::to_js_string),
633+
false
634+
)
626635
);
627636

628637
let mut mydst = dst.clone();

0 commit comments

Comments
 (0)