From 34e2d3bab8905df4ad154f8fa8cdb5fe2dc769da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Thu, 21 Apr 2022 00:15:04 +0200 Subject: [PATCH] rustdoc: Optimize IdMap --- Cargo.lock | 5 +- src/librustdoc/Cargo.toml | 1 + src/librustdoc/html/markdown.rs | 92 +++++++++++++++++---------------- 3 files changed, 52 insertions(+), 46 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index fbc9ca7051f6d..f4743d9179ec3 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2491,9 +2491,9 @@ dependencies = [ [[package]] name = "once_cell" -version = "1.7.2" +version = "1.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3" +checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9" [[package]] name = "opaque-debug" @@ -4496,6 +4496,7 @@ dependencies = [ "expect-test", "itertools", "minifier", + "once_cell", "pulldown-cmark", "rayon", "regex", diff --git a/src/librustdoc/Cargo.toml b/src/librustdoc/Cargo.toml index cc5c583bea8e2..21efd040663b8 100644 --- a/src/librustdoc/Cargo.toml +++ b/src/librustdoc/Cargo.toml @@ -22,6 +22,7 @@ regex = "1" rustdoc-json-types = { path = "../rustdoc-json-types" } tracing = "0.1" tracing-tree = "0.2.0" +once_cell = "1.10.0" [dependencies.tracing-subscriber] version = "0.3.3" diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index eafe6f17d44bf..bbc188ac8dbec 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -32,6 +32,7 @@ use rustc_middle::ty::TyCtxt; use rustc_span::edition::Edition; use rustc_span::Span; +use once_cell::sync::Lazy; use std::borrow::Cow; use std::cell::RefCell; use std::collections::VecDeque; @@ -1417,62 +1418,65 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec, + map: FxHashMap, usize>, } -fn init_id_map() -> FxHashMap { +// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly. +static DEFAULT_ID_MAP: Lazy, usize>> = Lazy::new(|| init_id_map()); + +fn init_id_map() -> FxHashMap, usize> { let mut map = FxHashMap::default(); // This is the list of IDs used in Javascript. - map.insert("help".to_owned(), 1); + map.insert("help".into(), 1); // This is the list of IDs used in HTML generated in Rust (including the ones // used in tera template files). - map.insert("mainThemeStyle".to_owned(), 1); - map.insert("themeStyle".to_owned(), 1); - map.insert("theme-picker".to_owned(), 1); - map.insert("theme-choices".to_owned(), 1); - map.insert("settings-menu".to_owned(), 1); - map.insert("help-button".to_owned(), 1); - map.insert("main-content".to_owned(), 1); - map.insert("search".to_owned(), 1); - map.insert("crate-search".to_owned(), 1); - map.insert("render-detail".to_owned(), 1); - map.insert("toggle-all-docs".to_owned(), 1); - map.insert("all-types".to_owned(), 1); - map.insert("default-settings".to_owned(), 1); - map.insert("rustdoc-vars".to_owned(), 1); - map.insert("sidebar-vars".to_owned(), 1); - map.insert("copy-path".to_owned(), 1); - map.insert("TOC".to_owned(), 1); + map.insert("mainThemeStyle".into(), 1); + map.insert("themeStyle".into(), 1); + map.insert("theme-picker".into(), 1); + map.insert("theme-choices".into(), 1); + map.insert("settings-menu".into(), 1); + map.insert("help-button".into(), 1); + map.insert("main-content".into(), 1); + map.insert("search".into(), 1); + map.insert("crate-search".into(), 1); + map.insert("render-detail".into(), 1); + map.insert("toggle-all-docs".into(), 1); + map.insert("all-types".into(), 1); + map.insert("default-settings".into(), 1); + map.insert("rustdoc-vars".into(), 1); + map.insert("sidebar-vars".into(), 1); + map.insert("copy-path".into(), 1); + map.insert("TOC".into(), 1); // This is the list of IDs used by rustdoc sections (but still generated by // rustdoc). - map.insert("fields".to_owned(), 1); - map.insert("variants".to_owned(), 1); - map.insert("implementors-list".to_owned(), 1); - map.insert("synthetic-implementors-list".to_owned(), 1); - map.insert("foreign-impls".to_owned(), 1); - map.insert("implementations".to_owned(), 1); - map.insert("trait-implementations".to_owned(), 1); - map.insert("synthetic-implementations".to_owned(), 1); - map.insert("blanket-implementations".to_owned(), 1); - map.insert("required-associated-types".to_owned(), 1); - map.insert("provided-associated-types".to_owned(), 1); - map.insert("provided-associated-consts".to_owned(), 1); - map.insert("required-associated-consts".to_owned(), 1); - map.insert("required-methods".to_owned(), 1); - map.insert("provided-methods".to_owned(), 1); - map.insert("implementors".to_owned(), 1); - map.insert("synthetic-implementors".to_owned(), 1); - map.insert("implementations-list".to_owned(), 1); - map.insert("trait-implementations-list".to_owned(), 1); - map.insert("synthetic-implementations-list".to_owned(), 1); - map.insert("blanket-implementations-list".to_owned(), 1); - map.insert("deref-methods".to_owned(), 1); + map.insert("fields".into(), 1); + map.insert("variants".into(), 1); + map.insert("implementors-list".into(), 1); + map.insert("synthetic-implementors-list".into(), 1); + map.insert("foreign-impls".into(), 1); + map.insert("implementations".into(), 1); + map.insert("trait-implementations".into(), 1); + map.insert("synthetic-implementations".into(), 1); + map.insert("blanket-implementations".into(), 1); + map.insert("required-associated-types".into(), 1); + map.insert("provided-associated-types".into(), 1); + map.insert("provided-associated-consts".into(), 1); + map.insert("required-associated-consts".into(), 1); + map.insert("required-methods".into(), 1); + map.insert("provided-methods".into(), 1); + map.insert("implementors".into(), 1); + map.insert("synthetic-implementors".into(), 1); + map.insert("implementations-list".into(), 1); + map.insert("trait-implementations-list".into(), 1); + map.insert("synthetic-implementations-list".into(), 1); + map.insert("blanket-implementations-list".into(), 1); + map.insert("deref-methods".into(), 1); map } impl IdMap { pub fn new() -> Self { - IdMap { map: init_id_map() } + IdMap { map: DEFAULT_ID_MAP.clone() } } crate fn derive + ToString>(&mut self, candidate: S) -> String { @@ -1485,7 +1489,7 @@ impl IdMap { } }; - self.map.insert(id.clone(), 1); + self.map.insert(id.clone().into(), 1); id } }