Skip to content

Commit 36fa557

Browse files
Merge all loops into one when generating search index
1 parent 05eda41 commit 36fa557

File tree

1 file changed

+65
-92
lines changed

1 file changed

+65
-92
lines changed

src/librustdoc/html/render/search_index.rs

+65-92
Original file line numberDiff line numberDiff line change
@@ -265,102 +265,75 @@ pub(crate) fn build_index<'tcx>(
265265
})
266266
.collect::<Vec<_>>();
267267

268+
let mut names = Vec::with_capacity(self.items.len());
269+
let mut types = String::with_capacity(self.items.len());
270+
let mut full_paths = Vec::with_capacity(self.items.len());
271+
let mut descriptions = Vec::with_capacity(self.items.len());
272+
let mut parents = Vec::with_capacity(self.items.len());
273+
let mut functions = Vec::with_capacity(self.items.len());
274+
let mut deprecated = Vec::with_capacity(self.items.len());
275+
276+
for (index, item) in self.items.iter().enumerate() {
277+
let n = item.ty as u8;
278+
let c = char::try_from(n + b'A').expect("item types must fit in ASCII");
279+
assert!(c <= 'z', "item types must fit within ASCII printables");
280+
types.push(c);
281+
282+
assert_eq!(
283+
item.parent.is_some(),
284+
item.parent_idx.is_some(),
285+
"`{}` is missing idx",
286+
item.name
287+
);
288+
// 0 is a sentinel, everything else is one-indexed
289+
parents.push(item.parent_idx.map(|x| x + 1).unwrap_or(0));
290+
291+
names.push(item.name.as_str());
292+
descriptions.push(&item.desc);
293+
294+
if !item.path.is_empty() {
295+
full_paths.push((index, &item.path));
296+
}
297+
298+
// Fake option to get `0` out as a sentinel instead of `null`.
299+
// We want to use `0` because it's three less bytes.
300+
enum FunctionOption<'a> {
301+
Function(&'a IndexItemFunctionType),
302+
None,
303+
}
304+
impl<'a> Serialize for FunctionOption<'a> {
305+
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
306+
where
307+
S: Serializer,
308+
{
309+
match self {
310+
FunctionOption::None => 0.serialize(serializer),
311+
FunctionOption::Function(ty) => ty.serialize(serializer),
312+
}
313+
}
314+
}
315+
functions.push(match &item.search_type {
316+
Some(ty) => FunctionOption::Function(ty),
317+
None => FunctionOption::None,
318+
});
319+
320+
if item.deprecation.is_some() {
321+
deprecated.push(index);
322+
}
323+
}
324+
268325
let has_aliases = !self.aliases.is_empty();
269326
let mut crate_data =
270327
serializer.serialize_struct("CrateData", if has_aliases { 9 } else { 8 })?;
271328
crate_data.serialize_field("doc", &self.doc)?;
272-
crate_data.serialize_field(
273-
"t",
274-
&self
275-
.items
276-
.iter()
277-
.map(|item| {
278-
let n = item.ty as u8;
279-
let c = char::try_from(n + b'A').expect("item types must fit in ASCII");
280-
assert!(c <= 'z', "item types must fit within ASCII printables");
281-
c
282-
})
283-
.collect::<String>(),
284-
)?;
285-
crate_data.serialize_field(
286-
"n",
287-
&self.items.iter().map(|item| item.name.as_str()).collect::<Vec<_>>(),
288-
)?;
289-
crate_data.serialize_field(
290-
"q",
291-
&self
292-
.items
293-
.iter()
294-
.enumerate()
295-
// Serialize as an array of item indices and full paths
296-
.filter_map(
297-
|(index, item)| {
298-
if item.path.is_empty() { None } else { Some((index, &item.path)) }
299-
},
300-
)
301-
.collect::<Vec<_>>(),
302-
)?;
303-
crate_data.serialize_field(
304-
"d",
305-
&self.items.iter().map(|item| &item.desc).collect::<Vec<_>>(),
306-
)?;
307-
crate_data.serialize_field(
308-
"i",
309-
&self
310-
.items
311-
.iter()
312-
.map(|item| {
313-
assert_eq!(
314-
item.parent.is_some(),
315-
item.parent_idx.is_some(),
316-
"`{}` is missing idx",
317-
item.name
318-
);
319-
// 0 is a sentinel, everything else is one-indexed
320-
item.parent_idx.map(|x| x + 1).unwrap_or(0)
321-
})
322-
.collect::<Vec<_>>(),
323-
)?;
324-
crate_data.serialize_field(
325-
"f",
326-
&self
327-
.items
328-
.iter()
329-
.map(|item| {
330-
// Fake option to get `0` out as a sentinel instead of `null`.
331-
// We want to use `0` because it's three less bytes.
332-
enum FunctionOption<'a> {
333-
Function(&'a IndexItemFunctionType),
334-
None,
335-
}
336-
impl<'a> Serialize for FunctionOption<'a> {
337-
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
338-
where
339-
S: Serializer,
340-
{
341-
match self {
342-
FunctionOption::None => 0.serialize(serializer),
343-
FunctionOption::Function(ty) => ty.serialize(serializer),
344-
}
345-
}
346-
}
347-
match &item.search_type {
348-
Some(ty) => FunctionOption::Function(ty),
349-
None => FunctionOption::None,
350-
}
351-
})
352-
.collect::<Vec<_>>(),
353-
)?;
354-
crate_data.serialize_field(
355-
"c",
356-
&self
357-
.items
358-
.iter()
359-
.enumerate()
360-
// Serialize as an array of deprecated item indices
361-
.filter_map(|(index, item)| item.deprecation.map(|_| index))
362-
.collect::<Vec<_>>(),
363-
)?;
329+
crate_data.serialize_field("t", &types)?;
330+
crate_data.serialize_field("n", &names)?;
331+
// Serialize as an array of item indices and full paths
332+
crate_data.serialize_field("q", &full_paths)?;
333+
crate_data.serialize_field("d", &descriptions)?;
334+
crate_data.serialize_field("i", &parents)?;
335+
crate_data.serialize_field("f", &functions)?;
336+
crate_data.serialize_field("c", &deprecated)?;
364337
crate_data.serialize_field("p", &paths)?;
365338
if has_aliases {
366339
crate_data.serialize_field("a", &self.aliases)?;

0 commit comments

Comments
 (0)