Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d69daf1

Browse files
Store default ID map in a static
1 parent 91a72c3 commit d69daf1

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,
@@ -1885,10 +1886,13 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
18851886
#[derive(Clone, Default, Debug)]
18861887
pub struct IdMap {
18871888
map: FxHashMap<Cow<'static, str>, usize>,
1888-
defined_ids: FxHashSet<&'static str>,
18891889
existing_footnotes: usize,
18901890
}
18911891

1892+
// The map is pre-initialized and then can be used as is to prevent cloning it for each item
1893+
// (in the sidebar rendering).
1894+
static DEFAULT_ID_MAP: OnceLock<FxHashSet<&'static str>> = OnceLock::new();
1895+
18921896
fn init_id_map() -> FxHashSet<&'static str> {
18931897
let mut map = FxHashSet::default();
18941898
// This is the list of IDs used in JavaScript.
@@ -1945,14 +1949,14 @@ fn init_id_map() -> FxHashSet<&'static str> {
19451949

19461950
impl IdMap {
19471951
pub fn new() -> Self {
1948-
IdMap { map: FxHashMap::default(), defined_ids: init_id_map(), existing_footnotes: 0 }
1952+
IdMap { map: FxHashMap::default(), existing_footnotes: 0 }
19491953
}
19501954

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

0 commit comments

Comments
 (0)