@@ -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,
@@ -1882,10 +1883,13 @@ pub(crate) fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<Rust
1882
1883
#[ derive( Clone , Default , Debug ) ]
1883
1884
pub struct IdMap {
1884
1885
map : FxHashMap < Cow < ' static , str > , usize > ,
1885
- defined_ids : FxHashSet < & ' static str > ,
1886
1886
existing_footnotes : usize ,
1887
1887
}
1888
1888
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
+
1889
1893
fn init_id_map ( ) -> FxHashSet < & ' static str > {
1890
1894
let mut map = FxHashSet :: default ( ) ;
1891
1895
// This is the list of IDs used in JavaScript.
@@ -1942,14 +1946,14 @@ fn init_id_map() -> FxHashSet<&'static str> {
1942
1946
1943
1947
impl IdMap {
1944
1948
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 }
1946
1950
}
1947
1951
1948
1952
pub ( crate ) fn derive < S : AsRef < str > + ToString > ( & mut self , candidate : S ) -> String {
1949
1953
let id = match self . map . get_mut ( candidate. as_ref ( ) ) {
1950
1954
None => {
1951
1955
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 ( ) ) {
1953
1957
let id = format ! ( "{}-{}" , candidate, 1 ) ;
1954
1958
self . map . insert ( candidate. into ( ) , 2 ) ;
1955
1959
id
0 commit comments