Skip to content

Commit c031c78

Browse files
authored
Rollup merge of rust-lang#144320 - lolbinarycat:rustdoc-search_index-BTreeMap-str, r=GuillaumeGomez
rustdoc: avoid allocating a temp String for aliases in search index Here's the optimization I talked about in rust-lang#143988 (comment) I got around the Serialize issue using the newtype pattern. The wrapper type could be factored out into a helper that would work with anything that impls `AsRef<&str>`, but I'm not sure if that would be helpful anywhere else. r? ````@GuillaumeGomez````
2 parents fafdf5b + 9e75032 commit c031c78

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

src/librustdoc/html/render/search_index.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,22 @@ pub(crate) fn build_index(
100100
let crate_doc =
101101
short_markdown_summary(&krate.module.doc_value(), &krate.module.link_names(cache));
102102

103+
#[derive(Eq, Ord, PartialEq, PartialOrd)]
104+
struct SerSymbolAsStr(Symbol);
105+
106+
impl Serialize for SerSymbolAsStr {
107+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
108+
where
109+
S: Serializer,
110+
{
111+
self.0.as_str().serialize(serializer)
112+
}
113+
}
114+
115+
type AliasMap = BTreeMap<SerSymbolAsStr, Vec<usize>>;
103116
// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
104117
// we need the alias element to have an array of items.
105-
let mut aliases: BTreeMap<String, Vec<usize>> = BTreeMap::new();
118+
let mut aliases: AliasMap = BTreeMap::new();
106119

107120
// Sort search index items. This improves the compressibility of the search index.
108121
cache.search_index.sort_unstable_by(|k1, k2| {
@@ -116,7 +129,7 @@ pub(crate) fn build_index(
116129
// Set up alias indexes.
117130
for (i, item) in cache.search_index.iter().enumerate() {
118131
for alias in &item.aliases[..] {
119-
aliases.entry(alias.to_string()).or_default().push(i);
132+
aliases.entry(SerSymbolAsStr(*alias)).or_default().push(i);
120133
}
121134
}
122135

@@ -474,7 +487,7 @@ pub(crate) fn build_index(
474487
// The String is alias name and the vec is the list of the elements with this alias.
475488
//
476489
// To be noted: the `usize` elements are indexes to `items`.
477-
aliases: &'a BTreeMap<String, Vec<usize>>,
490+
aliases: &'a AliasMap,
478491
// Used when a type has more than one impl with an associated item with the same name.
479492
associated_item_disambiguators: &'a Vec<(usize, String)>,
480493
// A list of shard lengths encoded as vlqhex. See the comment in write_vlqhex_to_string

0 commit comments

Comments
 (0)