From fc31fce670ad76db14044a519a15870347253766 Mon Sep 17 00:00:00 2001 From: Michael Howell Date: Fri, 5 Aug 2022 16:36:47 -0700 Subject: [PATCH] rustdoc: use serde, which can escape strings more quickly This means we don't gain as much as we did from using single-quotes, since serde_json can only produce double-quoted strings, but it's still a win. --- src/librustdoc/html/render/write_shared.rs | 50 +++++++--------------- 1 file changed, 15 insertions(+), 35 deletions(-) diff --git a/src/librustdoc/html/render/write_shared.rs b/src/librustdoc/html/render/write_shared.rs index 0b0f5056019bd..f9abb46207d74 100644 --- a/src/librustdoc/html/render/write_shared.rs +++ b/src/librustdoc/html/render/write_shared.rs @@ -1,5 +1,4 @@ use std::ffi::OsStr; -use std::fmt; use std::fs::{self, File}; use std::io::prelude::*; use std::io::{self, BufReader}; @@ -10,6 +9,8 @@ use std::sync::LazyLock as Lazy; use itertools::Itertools; use rustc_data_structures::flock; use rustc_data_structures::fx::{FxHashMap, FxHashSet}; +use serde::ser::SerializeSeq; +use serde::{Serialize, Serializer}; use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS}; use crate::clean::Crate; @@ -563,36 +564,18 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex}; types: Vec, } - impl Implementor { - fn to_js_string(&self) -> impl fmt::Display + '_ { - fn single_quote_string(s: &str) -> String { - let mut result = String::with_capacity(s.len() + 2); - result.push_str("'"); - for c in s.chars() { - if c == '"' { - result.push_str("\""); - } else { - result.extend(c.escape_default()); - } - } - result.push_str("'"); - result + impl Serialize for Implementor { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + let mut seq = serializer.serialize_seq(None)?; + seq.serialize_element(&self.text)?; + if self.synthetic { + seq.serialize_element(&1)?; + seq.serialize_element(&self.types)?; } - crate::html::format::display_fn(|f| { - let text_esc = single_quote_string(&self.text); - if self.synthetic { - let types = crate::html::format::comma_sep( - self.types.iter().map(|type_| single_quote_string(type_)), - false, - ); - // use `1` to represent a synthetic, because it's fewer bytes than `true` - 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}]") - } - }) + seq.end() } } @@ -626,12 +609,9 @@ if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex}; } let implementors = format!( - r#""{}":[{}]"#, + r#""{}":{}"#, krate.name(cx.tcx()), - crate::html::format::comma_sep( - implementors.iter().map(Implementor::to_js_string), - false - ) + serde_json::to_string(&implementors).expect("failed serde conversion"), ); let mut mydst = dst.clone();