File tree 3 files changed +36
-5
lines changed
3 files changed +36
-5
lines changed Original file line number Diff line number Diff line change @@ -234,7 +234,33 @@ pub(crate) fn build_index<'tcx>(
234
234
) ?;
235
235
crate_data. serialize_field (
236
236
"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 < _ > > ( ) ,
238
264
) ?;
239
265
crate_data. serialize_field (
240
266
"p" ,
Original file line number Diff line number Diff line change @@ -86,6 +86,9 @@ let Results;
86
86
* A pair of [inputs, outputs], or 0 for null. This is stored in the search index.
87
87
* The JavaScript deserializes this into FunctionSearchType.
88
88
*
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
+ *
89
92
* An input or output can be encoded as just a number if there is only one of them, AND
90
93
* it has no generics. The no generics rule exists to avoid ambiguity: imagine if you had
91
94
* a function with a single output, and that output had a single generic:
@@ -114,6 +117,9 @@ let RawFunctionSearchType;
114
117
* A single function input or output type. This is either a single path ID, or a pair of
115
118
* [path ID, generics].
116
119
*
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
+ *
117
123
* @typedef {number | [number, Array<RawFunctionType>] }
118
124
*/
119
125
let RawFunctionType ;
Original file line number Diff line number Diff line change @@ -1846,9 +1846,6 @@ function initSearch(rawSearchIndex) {
1846
1846
function buildItemSearchTypeAll ( types , lowercasePaths ) {
1847
1847
const PATH_INDEX_DATA = 0 ;
1848
1848
const GENERICS_DATA = 1 ;
1849
- if ( types === null ) {
1850
- return [ ] ;
1851
- }
1852
1849
return types . map ( type => {
1853
1850
let pathIndex , generics ;
1854
1851
if ( typeof type === "number" ) {
@@ -1859,6 +1856,7 @@ function initSearch(rawSearchIndex) {
1859
1856
generics = buildItemSearchTypeAll ( type [ GENERICS_DATA ] , lowercasePaths ) ;
1860
1857
}
1861
1858
return {
1859
+ // `0` is used as a sentinel because it's fewer bytes than `null`
1862
1860
name : pathIndex === 0 ? null : lowercasePaths [ pathIndex - 1 ] . name ,
1863
1861
ty : pathIndex === 0 ? null : lowercasePaths [ pathIndex - 1 ] . ty ,
1864
1862
generics : generics ,
@@ -1884,7 +1882,8 @@ function initSearch(rawSearchIndex) {
1884
1882
function buildFunctionSearchType ( functionSearchType , lowercasePaths ) {
1885
1883
const INPUTS_DATA = 0 ;
1886
1884
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 ) {
1888
1887
return null ;
1889
1888
}
1890
1889
let inputs , output ;
You can’t perform that action at this time.
0 commit comments