rustdoc: sort deprecated items lower in search

serialize `q` (`itemPaths`) sparsely
overall 4% reduction in search index size
This commit is contained in:
Peter Jaszkowiak 2023-02-03 00:08:57 -07:00
parent 6c991b0740
commit d2e4b59e60
5 changed files with 50 additions and 9 deletions

View File

@ -337,6 +337,7 @@ impl<'a, 'tcx> DocFolder for CacheBuilder<'a, 'tcx> {
self.cache, self.cache,
), ),
aliases: item.attrs.get_doc_aliases(), aliases: item.attrs.get_doc_aliases(),
deprecation: item.deprecation(self.tcx),
}); });
} }
} }

View File

@ -107,6 +107,7 @@ pub(crate) struct IndexItem {
pub(crate) parent_idx: Option<usize>, pub(crate) parent_idx: Option<usize>,
pub(crate) search_type: Option<IndexItemFunctionType>, pub(crate) search_type: Option<IndexItemFunctionType>,
pub(crate) aliases: Box<[Symbol]>, pub(crate) aliases: Box<[Symbol]>,
pub(crate) deprecation: Option<Deprecation>,
} }
/// A type used for the search index. /// A type used for the search index.

View File

@ -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. // The trailing space after each tag is to space it properly against the rest of the docs.
if let Some(depr) = &item.deprecation(tcx) { if let Some(depr) = &item.deprecation(tcx) {
let mut message = "Deprecated"; let message = if stability::deprecation_in_effect(depr) {
if !stability::deprecation_in_effect(depr) { "Deprecated"
message = "Deprecation planned"; } else {
} "Deprecation planned"
};
tags += &tag_html("deprecated", "", message); tags += &tag_html("deprecated", "", message);
} }

View File

@ -42,6 +42,7 @@ pub(crate) fn build_index<'tcx>(
parent_idx: None, parent_idx: None,
search_type: get_function_type_for_search(item, tcx, impl_generics.as_ref(), cache), search_type: get_function_type_for_search(item, tcx, impl_generics.as_ref(), cache),
aliases: item.attrs.get_doc_aliases(), aliases: item.attrs.get_doc_aliases(),
deprecation: item.deprecation(tcx),
}); });
} }
} }
@ -244,7 +245,17 @@ pub(crate) fn build_index<'tcx>(
)?; )?;
crate_data.serialize_field( crate_data.serialize_field(
"q", "q",
&self.items.iter().map(|item| &item.path).collect::<Vec<_>>(), &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::<Vec<_>>(),
)?; )?;
crate_data.serialize_field( crate_data.serialize_field(
"d", "d",
@ -297,6 +308,16 @@ pub(crate) fn build_index<'tcx>(
}) })
.collect::<Vec<_>>(), .collect::<Vec<_>>(),
)?; )?;
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::<Vec<_>>(),
)?;
crate_data.serialize_field( crate_data.serialize_field(
"p", "p",
&self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(), &self.paths.iter().map(|(it, s)| (it, s.as_str())).collect::<Vec<_>>(),

View File

@ -811,6 +811,13 @@ function initSearch(rawSearchIndex) {
return a - b; 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) // sort by crate (current crate comes first)
a = (aaa.item.crate !== preferredCrate); a = (aaa.item.crate !== preferredCrate);
b = (bbb.item.crate !== preferredCrate); b = (bbb.item.crate !== preferredCrate);
@ -1170,6 +1177,7 @@ function initSearch(rawSearchIndex) {
parent: item.parent, parent: item.parent,
type: item.type, type: item.type,
is_alias: true, is_alias: true,
deprecated: item.deprecated,
}; };
} }
@ -1965,10 +1973,11 @@ function initSearch(rawSearchIndex) {
* n: Array<string>, * n: Array<string>,
* t: Array<Number>, * t: Array<Number>,
* d: Array<string>, * d: Array<string>,
* q: Array<string>, * q: Array<[Number, string]>,
* i: Array<Number>, * i: Array<Number>,
* f: Array<RawFunctionSearchType>, * f: Array<RawFunctionSearchType>,
* p: Array<Object>, * p: Array<Object>,
* c: Array<Number>
* }} * }}
*/ */
const crateCorpus = rawSearchIndex[crate]; const crateCorpus = rawSearchIndex[crate];
@ -1987,6 +1996,7 @@ function initSearch(rawSearchIndex) {
type: null, type: null,
id: id, id: id,
normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""), normalizedName: crate.indexOf("_") === -1 ? crate : crate.replace(/_/g, ""),
deprecated: null,
}; };
id += 1; id += 1;
searchIndex.push(crateRow); searchIndex.push(crateRow);
@ -1996,14 +2006,20 @@ function initSearch(rawSearchIndex) {
const itemTypes = crateCorpus.t; const itemTypes = crateCorpus.t;
// an array of (String) item names // an array of (String) item names
const itemNames = crateCorpus.n; const itemNames = crateCorpus.n;
// an array of (String) full paths (or empty string for previous path) // an array of [(Number) item index,
const itemPaths = crateCorpus.q; // (String) full path]
// an item whose index is not present will fall back to the previous present path
// i.e. if indices 4 and 11 are present, but 5-10 and 12-13 are not present,
// 5-10 will fall back to the path for 4 and 12-13 will fall back to the path for 11
const itemPaths = new Map(crateCorpus.q);
// an array of (String) descriptions // an array of (String) descriptions
const itemDescs = crateCorpus.d; const itemDescs = crateCorpus.d;
// an array of (Number) the parent path index + 1 to `paths`, or 0 if none // an array of (Number) the parent path index + 1 to `paths`, or 0 if none
const itemParentIdxs = crateCorpus.i; const itemParentIdxs = crateCorpus.i;
// an array of (Object | null) the type of the function, if any // an array of (Object | null) the type of the function, if any
const itemFunctionSearchTypes = crateCorpus.f; const itemFunctionSearchTypes = crateCorpus.f;
// an array of (Number) indices for the deprecated items
const deprecatedItems = new Set(crateCorpus.c);
// an array of [(Number) item type, // an array of [(Number) item type,
// (String) name] // (String) name]
const paths = crateCorpus.p; const paths = crateCorpus.p;
@ -2045,12 +2061,13 @@ function initSearch(rawSearchIndex) {
crate: crate, crate: crate,
ty: itemTypes[i], ty: itemTypes[i],
name: itemNames[i], name: itemNames[i],
path: itemPaths[i] ? itemPaths[i] : lastPath, path: itemPaths.has(i) ? itemPaths.get(i) : lastPath,
desc: itemDescs[i], desc: itemDescs[i],
parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined, parent: itemParentIdxs[i] > 0 ? paths[itemParentIdxs[i] - 1] : undefined,
type: buildFunctionSearchType(itemFunctionSearchTypes[i], lowercasePaths), type: buildFunctionSearchType(itemFunctionSearchTypes[i], lowercasePaths),
id: id, id: id,
normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""), normalizedName: word.indexOf("_") === -1 ? word : word.replace(/_/g, ""),
deprecated: deprecatedItems.has(i),
}; };
id += 1; id += 1;
searchIndex.push(row); searchIndex.push(row);