Skip to content

Commit 25b7207

Browse files
committed
Extract common functionality into closure
Also, use `Vec::resize` instead of individual `push`es
1 parent 51866db commit 25b7207

File tree

1 file changed

+39
-37
lines changed

1 file changed

+39
-37
lines changed

src/librustdoc/html/render/search_index.rs

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1864,48 +1864,50 @@ pub(crate) fn build_index(
18641864
// unoccupied size.
18651865
if item.ty.is_fn_like() { 0 } else { 16 };
18661866
serialized_index.function_data[new_entry_id] = Some(search_type.clone());
1867-
for index in used_in_function_inputs {
1868-
let postings = if index >= 0 {
1869-
assert!(serialized_index.path_data[index as usize].is_some());
1870-
&mut serialized_index.type_data[index as usize]
1871-
.as_mut()
1872-
.unwrap()
1873-
.inverted_function_inputs_index
1874-
} else {
1875-
let generic_id = usize::try_from(-index).unwrap() - 1;
1876-
for _ in serialized_index.generic_inverted_index.len()..=generic_id {
1877-
serialized_index.generic_inverted_index.push(Vec::new());
1867+
1868+
#[derive(Clone, Copy)]
1869+
enum InvertedIndexType {
1870+
Inputs,
1871+
Output,
1872+
}
1873+
impl InvertedIndexType {
1874+
fn from_type_data(self, type_data: &mut TypeData) -> &mut Vec<Vec<u32>> {
1875+
match self {
1876+
Self::Inputs => &mut type_data.inverted_function_inputs_index,
1877+
Self::Output => &mut type_data.inverted_function_output_index,
18781878
}
1879-
&mut serialized_index.generic_inverted_index[generic_id]
1880-
};
1881-
while postings.len() <= search_type_size {
1882-
postings.push(Vec::new());
1883-
}
1884-
if postings[search_type_size].last() != Some(&(new_entry_id as u32)) {
1885-
postings[search_type_size].push(new_entry_id as u32);
18861879
}
18871880
}
1888-
for index in used_in_function_output {
1889-
let postings = if index >= 0 {
1890-
assert!(serialized_index.path_data[index as usize].is_some());
1891-
&mut serialized_index.type_data[index as usize]
1892-
.as_mut()
1893-
.unwrap()
1894-
.inverted_function_output_index
1895-
} else {
1896-
let generic_id = usize::try_from(-index).unwrap() - 1;
1897-
for _ in serialized_index.generic_inverted_index.len()..=generic_id {
1898-
serialized_index.generic_inverted_index.push(Vec::new());
1881+
1882+
let mut process_used_in_function =
1883+
|used_in_function: BTreeSet<isize>, index_type: InvertedIndexType| {
1884+
for index in used_in_function {
1885+
let postings = if index >= 0 {
1886+
assert!(serialized_index.path_data[index as usize].is_some());
1887+
index_type.from_type_data(
1888+
serialized_index.type_data[index as usize].as_mut().unwrap(),
1889+
)
1890+
} else {
1891+
let generic_id = index.unsigned_abs() - 1;
1892+
if generic_id >= serialized_index.generic_inverted_index.len() {
1893+
serialized_index
1894+
.generic_inverted_index
1895+
.resize(generic_id + 1, Vec::new());
1896+
}
1897+
&mut serialized_index.generic_inverted_index[generic_id]
1898+
};
1899+
if search_type_size >= postings.len() {
1900+
postings.resize(search_type_size + 1, Vec::new());
1901+
}
1902+
let posting = &mut postings[search_type_size];
1903+
if posting.last() != Some(&(new_entry_id as u32)) {
1904+
posting.push(new_entry_id as u32);
1905+
}
18991906
}
1900-
&mut serialized_index.generic_inverted_index[generic_id]
19011907
};
1902-
while postings.len() <= search_type_size {
1903-
postings.push(Vec::new());
1904-
}
1905-
if postings[search_type_size].last() != Some(&(new_entry_id as u32)) {
1906-
postings[search_type_size].push(new_entry_id as u32);
1907-
}
1908-
}
1908+
1909+
process_used_in_function(used_in_function_inputs, InvertedIndexType::Inputs);
1910+
process_used_in_function(used_in_function_output, InvertedIndexType::Output);
19091911
}
19101912
}
19111913

0 commit comments

Comments
 (0)