Skip to content

Commit 4ffeda1

Browse files
committed
Auto merge of #147045 - notriddle:search-index-entrydata-path, r=GuillaumeGomez
rustdoc-search: use the same ID for entry and path to same item This decreases the size of the compiler-doc from 57MiB to 56MiB. r? `@GuillaumeGomez`
2 parents c7f6aa2 + 9dce3e8 commit 4ffeda1

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

src/librustdoc/html/render/search_index.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,34 @@ impl SerializedSearchIndex {
241241
self.alias_pointers.push(alias_pointer);
242242
index
243243
}
244+
/// Add potential search result to the database and return the row ID.
245+
///
246+
/// The returned ID can be used to attach more data to the search result.
247+
fn add_entry(&mut self, name: Symbol, entry_data: EntryData, desc: String) -> usize {
248+
let fqp = if let Some(module_path_index) = entry_data.module_path {
249+
let mut fqp = self.path_data[module_path_index].as_ref().unwrap().module_path.clone();
250+
fqp.push(Symbol::intern(&self.names[module_path_index]));
251+
fqp.push(name);
252+
fqp
253+
} else {
254+
vec![name]
255+
};
256+
// If a path with the same name already exists, but no entry does,
257+
// we can fill in the entry without having to allocate a new row ID.
258+
//
259+
// Because paths and entries both share the same index, using the same
260+
// ID saves space by making the tree smaller.
261+
if let Some(&other_path) = self.crate_paths_index.get(&(entry_data.ty, fqp))
262+
&& self.entry_data[other_path].is_none()
263+
&& self.descs[other_path].is_empty()
264+
{
265+
self.entry_data[other_path] = Some(entry_data);
266+
self.descs[other_path] = desc;
267+
other_path
268+
} else {
269+
self.push(name.as_str().to_string(), None, Some(entry_data), desc, None, None, None)
270+
}
271+
}
244272
fn push_path(&mut self, name: String, path_data: PathData) -> usize {
245273
self.push(name, Some(path_data), None, String::new(), None, None, None)
246274
}
@@ -1516,10 +1544,9 @@ pub(crate) fn build_index(
15161544
.as_ref()
15171545
.map(|path| serialized_index.get_id_by_module_path(path));
15181546

1519-
let new_entry_id = serialized_index.push(
1520-
item.name.as_str().to_string(),
1521-
None,
1522-
Some(EntryData {
1547+
let new_entry_id = serialized_index.add_entry(
1548+
item.name,
1549+
EntryData {
15231550
ty: item.ty,
15241551
parent: item.parent_idx,
15251552
module_path,
@@ -1538,11 +1565,8 @@ pub(crate) fn build_index(
15381565
None
15391566
},
15401567
krate: crate_idx,
1541-
}),
1568+
},
15421569
item.desc.to_string(),
1543-
None, // filled in after all the types have been indexed
1544-
None,
1545-
None,
15461570
);
15471571

15481572
// Aliases

0 commit comments

Comments
 (0)