rustdoc: sort search index items for compression

This should not affect the appearance of the docs pages themselves.

This makes the pre-compressed search index smaller, thanks to the
empty-string path duplication format, and also the gzipped version,
by giving the algorithm more structure to work with.

    rust$ wc -c search-index-old.js search-index-new.js
    2628334 search-index-old.js
    2586181 search-index-new.js
    5214515 total
    rust$ gzip search-index-*
    rust$ wc -c search-index-old.js.gz search-index-new.js.gz
    239486 search-index-old.js.gz
    237386 search-index-new.js.gz
    476872 total
This commit is contained in:
Michael Howell 2021-04-03 19:08:14 -07:00
parent 0b417ab5cd
commit 5f92951d4f
4 changed files with 20 additions and 17 deletions
src/librustdoc
clean
formats
html/render

View File

@ -914,7 +914,7 @@ fn update_need_backline(doc_strings: &mut Vec<DocFragment>, frag: &DocFragment)
.collect()
}
crate fn get_doc_aliases(&self) -> FxHashSet<String> {
crate fn get_doc_aliases(&self) -> Box<[String]> {
let mut aliases = FxHashSet::default();
for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) {
@ -931,7 +931,7 @@ fn update_need_backline(doc_strings: &mut Vec<DocFragment>, frag: &DocFragment)
aliases.insert(attr.value_str().map(|s| s.to_string()).unwrap());
}
}
aliases
aliases.into_iter().collect::<Vec<String>>().into()
}
}

View File

@ -309,15 +309,8 @@ fn fold_item(&mut self, item: clean::Item) -> Option<clean::Item> {
parent,
parent_idx: None,
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
aliases: item.attrs.get_doc_aliases(),
});
for alias in item.attrs.get_doc_aliases() {
self.cache
.aliases
.entry(alias.to_lowercase())
.or_insert(Vec::new())
.push(self.cache.search_index.len() - 1);
}
}
}
(Some(parent), None) if is_inherent_impl_item => {

View File

@ -82,19 +82,28 @@
parent: Some(did),
parent_idx: None,
search_type: get_index_search_type(&item, cache, tcx),
aliases: item.attrs.get_doc_aliases(),
});
for alias in item.attrs.get_doc_aliases() {
cache
.aliases
.entry(alias.to_lowercase())
.or_insert(Vec::new())
.push(cache.search_index.len() - 1);
}
}
}
let Cache { ref mut search_index, ref paths, ref mut aliases, .. } = *cache;
// Sort search index items. This improves the compressibility of the search index.
search_index.sort_unstable_by(|k1, k2| {
// `sort_unstable_by_key` produces lifetime errors
let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent);
let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent);
std::cmp::Ord::cmp(&k1, &k2)
});
// Set up alias indexes.
for (i, item) in search_index.iter().enumerate() {
for alias in &item.aliases[..] {
aliases.entry(alias.to_lowercase()).or_insert(Vec::new()).push(i);
}
}
// Reduce `DefId` in paths into smaller sequential numbers,
// and prune the paths that do not appear in the index.
let mut lastpath = String::new();

View File

@ -164,6 +164,7 @@ impl SharedContext<'_> {
crate parent: Option<DefId>,
crate parent_idx: Option<usize>,
crate search_type: Option<IndexItemFunctionType>,
crate aliases: Box<[String]>,
}
/// A type used for the search index.