Skip to content

Commit e17ac66

Browse files
* Update aliases data struct from HashMap to BTreeMap to have more deterministic results
* Update Javascript to take this change into account * Update CrateData::aliases field to take a reference instead (it allowed to remove a conversion loop)
1 parent c4d9318 commit e17ac66

File tree

2 files changed

+11
-12
lines changed

2 files changed

+11
-12
lines changed

src/librustdoc/html/render/cache.rs

+7-9
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ crate struct Cache {
120120

121121
/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
122122
/// we need the alias element to have an array of items.
123-
pub(super) aliases: FxHashMap<String, Vec<usize>>,
123+
pub(super) aliases: BTreeMap<String, Vec<usize>>,
124124
}
125125

126126
impl Cache {
@@ -331,7 +331,7 @@ impl DocFolder for Cache {
331331
for alias in item.attrs.get_doc_aliases() {
332332
self.aliases
333333
.entry(alias.to_lowercase())
334-
.or_insert(Vec::with_capacity(1))
334+
.or_insert(Vec::new())
335335
.push(self.search_index.len() - 1);
336336
}
337337
}
@@ -553,10 +553,10 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
553553
parent_idx: None,
554554
search_type: get_index_search_type(&item),
555555
});
556-
for alias in item.attrs.get_doc_aliases().into_iter() {
556+
for alias in item.attrs.get_doc_aliases() {
557557
aliases
558558
.entry(alias.to_lowercase())
559-
.or_insert(Vec::with_capacity(1))
559+
.or_insert(Vec::new())
560560
.push(search_index.len() - 1);
561561
}
562562
}
@@ -600,9 +600,6 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
600600
.map(|module| shorten(plain_summary_line(module.doc_value())))
601601
.unwrap_or(String::new());
602602

603-
let crate_aliases =
604-
aliases.iter().map(|(k, values)| (k.clone(), values.clone())).collect::<Vec<_>>();
605-
606603
#[derive(Serialize)]
607604
struct CrateData<'a> {
608605
doc: String,
@@ -614,7 +611,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
614611
//
615612
// To be noted: the `usize` elements are indexes to `items`.
616613
#[serde(rename = "a")]
617-
aliases: Option<Vec<(String, Vec<usize>)>>,
614+
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
615+
aliases: &'a BTreeMap<String, Vec<usize>>,
618616
}
619617

620618
// Collect the index into a string
@@ -625,7 +623,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
625623
doc: crate_doc,
626624
items: crate_items,
627625
paths: crate_paths,
628-
aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) },
626+
aliases,
629627
})
630628
.expect("failed serde conversion")
631629
// All these `replace` calls are because we have to go through JS string for JSON content.

src/librustdoc/html/static/main.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1781,12 +1781,13 @@ function getSearchElement() {
17811781
if (aliases) {
17821782
ALIASES[crate] = {};
17831783
var j, local_aliases;
1784-
for (i = 0; i < aliases.length; ++i) {
1785-
var alias_name = aliases[i][0];
1784+
for (var alias_name in aliases) {
1785+
if (!aliases.hasOwnProperty(alias_name)) { continue; }
1786+
17861787
if (!ALIASES[crate].hasOwnProperty(alias_name)) {
17871788
ALIASES[crate][alias_name] = [];
17881789
}
1789-
local_aliases = aliases[i][1];
1790+
local_aliases = aliases[alias_name];
17901791
for (j = 0; j < local_aliases.length; ++j) {
17911792
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
17921793
}

0 commit comments

Comments
 (0)