Skip to content

Commit 5f92951

Browse files
committed
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
1 parent 0b417ab commit 5f92951

File tree

4 files changed

+20
-17
lines changed

4 files changed

+20
-17
lines changed

src/librustdoc/clean/types.rs

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -309,15 +309,8 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
309309
parent,
310310
parent_idx: None,
311311
search_type: get_index_search_type(&item, &self.empty_cache, self.tcx),
312+
aliases: item.attrs.get_doc_aliases(),
312313
});
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-
}
321314
}
322315
}
323316
(Some(parent), None) if is_inherent_impl_item => {

src/librustdoc/html/render/cache.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,19 +82,28 @@ 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

9690
let Cache { ref mut search_index, ref paths, ref mut aliases, .. } = *cache;
9791

92+
// Sort search index items. This improves the compressibility of the search index.
93+
search_index.sort_unstable_by(|k1, k2| {
94+
// `sort_unstable_by_key` produces lifetime errors
95+
let k1 = (&k1.path, &k1.name, &k1.ty, &k1.parent);
96+
let k2 = (&k2.path, &k2.name, &k2.ty, &k2.parent);
97+
std::cmp::Ord::cmp(&k1, &k2)
98+
});
99+
100+
// Set up alias indexes.
101+
for (i, item) in search_index.iter().enumerate() {
102+
for alias in &item.aliases[..] {
103+
aliases.entry(alias.to_lowercase()).or_insert(Vec::new()).push(i);
104+
}
105+
}
106+
98107
// Reduce `DefId` in paths into smaller sequential numbers,
99108
// and prune the paths that do not appear in the index.
100109
let mut lastpath = String::new();

src/librustdoc/html/render/mod.rs

Lines changed: 1 addition & 0 deletions
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)