Skip to content

Commit 12d007d

Browse files
authored
Rollup merge of #83835 - notriddle:sort-index, r=ollie27
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
2 parents 67ffbed + 2370e3b commit 12d007d

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

src/librustdoc/clean/types.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -914,7 +914,7 @@ impl Attributes {
914914
.collect()
915915
}
916916

917-
crate fn get_doc_aliases(&self) -> FxHashSet<String> {
917+
crate fn get_doc_aliases(&self) -> Box<[String]> {
918918
let mut aliases = FxHashSet::default();
919919

920920
for attr in self.other_attrs.lists(sym::doc).filter(|a| a.has_name(sym::alias)) {
@@ -931,7 +931,7 @@ impl Attributes {
931931
aliases.insert(attr.value_str().map(|s| s.to_string()).unwrap());
932932
}
933933
}
934-
aliases
934+
aliases.into_iter().collect::<Vec<String>>().into()
935935
}
936936
}
937937

src/librustdoc/formats/cache.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -120,10 +120,6 @@ crate struct Cache {
120120
// when gathering trait documentation on a type, hold impls here while
121121
// folding and add them to the cache later on if we find the trait.
122122
orphan_trait_impls: Vec<(DefId, FxHashSet<DefId>, Impl)>,
123-
124-
/// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
125-
/// we need the alias element to have an array of items.
126-
crate aliases: BTreeMap<String, Vec<usize>>,
127123
}
128124

129125
/// This struct is used to wrap the `cache` and `tcx` in order to run `DocFolder`.
@@ -309,15 +305,8 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
309305
parent,
310306
parent_idx: None,
311307
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
308+
aliases: item.attrs.get_doc_aliases(),
312309
});
313-
314-
for alias in item.attrs.get_doc_aliases() {
315-
self.cache
316-
.aliases
317-
.entry(alias.to_lowercase())
318-
.or_insert(Vec::new())
319-
.push(self.cache.search_index.len() - 1);
320-
}
321310
}
322311
}
323312
(Some(parent), None) if is_inherent_impl_item => {

src/librustdoc/html/render/cache.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -82,18 +82,31 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
8282
parent: Some(did),
8383
parent_idx: None,
8484
search_type: get_index_search_type(&item, cache, tcx),
85+
aliases: item.attrs.get_doc_aliases(),
8586
});
86-
for alias in item.attrs.get_doc_aliases() {
87-
cache
88-
.aliases
89-
.entry(alias.to_lowercase())
90-
.or_insert(Vec::new())
91-
.push(cache.search_index.len() - 1);
92-
}
9387
}
9488
}
9589

96-
let Cache { ref mut search_index, ref paths, ref mut aliases, .. } = *cache;
90+
let Cache { ref mut search_index, ref paths, .. } = *cache;
91+
92+
// Aliases added through `#[doc(alias = "...")]`. Since a few items can have the same alias,
93+
// we need the alias element to have an array of items.
94+
let mut aliases: BTreeMap<String, Vec<usize>> = BTreeMap::new();
95+
96+
// Sort search index items. This improves the compressibility of the search index.
97+
search_index.sort_unstable_by(|k1, k2| {
98+
// `sort_unstable_by_key` produces lifetime errors
99+
let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent);
100+
let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent);
101+
std::cmp::Ord::cmp(&k1, &k2)
102+
});
103+
104+
// Set up alias indexes.
105+
for (i, item) in search_index.iter().enumerate() {
106+
for alias in &item.aliases[..] {
107+
aliases.entry(alias.to_lowercase()).or_insert(Vec::new()).push(i);
108+
}
109+
}
97110

98111
// Reduce `DefId` in paths into smaller sequential numbers,
99112
// and prune the paths that do not appear in the index.
@@ -201,7 +214,7 @@ crate fn build_index<'tcx>(krate: &clean::Crate, cache: &mut Cache, tcx: TyCtxt<
201214
doc: crate_doc,
202215
items: crate_items,
203216
paths: crate_paths,
204-
aliases,
217+
aliases: &aliases,
205218
})
206219
.expect("failed serde conversion")
207220
// All these `replace` calls are because we have to go through JS string for JSON content.

src/librustdoc/html/render/mod.rs

+1
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ crate struct IndexItem {
164164
crate parent: Option<DefId>,
165165
crate parent_idx: Option<usize>,
166166
crate search_type: Option<IndexItemFunctionType>,
167+
crate aliases: Box<[String]>,
167168
}
168169

169170
/// A type used for the search index.

0 commit comments

Comments
 (0)