Skip to content

Commit 33cf9ea

Browse files
committed
Add comments, fixes for 0 sentinel
1 parent b54e3e6 commit 33cf9ea

File tree

3 files changed

+36
-5
lines changed

3 files changed

+36
-5
lines changed

src/librustdoc/html/render/search_index.rs

+27-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,33 @@ pub(crate) fn build_index<'tcx>(
234234
)?;
235235
crate_data.serialize_field(
236236
"f",
237-
&self.items.iter().map(|item| &item.search_type).collect::<Vec<_>>(),
237+
&self
238+
.items
239+
.iter()
240+
.map(|item| {
241+
// Fake option to get `0` out as a sentinel instead of `null`.
242+
// We want to use `0` because it's three less bytes.
243+
enum FunctionOption<'a> {
244+
Function(&'a IndexItemFunctionType),
245+
None,
246+
}
247+
impl<'a> Serialize for FunctionOption<'a> {
248+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
249+
where
250+
S: Serializer,
251+
{
252+
match self {
253+
FunctionOption::None => 0.serialize(serializer),
254+
FunctionOption::Function(ty) => ty.serialize(serializer),
255+
}
256+
}
257+
}
258+
match &item.search_type {
259+
Some(ty) => FunctionOption::Function(ty),
260+
None => FunctionOption::None,
261+
}
262+
})
263+
.collect::<Vec<_>>(),
238264
)?;
239265
crate_data.serialize_field(
240266
"p",

src/librustdoc/html/static/js/externs.js

+6
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ let Results;
8686
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.
8787
* The JavaScript deserializes this into FunctionSearchType.
8888
*
89+
* Numeric IDs are *ONE-indexed* into the paths array (`p`). Zero is used as a sentinel for `null`
90+
* because `null` is four bytes while `0` is one byte.
91+
*
8992
* An input or output can be encoded as just a number if there is only one of them, AND
9093
* it has no generics. The no generics rule exists to avoid ambiguity: imagine if you had
9194
* a function with a single output, and that output had a single generic:
@@ -114,6 +117,9 @@ let RawFunctionSearchType;
114117
* A single function input or output type. This is either a single path ID, or a pair of
115118
* [path ID, generics].
116119
*
120+
* Numeric IDs are *ONE-indexed* into the paths array (`p`). Zero is used as a sentinel for `null`
121+
* because `null` is four bytes while `0` is one byte.
122+
*
117123
* @typedef {number | [number, Array<RawFunctionType>]}
118124
*/
119125
let RawFunctionType;

src/librustdoc/html/static/js/search.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -1846,9 +1846,6 @@ function initSearch(rawSearchIndex) {
18461846
function buildItemSearchTypeAll(types, lowercasePaths) {
18471847
const PATH_INDEX_DATA = 0;
18481848
const GENERICS_DATA = 1;
1849-
if (types === null) {
1850-
return [];
1851-
}
18521849
return types.map(type => {
18531850
let pathIndex, generics;
18541851
if (typeof type === "number") {
@@ -1859,6 +1856,7 @@ function initSearch(rawSearchIndex) {
18591856
generics = buildItemSearchTypeAll(type[GENERICS_DATA], lowercasePaths);
18601857
}
18611858
return {
1859+
// `0` is used as a sentinel because it's fewer bytes than `null`
18621860
name: pathIndex === 0 ? null : lowercasePaths[pathIndex - 1].name,
18631861
ty: pathIndex === 0 ? null : lowercasePaths[pathIndex - 1].ty,
18641862
generics: generics,
@@ -1884,7 +1882,8 @@ function initSearch(rawSearchIndex) {
18841882
function buildFunctionSearchType(functionSearchType, lowercasePaths) {
18851883
const INPUTS_DATA = 0;
18861884
const OUTPUT_DATA = 1;
1887-
if (functionSearchType === 0 || functionSearchType === null) {
1885+
// `0` is used as a sentinel because it's fewer bytes than `null`
1886+
if (functionSearchType === 0) {
18881887
return null;
18891888
}
18901889
let inputs, output;

0 commit comments

Comments
 (0)