Skip to content

Commit 2e242f8

Browse files
Store default ID map in a static
1 parent 46afbc0 commit 2e242f8

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ use std::iter::Peekable;
3232
use std::ops::{ControlFlow, Range};
3333
use std::path::PathBuf;
3434
use std::str::{self, CharIndices};
35+
use std::sync::OnceLock;
3536

3637
use pulldown_cmark::{
3738
BrokenLink, CodeBlockKind, CowStr, Event, LinkType, Options, Parser, Tag, TagEnd, html,
@@ -1882,10 +1883,13 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
18821883
#[derive(Clone, Default, Debug)]
18831884
pub struct IdMap {
18841885
map: FxHashMap<Cow<'static, str>, usize>,
1885-
defined_ids: FxHashSet<&'static str>,
18861886
existing_footnotes: usize,
18871887
}
18881888

1889+
// The map is pre-initialized and then can be used as is to prevent cloning it for each item
1890+
// (in the sidebar rendering).
1891+
static DEFAULT_ID_MAP: OnceLock<FxHashSet<&'static str>> = OnceLock::new();
1892+
18891893
fn init_id_map() -> FxHashSet<&'static str> {
18901894
let mut map = FxHashSet::default();
18911895
// This is the list of IDs used in JavaScript.
@@ -1942,14 +1946,14 @@ fn init_id_map() -> FxHashSet<&'static str> {
19421946

19431947
impl IdMap {
19441948
pub fn new() -> Self {
1945-
IdMap { map: FxHashMap::default(), defined_ids: init_id_map(), existing_footnotes: 0 }
1949+
IdMap { map: FxHashMap::default(), existing_footnotes: 0 }
19461950
}
19471951

19481952
pub(crate) fn derive<S: AsRef<str> + ToString>(&mut self, candidate: S) -> String {
19491953
let id = match self.map.get_mut(candidate.as_ref()) {
19501954
None => {
19511955
let candidate = candidate.to_string();
1952-
if self.defined_ids.contains(candidate.as_str()) {
1956+
if DEFAULT_ID_MAP.get_or_init(init_id_map).contains(candidate.as_str()) {
19531957
let id = format!("{}-{}", candidate, 1);
19541958
self.map.insert(candidate.into(), 2);
19551959
id

0 commit comments

Comments
 (0)