1
Fork 0

Merge all loops into one when generating search index

This commit is contained in:
Guillaume Gomez 2023-09-01 14:46:47 +02:00
parent 05eda41658
commit 36fa557a5e

View file

@ -265,51 +265,20 @@ pub(crate) fn build_index<'tcx>(
}) })
.collect::<Vec<_>>(); .collect::<Vec<_>>();
let has_aliases = !self.aliases.is_empty(); let mut names = Vec::with_capacity(self.items.len());
let mut crate_data = let mut types = String::with_capacity(self.items.len());
serializer.serialize_struct("CrateData", if has_aliases { 9 } else { 8 })?; let mut full_paths = Vec::with_capacity(self.items.len());
crate_data.serialize_field("doc", &self.doc)?; let mut descriptions = Vec::with_capacity(self.items.len());
crate_data.serialize_field( let mut parents = Vec::with_capacity(self.items.len());
"t", let mut functions = Vec::with_capacity(self.items.len());
&self let mut deprecated = Vec::with_capacity(self.items.len());
.items
.iter() for (index, item) in self.items.iter().enumerate() {
.map(|item| {
let n = item.ty as u8; let n = item.ty as u8;
let c = char::try_from(n + b'A').expect("item types must fit in ASCII"); let c = char::try_from(n + b'A').expect("item types must fit in ASCII");
assert!(c <= 'z', "item types must fit within ASCII printables"); assert!(c <= 'z', "item types must fit within ASCII printables");
c types.push(c);
})
.collect::<String>(),
)?;
crate_data.serialize_field(
"n",
&self.items.iter().map(|item| item.name.as_str()).collect::<Vec<_>>(),
)?;
crate_data.serialize_field(
"q",
&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(
"d",
&self.items.iter().map(|item| &item.desc).collect::<Vec<_>>(),
)?;
crate_data.serialize_field(
"i",
&self
.items
.iter()
.map(|item| {
assert_eq!( assert_eq!(
item.parent.is_some(), item.parent.is_some(),
item.parent_idx.is_some(), item.parent_idx.is_some(),
@ -317,16 +286,15 @@ pub(crate) fn build_index<'tcx>(
item.name item.name
); );
// 0 is a sentinel, everything else is one-indexed // 0 is a sentinel, everything else is one-indexed
item.parent_idx.map(|x| x + 1).unwrap_or(0) parents.push(item.parent_idx.map(|x| x + 1).unwrap_or(0));
})
.collect::<Vec<_>>(), names.push(item.name.as_str());
)?; descriptions.push(&item.desc);
crate_data.serialize_field(
"f", if !item.path.is_empty() {
&self full_paths.push((index, &item.path));
.items }
.iter()
.map(|item| {
// Fake option to get `0` out as a sentinel instead of `null`. // Fake option to get `0` out as a sentinel instead of `null`.
// We want to use `0` because it's three less bytes. // We want to use `0` because it's three less bytes.
enum FunctionOption<'a> { enum FunctionOption<'a> {
@ -344,23 +312,28 @@ pub(crate) fn build_index<'tcx>(
} }
} }
} }
match &item.search_type { functions.push(match &item.search_type {
Some(ty) => FunctionOption::Function(ty), Some(ty) => FunctionOption::Function(ty),
None => FunctionOption::None, None => FunctionOption::None,
});
if item.deprecation.is_some() {
deprecated.push(index);
} }
}) }
.collect::<Vec<_>>(),
)?; let has_aliases = !self.aliases.is_empty();
crate_data.serialize_field( let mut crate_data =
"c", serializer.serialize_struct("CrateData", if has_aliases { 9 } else { 8 })?;
&self crate_data.serialize_field("doc", &self.doc)?;
.items crate_data.serialize_field("t", &types)?;
.iter() crate_data.serialize_field("n", &names)?;
.enumerate() // Serialize as an array of item indices and full paths
// Serialize as an array of deprecated item indices crate_data.serialize_field("q", &full_paths)?;
.filter_map(|(index, item)| item.deprecation.map(|_| index)) crate_data.serialize_field("d", &descriptions)?;
.collect::<Vec<_>>(), crate_data.serialize_field("i", &parents)?;
)?; crate_data.serialize_field("f", &functions)?;
crate_data.serialize_field("c", &deprecated)?;
crate_data.serialize_field("p", &paths)?; crate_data.serialize_field("p", &paths)?;
if has_aliases { if has_aliases {
crate_data.serialize_field("a", &self.aliases)?; crate_data.serialize_field("a", &self.aliases)?;