From d2e4b59e60aae6f47baae337a3a7f9f04c728592 Mon Sep 17 00:00:00 2001
From: Peter Jaszkowiak
Date: Fri, 3 Feb 2023 00:08:57 -0700
Subject: [PATCH] rustdoc: sort deprecated items lower in search
serialize `q` (`itemPaths`) sparsely
overall 4% reduction in search index size
---
src/librustdoc/formats/cache.rs | 1 +
src/librustdoc/html/render/mod.rs | 1 +
src/librustdoc/html/render/print_item.rs | 9 ++++----
src/librustdoc/html/render/search_index.rs | 23 +++++++++++++++++++-
src/librustdoc/html/static/js/search.js | 25 ++++++++++++++++++----
5 files changed, 50 insertions(+), 9 deletions(-)
diff --git a/src/librustdoc/formats/cache.rs b/src/librustdoc/formats/cache.rs
index 24752cddb337c..e7480978c2591 100644
--- a/src/librustdoc/formats/cache.rs
+++ b/src/librustdoc/formats/cache.rs
@@ -337,6 +337,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
self.cache,
),
aliases: item.attrs.get_doc_aliases(),
+ deprecation: item.deprecation(self.tcx),
});
}
}
diff --git a/src/librustdoc/html/render/mod.rs b/src/librustdoc/html/render/mod.rs
index 816a8f4e274ce..dc96c452cd2c2 100644
--- a/src/librustdoc/html/render/mod.rs
+++ b/src/librustdoc/html/render/mod.rs
@@ -107,6 +107,7 @@ pub(crate) struct IndexItem {
pub(crate) parent_idx: Option,
pub(crate) search_type: Option,
pub(crate) aliases: Box<[Symbol]>,
+ pub(crate) deprecation: Option,
}
/// A type used for the search index.
diff --git a/src/librustdoc/html/render/print_item.rs b/src/librustdoc/html/render/print_item.rs
index bd7548003ad3b..4c76ee156c98e 100644
--- a/src/librustdoc/html/render/print_item.rs
+++ b/src/librustdoc/html/render/print_item.rs
@@ -470,10 +470,11 @@ fn extra_info_tags(item: &clean::Item, parent: &clean::Item, tcx: TyCtxt<'_>) ->
// The trailing space after each tag is to space it properly against the rest of the docs.
if let Some(depr) = &item.deprecation(tcx) {
- let mut message = "Deprecated";
- if !stability::deprecation_in_effect(depr) {
- message = "Deprecation planned";
- }
+ let message = if stability::deprecation_in_effect(depr) {
+ "Deprecated"
+ } else {
+ "Deprecation planned"
+ };
tags += &tag_html("deprecated", "", message);
}
diff --git a/src/librustdoc/html/render/search_index.rs b/src/librustdoc/html/render/search_index.rs
index 5b0caac099bc3..4ff39d6b14366 100644
--- a/src/librustdoc/html/render/search_index.rs
+++ b/src/librustdoc/html/render/search_index.rs
@@ -42,6 +42,7 @@ pub(crate) fn build_index<'tcx>(
parent_idx: None,
search_type: get_function_type_for_search(item, tcx, impl_generics.as_ref(), cache),
aliases: item.attrs.get_doc_aliases(),
+ deprecation: item.deprecation(tcx),
});
}
}
@@ -244,7 +245,17 @@ pub(crate) fn build_index<'tcx>(
)?;
crate_data.serialize_field(
"q",
- &self.items.iter().map(|item| &item.path).collect::>(),
+ &self
+ .items
+ .iter()
+ .enumerate()
+ // Serialize as an array of item indices and full paths
+ .filter_map(
+ |(index, item)| {
+ if item.path.is_empty() { None } else { Some((index, &item.path)) }
+ },
+ )
+ .collect::>(),
)?;
crate_data.serialize_field(
"d",
@@ -297,6 +308,16 @@ pub(crate) fn build_index<'tcx>(
})
.collect::>(),
)?;
+ crate_data.serialize_field(
+ "c",
+ &self
+ .items
+ .iter()
+ .enumerate()
+ // Serialize as an array of deprecated item indices
+ .filter_map(|(index, item)| item.deprecation.map(|_| index))
+ .collect::>(),
+ )?;
crate_data.serialize_field(
"p",
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::>(),
diff --git a/src/librustdoc/html/static/js/search.js b/src/librustdoc/html/static/js/search.js
index 88592fa0c84c1..5697d005b33ea 100644
--- a/src/librustdoc/html/static/js/search.js
+++ b/src/librustdoc/html/static/js/search.js
@@ -811,6 +811,13 @@ function initSearch(rawSearchIndex) {
return a - b;
}
+ // sort deprecated items later
+ a = aaa.item.deprecated;
+ b = bbb.item.deprecated;
+ if (a !== b) {
+ return a - b;
+ }
+
// sort by crate (current crate comes first)
a = (aaa.item.crate !== preferredCrate);
b = (bbb.item.crate !== preferredCrate);
@@ -1170,6 +1177,7 @@ function initSearch(rawSearchIndex) {
parent: item.parent,
type: item.type,
is_alias: true,
+ deprecated: item.deprecated,
};
}
@@ -1965,10 +1973,11 @@ function initSearch(rawSearchIndex) {
* n: Array,
* t: Array,
* d: Array,
- * q: Array,
+ * q: Array<[Number, string]>,
* i: Array,
* f: Array,
* p: Array