From e17ac668997410a1a9d2da8725329afb8b5f2901 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Wed, 13 May 2020 17:03:37 +0200 Subject: [PATCH] * 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) --- src/librustdoc/html/render/cache.rs | 16 +++++++--------- src/librustdoc/html/static/main.js | 7 ++++--- 2 files changed, 11 insertions(+), 12 deletions(-) diff --git a/src/librustdoc/html/render/cache.rs b/src/librustdoc/html/render/cache.rs index b8d97c2ac503b..57d385de32096 100644 --- a/src/librustdoc/html/render/cache.rs +++ b/src/librustdoc/html/render/cache.rs @@ -120,7 +120,7 @@ crate struct Cache { /// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias, /// we need the alias element to have an array of items. - pub(super) aliases: FxHashMap>, + pub(super) aliases: BTreeMap>, } impl Cache { @@ -331,7 +331,7 @@ impl DocFolder for Cache { for alias in item.attrs.get_doc_aliases() { self.aliases .entry(alias.to_lowercase()) - .or_insert(Vec::with_capacity(1)) + .or_insert(Vec::new()) .push(self.search_index.len() - 1); } } @@ -553,10 +553,10 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { parent_idx: None, search_type: get_index_search_type(&item), }); - for alias in item.attrs.get_doc_aliases().into_iter() { + for alias in item.attrs.get_doc_aliases() { aliases .entry(alias.to_lowercase()) - .or_insert(Vec::with_capacity(1)) + .or_insert(Vec::new()) .push(search_index.len() - 1); } } @@ -600,9 +600,6 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { .map(|module| shorten(plain_summary_line(module.doc_value()))) .unwrap_or(String::new()); - let crate_aliases = - aliases.iter().map(|(k, values)| (k.clone(), values.clone())).collect::>(); - #[derive(Serialize)] struct CrateData<'a> { doc: String, @@ -614,7 +611,8 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { // // To be noted: the `usize` elements are indexes to `items`. #[serde(rename = "a")] - aliases: Option)>>, + #[serde(skip_serializing_if = "BTreeMap::is_empty")] + aliases: &'a BTreeMap>, } // Collect the index into a string @@ -625,7 +623,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String { doc: crate_doc, items: crate_items, paths: crate_paths, - aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) }, + aliases, }) .expect("failed serde conversion") // All these `replace` calls are because we have to go through JS string for JSON content. diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 7592331dd656d..9b498d66249e2 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1781,12 +1781,13 @@ function getSearchElement() { if (aliases) { ALIASES[crate] = {}; var j, local_aliases; - for (i = 0; i < aliases.length; ++i) { - var alias_name = aliases[i][0]; + for (var alias_name in aliases) { + if (!aliases.hasOwnProperty(alias_name)) { continue; } + if (!ALIASES[crate].hasOwnProperty(alias_name)) { ALIASES[crate][alias_name] = []; } - local_aliases = aliases[i][1]; + local_aliases = aliases[alias_name]; for (j = 0; j < local_aliases.length; ++j) { ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex); }