rustdoc: reduce the number of intermediate Strings allocated

This commit is contained in:
Michael Howell 2022-08-05 08:56:09 -07:00
parent 513cf8664a
commit dddc2fd6de
2 changed files with 22 additions and 13 deletions

View File

@ -152,7 +152,7 @@ impl Buffer {
} }
} }
fn comma_sep<T: fmt::Display>( pub(crate) fn comma_sep<T: fmt::Display>(
items: impl Iterator<Item = T>, items: impl Iterator<Item = T>,
space_after_comma: bool, space_after_comma: bool,
) -> impl fmt::Display { ) -> impl fmt::Display {

View File

@ -1,4 +1,5 @@
use std::ffi::OsStr; use std::ffi::OsStr;
use std::fmt;
use std::fs::{self, File}; use std::fs::{self, File};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, BufReader}; use std::io::{self, BufReader};
@ -563,7 +564,7 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
} }
impl Implementor { impl Implementor {
fn to_js_string(&self) -> String { fn to_js_string(&self) -> impl fmt::Display + '_ {
fn single_quote_string(s: &str) -> String { fn single_quote_string(s: &str) -> String {
let mut result = String::with_capacity(s.len() + 2); let mut result = String::with_capacity(s.len() + 2);
result.push_str("'"); result.push_str("'");
@ -577,16 +578,21 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
result.push_str("'"); result.push_str("'");
result result
} }
let text_esc = single_quote_string(&self.text); crate::html::format::display_fn(|f| {
if self.synthetic { let text_esc = single_quote_string(&self.text);
let types = self.types.iter().map(|type_| single_quote_string(type_)).join(","); if self.synthetic {
// use `1` to represent a synthetic, because it's fewer bytes than `true` let types = crate::html::format::comma_sep(
format!("[{text_esc},1,[{types}]]") self.types.iter().map(|type_| single_quote_string(type_)),
} else { false,
// The types list is only used for synthetic impls. );
// If this changes, `main.js` and `write_shared.rs` both need changed. // use `1` to represent a synthetic, because it's fewer bytes than `true`
format!("[{text_esc}]") write!(f, "[{text_esc},1,[{types}]]")
} } else {
// The types list is only used for synthetic impls.
// If this changes, `main.js` and `write_shared.rs` both need changed.
write!(f, "[{text_esc}]")
}
})
} }
} }
@ -622,7 +628,10 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
let implementors = format!( let implementors = format!(
r#""{}":[{}]"#, r#""{}":[{}]"#,
krate.name(cx.tcx()), krate.name(cx.tcx()),
implementors.iter().map(Implementor::to_js_string).join(",") crate::html::format::comma_sep(
implementors.iter().map(Implementor::to_js_string),
false
)
); );
let mut mydst = dst.clone(); let mut mydst = dst.clone();