@@ -32,6 +32,7 @@ use std::iter::Peekable;
32
32
use std:: ops:: { ControlFlow , Range } ;
33
33
use std:: path:: PathBuf ;
34
34
use std:: str:: { self , CharIndices } ;
35
+ use std:: sync:: OnceLock ;
35
36
36
37
use pulldown_cmark:: {
37
38
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
1885
1886
#[ derive( Clone , Default , Debug ) ]
1886
1887
pub struct IdMap {
1887
1888
map : FxHashMap < Cow < ' static , str > , usize > ,
1888
- defined_ids : FxHashSet < & ' static str > ,
1889
1889
existing_footnotes : usize ,
1890
1890
}
1891
1891
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
+
1892
1896
fn init_id_map ( ) -> FxHashSet < & ' static str > {
1893
1897
let mut map = FxHashSet :: default ( ) ;
1894
1898
// This is the list of IDs used in JavaScript.
@@ -1945,14 +1949,14 @@ fn init_id_map() -> FxHashSet<&'static str> {
1945
1949
1946
1950
impl IdMap {
1947
1951
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 }
1949
1953
}
1950
1954
1951
1955
pub ( crate ) fn derive < S : AsRef < str > + ToString > ( & mut self , candidate : S ) -> String {
1952
1956
let id = match self . map . get_mut ( candidate. as_ref ( ) ) {
1953
1957
None => {
1954
1958
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 ( ) ) {
1956
1960
let id = format ! ( "{}-{}" , candidate, 1 ) ;
1957
1961
self . map . insert ( candidate. into ( ) , 2 ) ;
1958
1962
id
0 commit comments