@@ -13,29 +13,54 @@ scans them linearly to search.
13
13
14
14
` search.js ` calls this Raw, because it turns it into
15
15
a more normal object tree after loading it.
16
- Naturally , it's also written without newlines or spaces.
16
+ For space savings , it's also written without newlines or spaces.
17
17
18
18
``` json
19
19
[
20
20
[ " crate_name" , {
21
- "doc" : " Documentation " ,
21
+ // name
22
22
"n" : [" function_name" , " Data" ],
23
+ // type
23
24
"t" : " HF" ,
24
- "d" : [ " This function gets the name of an integer with Data " , " The data struct " ],
25
+ // parent module
25
26
"q" : [[0 , " crate_name" ]],
27
+ // parent type
26
28
"i" : [2 , 0 ],
27
- "p" : [[1 , " i32" ], [1 , " str" ], [5 , " crate_name::Data" ]],
28
- "f" : " {{gb}{d}}`" ,
29
+ // function signature
30
+ "f" : " {{gb}{d}}`" , // [[3, 1], [2]]
31
+ // impl disambiguator
29
32
"b" : [],
30
- "c" : [],
33
+ // deprecated flag
34
+ "c" : " OjAAAAAAAAA=" , // empty bitmap
35
+ // empty description flag
36
+ "e" : " OjAAAAAAAAA=" , // empty bitmap
37
+ // type dictionary
38
+ "p" : [[1 , " i32" ], [1 , " str" ], [5 , " crate_name::Data" ]],
39
+ // aliases
31
40
"a" : [[" get_name" , 0 ]],
41
+ // description shards
42
+ "D" : " g" , // 3
32
43
}]
33
44
]
34
45
```
35
46
36
47
[ ` src/librustdoc/html/static/js/externs.js ` ]
37
48
defines an actual schema in a Closure ` @typedef ` .
38
49
50
+ | Key | Name | Description |
51
+ | --- | -------------------- | ------------ |
52
+ | ` n ` | Names | Item names |
53
+ | ` t ` | Item Type | One-char item type code |
54
+ | ` q ` | Parent module | ` Map<index, path> ` |
55
+ | ` i ` | Parent type | list of indexes |
56
+ | ` f ` | Function signature | [ encoded] ( #i-f-and-p ) |
57
+ | ` b ` | Impl disambiguator | ` Map<index, string> ` |
58
+ | ` c ` | Deprecation flag | [ roaring bitmap] ( #roaring-bitmaps ) |
59
+ | ` e ` | Description is empty | [ roaring bitmap] ( #roaring-bitmaps ) |
60
+ | ` p ` | Type dictionary | ` [[item type, path]] ` |
61
+ | ` a ` | Alias | ` Map<string, index> ` |
62
+ | ` D ` | description shards | [ encoded] ( #how-descriptions-are-stored ) |
63
+
39
64
The above index defines a crate called ` crate_name `
40
65
with a free function called ` function_name ` and a struct called ` Data ` ,
41
66
with the type signature ` Data, i32 -> str ` ,
@@ -78,36 +103,45 @@ It makes a lot of compromises:
78
103
79
104
### Parallel arrays and indexed maps
80
105
81
- Most data in the index
82
- (other than ` doc ` , which is a single string for the whole crate,
83
- ` p ` , which is a separate structure
84
- and ` a ` , which is also a separate structure)
85
- is a set of parallel arrays defining each searchable item.
106
+ Abstractly, Rustdoc Search data is a table, stored in column-major form.
107
+ Most data in the index represents a set of parallel arrays
108
+ (the "columns") which refer to the same data if they're at the same position.
86
109
87
110
For example,
88
111
the above search index can be turned into this table:
89
112
90
- | n | t | d | q | i | f | b | c |
91
- | ---| ---| ---| ---| ---| ---| ---| ---|
92
- | ` function_name ` | ` H ` | This function gets the name of an integer with Data | ` crate_name ` | 2 | ` {{gb}{d}} ` | NULL | NULL |
93
- | ` Data ` | ` F ` | The data struct | ` crate_name ` | 0 | `` ` `` | NULL | NULL |
113
+ | | n | t | [ d] | q | i | f | b | c |
114
+ | ---| ---| ---| -----| ---| ---| ---| ---| ---|
115
+ | 0 | ` crate_name ` | ` D ` | Documentation | NULL | 0 | NULL | NULL | 0 |
116
+ | 1 | ` function_name ` | ` H ` | This function gets the name of an integer with Data | ` crate_name ` | 2 | ` {{gb}{d}} ` | NULL | 0 |
117
+ | 2 | ` Data ` | ` F ` | The data struct | ` crate_name ` | 0 | `` ` `` | NULL | 0 |
118
+
119
+ [ d ] : #how-descriptions-are-stored
120
+
121
+ The crate row is implied in most columns, since its type is known (it's a crate),
122
+ it can't have a parent (crates form the root of the module tree),
123
+ its name is specified as the map key,
124
+ and function-specific data like the impl disambiguator can't apply either.
125
+ However, it can still have a description and it can still be deprecated.
126
+ The crate, therefore, has a primary key of ` 0 ` .
94
127
95
128
The above code doesn't use ` c ` , which holds deprecated indices,
96
129
or ` b ` , which maps indices to strings.
97
- If ` crate_name::function_name ` used both, it would look like this.
130
+ If ` crate_name::function_name ` used both, it might look like this.
98
131
99
132
``` json
100
133
"b" : [[0 , " impl-Foo-for-Bar" ]],
101
- "c" : [ 0 ] ,
134
+ "c" : " OjAAAAEAAAAAAAIAEAAAABUAbgZYCQ== " ,
102
135
```
103
136
104
- This attaches a disambiguator to index 0 and marks it deprecated.
137
+ This attaches a disambiguator to index 1 and marks it deprecated.
105
138
106
139
The advantage of this layout is that these APIs often have implicit structure
107
140
that DEFLATE can take advantage of,
108
141
but that rustdoc can't assume.
109
142
Like how names are usually CamelCase or snake_case,
110
143
but descriptions aren't.
144
+ It also makes it easier to use a sparse data for things like boolean flags.
111
145
112
146
` q ` is a Map from * the first applicable* ID to a parent module path.
113
147
This is a weird trick, but it makes more sense in pseudo-code:
@@ -129,6 +163,98 @@ before serializing.
129
163
Doing this allows rustdoc to not only make the search index smaller,
130
164
but reuse the same string representing the parent path across multiple in-memory items.
131
165
166
+ ### Representing sparse columns
167
+
168
+ #### VLQ Hex
169
+
170
+ This format is, as far as I know, used nowhere other than rustdoc.
171
+ It follows this grammar:
172
+
173
+ ``` ebnf
174
+ VLQHex = { VHItem | VHBackref }
175
+ VHItem = VHNumber | ( '{', {VHItem}, '}' )
176
+ VHNumber = { '@' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' }, ( '`' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k ' | 'l' | 'm' | 'n' | 'o' )
177
+ VHBackref = ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ':' | ';' | '<' | '=' | '>' | '?' )
178
+ ```
179
+
180
+ A VHNumber is a variable-length, self-terminating base16 number
181
+ (terminated because the last hexit is lowercase while all others are uppercase).
182
+ The sign bit is represented using [ zig-zag encoding] .
183
+
184
+ This alphabet is chosen because the characters can be turned into hexits by
185
+ masking off the last four bits of the ASCII encoding.
186
+
187
+ A major feature of this encoding, as with all of the "compression" done in rustdoc,
188
+ is that it can remain in its compressed format * even in memory at runtime* .
189
+ This is why ` HBackref ` is only used at the top level,
190
+ and why we don't just use [ Flate] for everything: the decoder in search.js
191
+ will reuse the entire decoded object whenever a backref is seen,
192
+ saving decode work and memory.
193
+
194
+ [ zig-zag encoding ] : https://en.wikipedia.org/wiki/Variable-length_quantity#Zigzag_encoding
195
+ [ Flate ] : https://en.wikipedia.org/wiki/Deflate
196
+
197
+ #### Roaring Bitmaps
198
+
199
+ Flag-style data, such as deprecation and empty descriptions,
200
+ are stored using the [ standard Roaring Bitmap serialization format with runs] .
201
+ The data is then base64 encoded when writing it.
202
+
203
+ As a brief overview: a roaring bitmap is a chunked array of bits,
204
+ described in [ this paper] .
205
+ A chunk can either be a list of integers, a bitfield, or a list of runs.
206
+ In any case, the search engine has to base64 decode it,
207
+ and read the chunk index itself,
208
+ but the payload data stays as-is.
209
+
210
+ All roaring bitmaps in rustdoc currently store a flag for each item index.
211
+ The crate is item 0, all others start at 1.
212
+
213
+ [ standard Roaring Bitmap serialization format with runs ] : https://github.com/RoaringBitmap/RoaringFormatSpec
214
+ [ this paper ] : https://arxiv.org/pdf/1603.06549.pdf
215
+
216
+ ### How descriptions are stored
217
+
218
+ The largest amount of data,
219
+ and the main thing Rustdoc Search deals with that isn't
220
+ actually used for searching, is descriptions.
221
+ In a SERP table, this is what appears on the rightmost column.
222
+
223
+ > | item type | item path | *** description*** (this part) |
224
+ > | --------- | --------------------- | --------------------------------------------------- |
225
+ > | function | my_crate::my_function | This function gets the name of an integer with Data |
226
+
227
+ When someone runs a search in rustdoc for the first time, their browser will
228
+ work through a "sandwich workload" of three steps:
229
+
230
+ 1 . Download the search-index.js and search.js files (a network bottleneck).
231
+ 2 . Perform the actual search (a CPU and memory bandwidth bottleneck).
232
+ 3 . Download the description data (another network bottleneck).
233
+
234
+ Reducing the amount of data downloaded here will almost always increase latency,
235
+ by delaying the decision of what to download behind other work and/or adding
236
+ data dependencies where something can't be downloaded without first downloading
237
+ something else. In this case, we can't start downloading descriptions until
238
+ after the search is done, because that's what allows it to decide * which*
239
+ descriptions to download (it needs to sort the results then truncate to 200).
240
+
241
+ To do this, two columns are stored in the search index, building on both
242
+ Roaring Bitmaps and on VLQ Hex.
243
+
244
+ * ` e ` is an index of ** e** mpty descriptions. It's a [ roaring bitmap] of
245
+ each item (the crate itself is item 0, the rest start at 1).
246
+ * ` D ` is a shard list, stored in [ VLQ hex] as flat list of integers.
247
+ Each integer gives you the number of descriptions in the shard.
248
+ As the decoder walks the index, it checks if the description is empty.
249
+ if it's not, then it's in the "current" shard. When all items are
250
+ exhausted, it goes on to the next shard.
251
+
252
+ Inside each shard is a newline-delimited list of descriptions,
253
+ wrapped in a JSONP-style function call.
254
+
255
+ [ roaring bitmap ] : #roaring-bitmaps
256
+ [ VLQ hex ] : #vlq-hex
257
+
132
258
### ` i ` , ` f ` , and ` p `
133
259
134
260
` i ` and ` f ` both index into ` p ` , the array of parent items.
@@ -139,33 +265,19 @@ It's different from `q` because `q` represents the parent *module or crate*,
139
265
which everything has,
140
266
while ` i ` /` q ` are used for * type and trait-associated items* like methods.
141
267
142
- ` f ` , the function signatures, use their own encoding.
143
-
144
- ``` ebnf
145
- f = { FItem | FBackref }
146
- FItem = FNumber | ( '{', {FItem}, '}' )
147
- FNumber = { '@' | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' | 'G' | 'H' | 'I' | 'J' | 'K' | 'L' | 'M' | 'N' | 'O' }, ( '`' | 'a' | 'b' | 'c' | 'd' | 'e' | 'f' | 'g' | 'h' | 'i' | 'j' | 'k ' | 'l' | 'm' | 'n' | 'o' )
148
- FBackref = ( '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' | ':' | ';' | '<' | '=' | '>' | '?' )
149
- ```
268
+ ` f ` , the function signatures, use a [ VLQ hex] tree.
269
+ A number is either a one-indexed reference into ` p ` ,
270
+ a negative number representing a generic,
271
+ or zero for null.
150
272
151
- An FNumber is a variable-length, self-terminating base16 number
152
- (terminated because the last hexit is lowercase while all others are uppercase).
153
- These are one-indexed references into ` p ` , because zero is used for nulls,
154
- and negative numbers represent generics.
155
- The sign bit is represented using [ zig-zag encoding]
156
273
(the internal object representation also uses negative numbers,
157
274
even after decoding,
158
275
to represent generics).
159
- This alphabet is chosen because the characters can be turned into hexits by
160
- masking off the last four bits of the ASCII encoding.
161
276
162
277
For example, ` {{gb}{d}} ` is equivalent to the json ` [[3, 1], [2]] ` .
163
278
Because of zigzag encoding, `` ` `` is +0, ` a ` is -0 (which is not used),
164
279
` b ` is +1, and ` c ` is -1.
165
280
166
- [ empirically ] : https://github.com/rust-lang/rust/pull/83003
167
- [ zig-zag encoding ] : https://en.wikipedia.org/wiki/Variable-length_quantity#Zigzag_encoding
168
-
169
281
## Searching by name
170
282
171
283
Searching by name works by looping through the search index
0 commit comments