@@ -3,9 +3,9 @@ mod serde;
33
44use std:: collections:: BTreeSet ;
55use std:: collections:: hash_map:: Entry ;
6- use std:: io;
76use std:: path:: Path ;
87use std:: string:: FromUtf8Error ;
8+ use std:: { io, iter} ;
99
1010use :: serde:: de:: { self , Deserializer , Error as _} ;
1111use :: serde:: ser:: { SerializeSeq , Serializer } ;
@@ -256,10 +256,14 @@ impl SerializedSearchIndex {
256256 /// The returned ID can be used to attach more data to the search result.
257257 fn add_entry ( & mut self , name : Symbol , entry_data : EntryData , desc : String ) -> usize {
258258 let fqp = if let Some ( module_path_index) = entry_data. module_path {
259- let mut fqp = self . path_data [ module_path_index] . as_ref ( ) . unwrap ( ) . module_path . clone ( ) ;
260- fqp. push ( Symbol :: intern ( & self . names [ module_path_index] ) ) ;
261- fqp. push ( name) ;
262- fqp
259+ self . path_data [ module_path_index]
260+ . as_ref ( )
261+ . unwrap ( )
262+ . module_path
263+ . iter ( )
264+ . copied ( )
265+ . chain ( [ Symbol :: intern ( & self . names [ module_path_index] ) , name] )
266+ . collect ( )
263267 } else {
264268 vec ! [ name]
265269 } ;
@@ -306,13 +310,13 @@ impl SerializedSearchIndex {
306310
307311 pub ( crate ) fn union ( mut self , other : & SerializedSearchIndex ) -> SerializedSearchIndex {
308312 let other_entryid_offset = self . names . len ( ) ;
309- let mut map_other_pathid_to_self_pathid: Vec < usize > = Vec :: new ( ) ;
313+ let mut map_other_pathid_to_self_pathid = Vec :: with_capacity ( other . path_data . len ( ) ) ;
310314 let mut skips = FxHashSet :: default ( ) ;
311315 for ( other_pathid, other_path_data) in other. path_data . iter ( ) . enumerate ( ) {
312316 if let Some ( other_path_data) = other_path_data {
313- let mut fqp = other_path_data. module_path . clone ( ) ;
314317 let name = Symbol :: intern ( & other. names [ other_pathid] ) ;
315- fqp. push ( name) ;
318+ let fqp =
319+ other_path_data. module_path . iter ( ) . copied ( ) . chain ( iter:: once ( name) ) . collect ( ) ;
316320 let self_pathid = other_entryid_offset + other_pathid;
317321 let self_pathid = match self . crate_paths_index . entry ( ( other_path_data. ty , fqp) ) {
318322 Entry :: Vacant ( slot) => {
@@ -555,19 +559,19 @@ impl SerializedSearchIndex {
555559 ) ;
556560 }
557561 }
558- for ( i , other_generic_inverted_index ) in other. generic_inverted_index . iter ( ) . enumerate ( ) {
559- for ( size , other_list ) in other_generic_inverted_index . iter ( ) . enumerate ( ) {
560- let self_generic_inverted_index = match self . generic_inverted_index . get_mut ( i ) {
561- Some ( self_generic_inverted_index) => self_generic_inverted_index ,
562- None => {
563- self . generic_inverted_index . push ( Vec :: new ( ) ) ;
564- self . generic_inverted_index . last_mut ( ) . unwrap ( )
565- }
566- } ;
567- while self_generic_inverted_index . len ( ) <= size {
568- self_generic_inverted_index . push ( Vec :: new ( ) ) ;
569- }
570- self_generic_inverted_index [ size ] . extend (
562+ if other. generic_inverted_index . len ( ) > self . generic_inverted_index . len ( ) {
563+ self . generic_inverted_index . resize ( other . generic_inverted_index . len ( ) , Vec :: new ( ) ) ;
564+ }
565+ for ( other_generic_inverted_index , self_generic_inverted_index) in
566+ iter :: zip ( & other . generic_inverted_index , & mut self . generic_inverted_index )
567+ {
568+ if other_generic_inverted_index . len ( ) > self_generic_inverted_index . len ( ) {
569+ self_generic_inverted_index . resize ( other_generic_inverted_index . len ( ) , Vec :: new ( ) ) ;
570+ }
571+ for ( other_list , self_list ) in
572+ iter :: zip ( other_generic_inverted_index , self_generic_inverted_index )
573+ {
574+ self_list . extend (
571575 other_list
572576 . iter ( )
573577 . copied ( )
@@ -1237,6 +1241,26 @@ impl<'de> Deserialize<'de> for SerializedOptional32 {
12371241 }
12381242}
12391243
1244+ trait VecExt < T > {
1245+ fn get_mut_ensured ( & mut self , index : usize ) -> & mut T
1246+ where
1247+ T : Default + Clone ;
1248+ }
1249+
1250+ impl < T > VecExt < T > for Vec < T > {
1251+ fn get_mut_ensured ( & mut self , index : usize ) -> & mut T
1252+ where
1253+ T : Default ,
1254+ {
1255+ if self . len ( ) <= index {
1256+ & mut self [ index]
1257+ } else {
1258+ self . resize_with ( index, T :: default) ;
1259+ self . push_mut ( T :: default ( ) )
1260+ }
1261+ }
1262+ }
1263+
12401264/// Builds the search index from the collected metadata
12411265pub ( crate ) fn build_index (
12421266 krate : & clean:: Crate ,
@@ -1819,20 +1843,23 @@ pub(crate) fn build_index(
18191843 tcx,
18201844 ) ;
18211845 }
1822- let mut used_in_constraints = Vec :: new ( ) ;
1823- for constraint in & mut search_type. where_clause {
1824- let mut used_in_constraint = BTreeSet :: new ( ) ;
1825- for trait_ in & mut constraint[ ..] {
1826- convert_render_type (
1827- trait_,
1828- cache,
1829- & mut serialized_index,
1830- & mut used_in_constraint,
1831- tcx,
1832- ) ;
1833- }
1834- used_in_constraints. push ( used_in_constraint) ;
1835- }
1846+ let used_in_constraints = search_type
1847+ . where_clause
1848+ . iter_mut ( )
1849+ . map ( |constraint| {
1850+ let mut used_in_constraint = BTreeSet :: new ( ) ;
1851+ for trait_ in constraint {
1852+ convert_render_type (
1853+ trait_,
1854+ cache,
1855+ & mut serialized_index,
1856+ & mut used_in_constraint,
1857+ tcx,
1858+ ) ;
1859+ }
1860+ used_in_constraint
1861+ } )
1862+ . collect :: < Vec < _ > > ( ) ;
18361863 loop {
18371864 let mut inserted_any = false ;
18381865 for ( i, used_in_constraint) in used_in_constraints. iter ( ) . enumerate ( ) {
@@ -1873,16 +1900,11 @@ pub(crate) fn build_index(
18731900 . inverted_function_inputs_index
18741901 } else {
18751902 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 ( ) ) ;
1878- }
1879- & mut serialized_index. generic_inverted_index [ generic_id]
1903+ serialized_index. generic_inverted_index . get_mut_ensured ( generic_id)
18801904 } ;
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 ) ;
1905+ let posting = postings. get_mut_ensured ( search_type_size) ;
1906+ if posting. last ( ) != Some ( & ( new_entry_id as u32 ) ) {
1907+ posting. push ( new_entry_id as u32 ) ;
18861908 }
18871909 }
18881910 for index in used_in_function_output {
@@ -1899,11 +1921,9 @@ pub(crate) fn build_index(
18991921 }
19001922 & mut serialized_index. generic_inverted_index [ generic_id]
19011923 } ;
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 ) ;
1924+ let posting = postings. get_mut_ensured ( search_type_size) ;
1925+ if posting. last ( ) != Some ( & ( new_entry_id as u32 ) ) {
1926+ posting. push ( new_entry_id as u32 ) ;
19071927 }
19081928 }
19091929 }
0 commit comments