From 60cc7783dabeb349c7eacb100f2a454b4546bfca Mon Sep 17 00:00:00 2001 From: klensy Date: Thu, 12 Jan 2023 19:47:53 +0300 Subject: [PATCH 1/6] evade clones --- src/librustdoc/html/render/mod.rs | 4 ++-- src/librustdoc/html/render/print_item.rs | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 8bccf68029aa3..03a960da75fb8 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -2769,8 +2769,8 @@ fn collect_paths_for_type(first_ty: clean::Type, cache: &Cache) -> Vec { let mut work = VecDeque::new(); let mut process_path = |did: DefId| { - let get_extern = || cache.external_paths.get(&did).map(|s| s.0.clone()); - let fqp = cache.exact_paths.get(&did).cloned().or_else(get_extern); + let get_extern = || cache.external_paths.get(&did).map(|s| &s.0); + let fqp = cache.exact_paths.get(&did).or_else(get_extern); if let Some(path) = fqp { out.push(join_with_double_colon(&path)); diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs index c16d6477fc379..73cb135bbf8c1 100644 --- a/src/librustdoc/html/render/print_item.rs +++ b/src/librustdoc/html/render/print_item.rs @@ -1027,8 +1027,8 @@ fn item_trait(w: &mut Buffer, cx: &mut Context<'_>, it: &clean::Item, t: &clean: .chain(std::iter::once("implementors")) .collect(); if let Some(did) = it.item_id.as_def_id() && - let get_extern = { || cache.external_paths.get(&did).map(|s| s.0.clone()) } && - let Some(fqp) = cache.exact_paths.get(&did).cloned().or_else(get_extern) { + let get_extern = { || cache.external_paths.get(&did).map(|s| &s.0) } && + let Some(fqp) = cache.exact_paths.get(&did).or_else(get_extern) { js_src_path.extend(fqp[..fqp.len() - 1].iter().copied()); js_src_path.push_fmt(format_args!("{}.{}.js", it.type_(), fqp.last().unwrap())); } else { From 4e1258c6b6f67df77157c7aea0f800854da99294 Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 13 Jan 2023 15:39:16 +0300 Subject: [PATCH 2/6] IndexItem.name String -> Symbol --- src/librustdoc/formats/cache.rs | 9 +++++---- src/librustdoc/html/render/mod.rs | 2 +- src/librustdoc/html/render/search_index.rs | 8 ++++---- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs index d027fb6e8763c..3149c22b8e55f 100644 --- a/src/librustdoc/formats/cache.rs +++ b/src/librustdoc/formats/cache.rs @@ -242,7 +242,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { } // Index this method for searching later on. - if let Some(ref s) = item.name.or_else(|| { + if let Some(s) = item.name.or_else(|| { if item.is_stripped() { None } else if let clean::ImportItem(ref i) = *item.kind && @@ -317,14 +317,15 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> { short_markdown_summary(x.as_str(), &item.link_names(self.cache)) }); let ty = item.type_(); - let name = s.to_string(); - if ty != ItemType::StructField || u16::from_str_radix(&name, 10).is_err() { + if ty != ItemType::StructField + || u16::from_str_radix(s.as_str(), 10).is_err() + { // In case this is a field from a tuple struct, we don't add it into // the search index because its name is something like "0", which is // not useful for rustdoc search. self.cache.search_index.push(IndexItem { ty, - name, + name: s, path: join_with_double_colon(path), desc, parent, diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs index 03a960da75fb8..8d018d45c106e 100644 --- a/src/librustdoc/html/render/mod.rs +++ b/src/librustdoc/html/render/mod.rs @@ -100,7 +100,7 @@ pub(crate) fn ensure_trailing_slash(v: &str) -> impl fmt::Display + '_ { #[derive(Debug)] pub(crate) struct IndexItem { pub(crate) ty: ItemType, - pub(crate) name: String, + pub(crate) name: Symbol, pub(crate) path: String, pub(crate) desc: String, pub(crate) parent: Option, diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index bc74d9cf96974..b0ae0dae0d6d5 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -35,7 +35,7 @@ pub(crate) fn build_index<'tcx>( .map_or_else(String::new, |s| short_markdown_summary(&s, &item.link_names(cache))); cache.search_index.push(IndexItem { ty: item.type_(), - name: item.name.unwrap().to_string(), + name: item.name.unwrap(), path: join_with_double_colon(&fqp[..fqp.len() - 1]), desc, parent: Some(parent), @@ -58,8 +58,8 @@ pub(crate) fn build_index<'tcx>( // Sort search index items. This improves the compressibility of the search index. cache.search_index.sort_unstable_by(|k1, k2| { // `sort_unstable_by_key` produces lifetime errors - let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent); - let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent); + let k1 = (&k1.path, k1.name.as_str(), &k1.ty, &k1.parent); + let k2 = (&k2.path, k2.name.as_str(), &k2.ty, &k2.parent); std::cmp::Ord::cmp(&k1, &k2) }); @@ -240,7 +240,7 @@ pub(crate) fn build_index<'tcx>( )?; crate_data.serialize_field( "n", - &self.items.iter().map(|item| &item.name).collect::>(), + &self.items.iter().map(|item| item.name.as_str()).collect::>(), )?; crate_data.serialize_field( "q", From cda6efeeb8d6e6155b776c7fedd32cfeeb37233e Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 13 Jan 2023 17:33:07 +0300 Subject: [PATCH 3/6] generate_macro_def_id_path: don't eagerly stringify Symbols --- src/librustdoc/html/format.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 5ad24bf268133..83fde5fd7205d 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -569,7 +569,7 @@ fn generate_macro_def_id_path( root_path: Option<&str>, ) -> Result<(String, ItemType, Vec), HrefError> { let tcx = cx.shared.tcx; - let crate_name = tcx.crate_name(def_id.krate).to_string(); + let crate_name = tcx.crate_name(def_id.krate); let cache = cx.cache(); let fqp: Vec = tcx @@ -584,7 +584,7 @@ fn generate_macro_def_id_path( } }) .collect(); - let mut relative = fqp.iter().map(|elem| elem.to_string()); + let mut relative = fqp.iter().copied(); let cstore = CStore::from_tcx(tcx); // We need this to prevent a `panic` when this function is used from intra doc links... if !cstore.has_crate_data(def_id.krate) { @@ -602,9 +602,9 @@ fn generate_macro_def_id_path( }; let mut path = if is_macro_2 { - once(crate_name.clone()).chain(relative).collect() + once(crate_name).chain(relative).collect() } else { - vec![crate_name.clone(), relative.next_back().unwrap()] + vec![crate_name, relative.next_back().unwrap()] }; if path.len() < 2 { // The minimum we can have is the crate name followed by the macro name. If shorter, then @@ -614,17 +614,22 @@ fn generate_macro_def_id_path( } if let Some(last) = path.last_mut() { - *last = format!("macro.{}.html", last); + *last = Symbol::intern(&format!("macro.{}.html", last.as_str())); } let url = match cache.extern_locations[&def_id.krate] { ExternalLocation::Remote(ref s) => { // `ExternalLocation::Remote` always end with a `/`. - format!("{}{}", s, path.join("/")) + format!("{}{}", s, path.iter().map(|p| p.as_str()).join("/")) } ExternalLocation::Local => { // `root_path` always end with a `/`. - format!("{}{}/{}", root_path.unwrap_or(""), crate_name, path.join("/")) + format!( + "{}{}/{}", + root_path.unwrap_or(""), + crate_name, + path.iter().map(|p| p.as_str()).join("/") + ) } ExternalLocation::Unknown => { debug!("crate {} not in cache when linkifying macros", crate_name); From 81b637b6f2b410e81fb47919c8b3817979ec7073 Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 13 Jan 2023 17:54:16 +0300 Subject: [PATCH 4/6] fmt_type: don't alloc const String, use &str instead --- src/librustdoc/html/format.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index 83fde5fd7205d..006076baf7257 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -1055,7 +1055,7 @@ fn fmt_type<'cx>( _ => String::new(), }; let m = mutability.print_with_space(); - let amp = if f.alternate() { "&".to_string() } else { "&".to_string() }; + let amp = if f.alternate() { "&" } else { "&" }; match **ty { clean::DynTrait(ref bounds, ref trait_lt) if bounds.len() > 1 || trait_lt.is_some() => From ffb2e843196060820130134a0815eccf198b487f Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 13 Jan 2023 18:05:16 +0300 Subject: [PATCH 5/6] Playground.crate_name String -> Symbol --- src/librustdoc/html/markdown.rs | 6 +++--- src/librustdoc/html/render/context.rs | 5 ++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index aeaee524fd453..4ff67fe1551dd 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -30,7 +30,7 @@ use rustc_hir::def_id::DefId; use rustc_hir::HirId; use rustc_middle::ty::TyCtxt; use rustc_span::edition::Edition; -use rustc_span::Span; +use rustc_span::{Span, Symbol}; use once_cell::sync::Lazy; use std::borrow::Cow; @@ -198,7 +198,7 @@ fn slugify(c: char) -> Option { #[derive(Clone, Debug)] pub struct Playground { - pub crate_name: Option, + pub crate_name: Option, pub url: String, } @@ -290,7 +290,7 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'_, 'a, I> { .map(|l| map_line(l).for_code()) .intersperse("\n".into()) .collect::(); - let krate = krate.as_ref().map(|s| &**s); + let krate = krate.as_ref().map(|s| s.as_str()); let (test, _, _) = doctest::make_test(&test, krate, false, &Default::default(), edition, None); let channel = if test.contains("#![feature(") { "&version=nightly" } else { "" }; diff --git a/src/librustdoc/html/render/context.rs b/src/librustdoc/html/render/context.rs index c8899ee62b5f9..9f266568769c0 100644 --- a/src/librustdoc/html/render/context.rs +++ b/src/librustdoc/html/render/context.rs @@ -464,8 +464,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { // If user passed in `--playground-url` arg, we fill in crate name here let mut playground = None; if let Some(url) = playground_url { - playground = - Some(markdown::Playground { crate_name: Some(krate.name(tcx).to_string()), url }); + playground = Some(markdown::Playground { crate_name: Some(krate.name(tcx)), url }); } let mut layout = layout::Layout { logo: String::new(), @@ -491,7 +490,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> { } (sym::html_playground_url, Some(s)) => { playground = Some(markdown::Playground { - crate_name: Some(krate.name(tcx).to_string()), + crate_name: Some(krate.name(tcx)), url: s.to_string(), }); } From 8998bd35ed2ebd5889f7726e971e95e642c3fdca Mon Sep 17 00:00:00 2001 From: klensy Date: Fri, 13 Jan 2023 18:27:30 +0300 Subject: [PATCH 6/6] CrateData: don't allocate String when Serialize, &str is enough --- src/librustdoc/html/render/search_index.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs index b0ae0dae0d6d5..c64349f413cee 100644 --- a/src/librustdoc/html/render/search_index.rs +++ b/src/librustdoc/html/render/search_index.rs @@ -299,7 +299,7 @@ pub(crate) fn build_index<'tcx>( )?; crate_data.serialize_field( "p", - &self.paths.iter().map(|(it, s)| (it, s.to_string())).collect::>(), + &self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::>(), )?; if has_aliases { crate_data.serialize_field("a", &self.aliases)?;