@@ -32,6 +32,7 @@ use rustc_middle::ty::TyCtxt;
32
32
use rustc_span:: edition:: Edition ;
33
33
use rustc_span:: Span ;
34
34
35
+ use once_cell:: sync:: Lazy ;
35
36
use std:: borrow:: Cow ;
36
37
use std:: cell:: RefCell ;
37
38
use std:: collections:: VecDeque ;
@@ -1417,62 +1418,65 @@ crate fn rust_code_blocks(md: &str, extra_info: &ExtraInfo<'_>) -> Vec<RustCodeB
1417
1418
1418
1419
#[ derive( Clone , Default , Debug ) ]
1419
1420
pub struct IdMap {
1420
- map : FxHashMap < String , usize > ,
1421
+ map : FxHashMap < Cow < ' static , str > , usize > ,
1421
1422
}
1422
1423
1423
- fn init_id_map ( ) -> FxHashMap < String , usize > {
1424
+ // The map is pre-initialized and cloned each time to avoid reinitializing it repeatedly.
1425
+ static DEFAULT_ID_MAP : Lazy < FxHashMap < Cow < ' static , str > , usize > > = Lazy :: new ( || init_id_map ( ) ) ;
1426
+
1427
+ fn init_id_map ( ) -> FxHashMap < Cow < ' static , str > , usize > {
1424
1428
let mut map = FxHashMap :: default ( ) ;
1425
1429
// This is the list of IDs used in Javascript.
1426
- map. insert ( "help" . to_owned ( ) , 1 ) ;
1430
+ map. insert ( "help" . into ( ) , 1 ) ;
1427
1431
// This is the list of IDs used in HTML generated in Rust (including the ones
1428
1432
// used in tera template files).
1429
- map. insert ( "mainThemeStyle" . to_owned ( ) , 1 ) ;
1430
- map. insert ( "themeStyle" . to_owned ( ) , 1 ) ;
1431
- map. insert ( "theme-picker" . to_owned ( ) , 1 ) ;
1432
- map. insert ( "theme-choices" . to_owned ( ) , 1 ) ;
1433
- map. insert ( "settings-menu" . to_owned ( ) , 1 ) ;
1434
- map. insert ( "help-button" . to_owned ( ) , 1 ) ;
1435
- map. insert ( "main-content" . to_owned ( ) , 1 ) ;
1436
- map. insert ( "search" . to_owned ( ) , 1 ) ;
1437
- map. insert ( "crate-search" . to_owned ( ) , 1 ) ;
1438
- map. insert ( "render-detail" . to_owned ( ) , 1 ) ;
1439
- map. insert ( "toggle-all-docs" . to_owned ( ) , 1 ) ;
1440
- map. insert ( "all-types" . to_owned ( ) , 1 ) ;
1441
- map. insert ( "default-settings" . to_owned ( ) , 1 ) ;
1442
- map. insert ( "rustdoc-vars" . to_owned ( ) , 1 ) ;
1443
- map. insert ( "sidebar-vars" . to_owned ( ) , 1 ) ;
1444
- map. insert ( "copy-path" . to_owned ( ) , 1 ) ;
1445
- map. insert ( "TOC" . to_owned ( ) , 1 ) ;
1433
+ map. insert ( "mainThemeStyle" . into ( ) , 1 ) ;
1434
+ map. insert ( "themeStyle" . into ( ) , 1 ) ;
1435
+ map. insert ( "theme-picker" . into ( ) , 1 ) ;
1436
+ map. insert ( "theme-choices" . into ( ) , 1 ) ;
1437
+ map. insert ( "settings-menu" . into ( ) , 1 ) ;
1438
+ map. insert ( "help-button" . into ( ) , 1 ) ;
1439
+ map. insert ( "main-content" . into ( ) , 1 ) ;
1440
+ map. insert ( "search" . into ( ) , 1 ) ;
1441
+ map. insert ( "crate-search" . into ( ) , 1 ) ;
1442
+ map. insert ( "render-detail" . into ( ) , 1 ) ;
1443
+ map. insert ( "toggle-all-docs" . into ( ) , 1 ) ;
1444
+ map. insert ( "all-types" . into ( ) , 1 ) ;
1445
+ map. insert ( "default-settings" . into ( ) , 1 ) ;
1446
+ map. insert ( "rustdoc-vars" . into ( ) , 1 ) ;
1447
+ map. insert ( "sidebar-vars" . into ( ) , 1 ) ;
1448
+ map. insert ( "copy-path" . into ( ) , 1 ) ;
1449
+ map. insert ( "TOC" . into ( ) , 1 ) ;
1446
1450
// This is the list of IDs used by rustdoc sections (but still generated by
1447
1451
// rustdoc).
1448
- map. insert ( "fields" . to_owned ( ) , 1 ) ;
1449
- map. insert ( "variants" . to_owned ( ) , 1 ) ;
1450
- map. insert ( "implementors-list" . to_owned ( ) , 1 ) ;
1451
- map. insert ( "synthetic-implementors-list" . to_owned ( ) , 1 ) ;
1452
- map. insert ( "foreign-impls" . to_owned ( ) , 1 ) ;
1453
- map. insert ( "implementations" . to_owned ( ) , 1 ) ;
1454
- map. insert ( "trait-implementations" . to_owned ( ) , 1 ) ;
1455
- map. insert ( "synthetic-implementations" . to_owned ( ) , 1 ) ;
1456
- map. insert ( "blanket-implementations" . to_owned ( ) , 1 ) ;
1457
- map. insert ( "required-associated-types" . to_owned ( ) , 1 ) ;
1458
- map. insert ( "provided-associated-types" . to_owned ( ) , 1 ) ;
1459
- map. insert ( "provided-associated-consts" . to_owned ( ) , 1 ) ;
1460
- map. insert ( "required-associated-consts" . to_owned ( ) , 1 ) ;
1461
- map. insert ( "required-methods" . to_owned ( ) , 1 ) ;
1462
- map. insert ( "provided-methods" . to_owned ( ) , 1 ) ;
1463
- map. insert ( "implementors" . to_owned ( ) , 1 ) ;
1464
- map. insert ( "synthetic-implementors" . to_owned ( ) , 1 ) ;
1465
- map. insert ( "implementations-list" . to_owned ( ) , 1 ) ;
1466
- map. insert ( "trait-implementations-list" . to_owned ( ) , 1 ) ;
1467
- map. insert ( "synthetic-implementations-list" . to_owned ( ) , 1 ) ;
1468
- map. insert ( "blanket-implementations-list" . to_owned ( ) , 1 ) ;
1469
- map. insert ( "deref-methods" . to_owned ( ) , 1 ) ;
1452
+ map. insert ( "fields" . into ( ) , 1 ) ;
1453
+ map. insert ( "variants" . into ( ) , 1 ) ;
1454
+ map. insert ( "implementors-list" . into ( ) , 1 ) ;
1455
+ map. insert ( "synthetic-implementors-list" . into ( ) , 1 ) ;
1456
+ map. insert ( "foreign-impls" . into ( ) , 1 ) ;
1457
+ map. insert ( "implementations" . into ( ) , 1 ) ;
1458
+ map. insert ( "trait-implementations" . into ( ) , 1 ) ;
1459
+ map. insert ( "synthetic-implementations" . into ( ) , 1 ) ;
1460
+ map. insert ( "blanket-implementations" . into ( ) , 1 ) ;
1461
+ map. insert ( "required-associated-types" . into ( ) , 1 ) ;
1462
+ map. insert ( "provided-associated-types" . into ( ) , 1 ) ;
1463
+ map. insert ( "provided-associated-consts" . into ( ) , 1 ) ;
1464
+ map. insert ( "required-associated-consts" . into ( ) , 1 ) ;
1465
+ map. insert ( "required-methods" . into ( ) , 1 ) ;
1466
+ map. insert ( "provided-methods" . into ( ) , 1 ) ;
1467
+ map. insert ( "implementors" . into ( ) , 1 ) ;
1468
+ map. insert ( "synthetic-implementors" . into ( ) , 1 ) ;
1469
+ map. insert ( "implementations-list" . into ( ) , 1 ) ;
1470
+ map. insert ( "trait-implementations-list" . into ( ) , 1 ) ;
1471
+ map. insert ( "synthetic-implementations-list" . into ( ) , 1 ) ;
1472
+ map. insert ( "blanket-implementations-list" . into ( ) , 1 ) ;
1473
+ map. insert ( "deref-methods" . into ( ) , 1 ) ;
1470
1474
map
1471
1475
}
1472
1476
1473
1477
impl IdMap {
1474
1478
pub fn new ( ) -> Self {
1475
- IdMap { map : init_id_map ( ) }
1479
+ IdMap { map : DEFAULT_ID_MAP . clone ( ) }
1476
1480
}
1477
1481
1478
1482
crate fn derive < S : AsRef < str > + ToString > ( & mut self , candidate : S ) -> String {
@@ -1485,7 +1489,7 @@ impl IdMap {
1485
1489
}
1486
1490
} ;
1487
1491
1488
- self . map . insert ( id. clone ( ) , 1 ) ;
1492
+ self . map . insert ( id. clone ( ) . into ( ) , 1 ) ;
1489
1493
id
1490
1494
}
1491
1495
}
0 commit comments