diff options
| author | bors <bors@rust-lang.org> | 2022-04-21 18:31:57 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2022-04-21 18:31:57 +0000 |
| commit | de1bc0008be096cf7ed67b93402250d3b3e480d0 (patch) | |
| tree | b5a4c4d3efe54f39a955e9898b12f021fa32d1d2 | |
| parent | b04c5329e1e145fb2fb46c5a7e775638712b03aa (diff) | |
| parent | 34e2d3bab8905df4ad154f8fa8cdb5fe2dc769da (diff) | |
| download | rust-de1bc0008be096cf7ed67b93402250d3b3e480d0.tar.gz rust-de1bc0008be096cf7ed67b93402250d3b3e480d0.zip | |
Auto merge of #96260 - Kobzol:rustdoc-idmap, r=petrochenkov
rustdoc: Optimize IdMap Slightly optimizes `IdMap`, which is hot in `markdown_links` (context [here](https://github.com/rust-lang/rust/pull/96135#issuecomment-1103539052)). There are more improvements that can be made near this place, but this seemed like an easy win locally (although I tried it on top of https://github.com/rust-lang/rust/pull/94857, so let's see what happens without that PR). r? `@petrochenkov`
| -rw-r--r-- | Cargo.lock | 5 | ||||
| -rw-r--r-- | src/librustdoc/Cargo.toml | 1 | ||||
| -rw-r--r-- | src/librustdoc/html/markdown.rs | 92 |
3 files changed, 52 insertions, 46 deletions
diff --git a/Cargo.lock b/Cargo.lock index 687fbb24d9f..30175ae3561 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2531,9 +2531,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" @@ -4536,6 +4536,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 cc5c583bea8..21efd040663 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 b0f7836a834..9e76af98298 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; @@ -1429,62 +1430,65 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB #[derive(Clone, Default, Debug)] pub struct IdMap { - map: FxHashMap<String, usize>, + map: FxHashMap<Cow<'static, str>, usize>, } -fn init_id_map() -> FxHashMap<String, usize> { +// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly. +static DEFAULT_ID_MAP: Lazy<FxHashMap<Cow<'static, str>, usize>> = Lazy::new(|| init_id_map()); + +fn init_id_map() -> FxHashMap<Cow<'static, str>, 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<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String { @@ -1497,7 +1501,7 @@ impl IdMap { } }; - self.map.insert(id.clone(), 1); + self.map.insert(id.clone().into(), 1); id } } |
