Skip to content

Commit de1bc00

Browse files
committed
Auto merge of #96260 - Kobzol:rustdoc-idmap, r=petrochenkov
rustdoc: Optimize IdMap Slightly optimizes `IdMap`, which is hot in `markdown_links` (context [here](#96135 (comment))). 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 #94857, so let's see what happens without that PR). r? `@petrochenkov`
2 parents b04c532 + 34e2d3b commit de1bc00

File tree

3 files changed

+52
-46
lines changed

3 files changed

+52
-46
lines changed

Cargo.lock

+3-2
Original file line numberDiff line numberDiff line change
@@ -2531,9 +2531,9 @@ dependencies = [
25312531

25322532
[[package]]
25332533
name = "once_cell"
2534-
version = "1.7.2"
2534+
version = "1.10.0"
25352535
source = "registry+https://github.com/rust-lang/crates.io-index"
2536-
checksum = "af8b08b04175473088b46763e51ee54da5f9a164bc162f615b91bc179dbf15a3"
2536+
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
25372537

25382538
[[package]]
25392539
name = "opaque-debug"
@@ -4536,6 +4536,7 @@ dependencies = [
45364536
"expect-test",
45374537
"itertools",
45384538
"minifier",
4539+
"once_cell",
45394540
"pulldown-cmark",
45404541
"rayon",
45414542
"regex",

src/librustdoc/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ regex = "1"
2222
rustdoc-json-types = { path = "../rustdoc-json-types" }
2323
tracing = "0.1"
2424
tracing-tree = "0.2.0"
25+
once_cell = "1.10.0"
2526

2627
[dependencies.tracing-subscriber]
2728
version = "0.3.3"

src/librustdoc/html/markdown.rs

+48-44
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use rustc_middle::ty::TyCtxt;
3232
use rustc_span::edition::Edition;
3333
use rustc_span::Span;
3434

35+
use once_cell::sync::Lazy;
3536
use std::borrow::Cow;
3637
use std::cell::RefCell;
3738
use std::collections::VecDeque;
@@ -1429,62 +1430,65 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
14291430

14301431
#[derive(Clone, Default, Debug)]
14311432
pub struct IdMap {
1432-
map: FxHashMap<String, usize>,
1433+
map: FxHashMap<Cow<'static, str>, usize>,
14331434
}
14341435

1435-
fn init_id_map() -> FxHashMap<String, usize> {
1436+
// The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
1437+
static DEFAULT_ID_MAP: Lazy<FxHashMap<Cow<'static, str>, usize>> = Lazy::new(|| init_id_map());
1438+
1439+
fn init_id_map() -> FxHashMap<Cow<'static, str>, usize> {
14361440
let mut map = FxHashMap::default();
14371441
// This is the list of IDs used in Javascript.
1438-
map.insert("help".to_owned(), 1);
1442+
map.insert("help".into(), 1);
14391443
// This is the list of IDs used in HTML generated in Rust (including the ones
14401444
// used in tera template files).
1441-
map.insert("mainThemeStyle".to_owned(), 1);
1442-
map.insert("themeStyle".to_owned(), 1);
1443-
map.insert("theme-picker".to_owned(), 1);
1444-
map.insert("theme-choices".to_owned(), 1);
1445-
map.insert("settings-menu".to_owned(), 1);
1446-
map.insert("help-button".to_owned(), 1);
1447-
map.insert("main-content".to_owned(), 1);
1448-
map.insert("search".to_owned(), 1);
1449-
map.insert("crate-search".to_owned(), 1);
1450-
map.insert("render-detail".to_owned(), 1);
1451-
map.insert("toggle-all-docs".to_owned(), 1);
1452-
map.insert("all-types".to_owned(), 1);
1453-
map.insert("default-settings".to_owned(), 1);
1454-
map.insert("rustdoc-vars".to_owned(), 1);
1455-
map.insert("sidebar-vars".to_owned(), 1);
1456-
map.insert("copy-path".to_owned(), 1);
1457-
map.insert("TOC".to_owned(), 1);
1445+
map.insert("mainThemeStyle".into(), 1);
1446+
map.insert("themeStyle".into(), 1);
1447+
map.insert("theme-picker".into(), 1);
1448+
map.insert("theme-choices".into(), 1);
1449+
map.insert("settings-menu".into(), 1);
1450+
map.insert("help-button".into(), 1);
1451+
map.insert("main-content".into(), 1);
1452+
map.insert("search".into(), 1);
1453+
map.insert("crate-search".into(), 1);
1454+
map.insert("render-detail".into(), 1);
1455+
map.insert("toggle-all-docs".into(), 1);
1456+
map.insert("all-types".into(), 1);
1457+
map.insert("default-settings".into(), 1);
1458+
map.insert("rustdoc-vars".into(), 1);
1459+
map.insert("sidebar-vars".into(), 1);
1460+
map.insert("copy-path".into(), 1);
1461+
map.insert("TOC".into(), 1);
14581462
// This is the list of IDs used by rustdoc sections (but still generated by
14591463
// rustdoc).
1460-
map.insert("fields".to_owned(), 1);
1461-
map.insert("variants".to_owned(), 1);
1462-
map.insert("implementors-list".to_owned(), 1);
1463-
map.insert("synthetic-implementors-list".to_owned(), 1);
1464-
map.insert("foreign-impls".to_owned(), 1);
1465-
map.insert("implementations".to_owned(), 1);
1466-
map.insert("trait-implementations".to_owned(), 1);
1467-
map.insert("synthetic-implementations".to_owned(), 1);
1468-
map.insert("blanket-implementations".to_owned(), 1);
1469-
map.insert("required-associated-types".to_owned(), 1);
1470-
map.insert("provided-associated-types".to_owned(), 1);
1471-
map.insert("provided-associated-consts".to_owned(), 1);
1472-
map.insert("required-associated-consts".to_owned(), 1);
1473-
map.insert("required-methods".to_owned(), 1);
1474-
map.insert("provided-methods".to_owned(), 1);
1475-
map.insert("implementors".to_owned(), 1);
1476-
map.insert("synthetic-implementors".to_owned(), 1);
1477-
map.insert("implementations-list".to_owned(), 1);
1478-
map.insert("trait-implementations-list".to_owned(), 1);
1479-
map.insert("synthetic-implementations-list".to_owned(), 1);
1480-
map.insert("blanket-implementations-list".to_owned(), 1);
1481-
map.insert("deref-methods".to_owned(), 1);
1464+
map.insert("fields".into(), 1);
1465+
map.insert("variants".into(), 1);
1466+
map.insert("implementors-list".into(), 1);
1467+
map.insert("synthetic-implementors-list".into(), 1);
1468+
map.insert("foreign-impls".into(), 1);
1469+
map.insert("implementations".into(), 1);
1470+
map.insert("trait-implementations".into(), 1);
1471+
map.insert("synthetic-implementations".into(), 1);
1472+
map.insert("blanket-implementations".into(), 1);
1473+
map.insert("required-associated-types".into(), 1);
1474+
map.insert("provided-associated-types".into(), 1);
1475+
map.insert("provided-associated-consts".into(), 1);
1476+
map.insert("required-associated-consts".into(), 1);
1477+
map.insert("required-methods".into(), 1);
1478+
map.insert("provided-methods".into(), 1);
1479+
map.insert("implementors".into(), 1);
1480+
map.insert("synthetic-implementors".into(), 1);
1481+
map.insert("implementations-list".into(), 1);
1482+
map.insert("trait-implementations-list".into(), 1);
1483+
map.insert("synthetic-implementations-list".into(), 1);
1484+
map.insert("blanket-implementations-list".into(), 1);
1485+
map.insert("deref-methods".into(), 1);
14821486
map
14831487
}
14841488

14851489
impl IdMap {
14861490
pub fn new() -> Self {
1487-
IdMap { map: init_id_map() }
1491+
IdMap { map: DEFAULT_ID_MAP.clone() }
14881492
}
14891493

14901494
crate fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
@@ -1497,7 +1501,7 @@ impl IdMap {
14971501
}
14981502
};
14991503

1500-
self.map.insert(id.clone(), 1);
1504+
self.map.insert(id.clone().into(), 1);
15011505
id
15021506
}
15031507
}

0 commit comments

Comments
 (0)