@@ -241,6 +241,34 @@ impl SerializedSearchIndex {
241
241
self . alias_pointers . push ( alias_pointer) ;
242
242
index
243
243
}
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
+ }
244
272
fn push_path ( & mut self , name : String , path_data : PathData ) -> usize {
245
273
self . push ( name, Some ( path_data) , None , String :: new ( ) , None , None , None )
246
274
}
@@ -1516,10 +1544,9 @@ pub(crate) fn build_index(
1516
1544
. as_ref ( )
1517
1545
. map ( |path| serialized_index. get_id_by_module_path ( path) ) ;
1518
1546
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 {
1523
1550
ty : item. ty ,
1524
1551
parent : item. parent_idx ,
1525
1552
module_path,
@@ -1538,11 +1565,8 @@ pub(crate) fn build_index(
1538
1565
None
1539
1566
} ,
1540
1567
krate : crate_idx,
1541
- } ) ,
1568
+ } ,
1542
1569
item. desc . to_string ( ) ,
1543
- None , // filled in after all the types have been indexed
1544
- None ,
1545
- None ,
1546
1570
) ;
1547
1571
1548
1572
// Aliases
0 commit comments