@@ -52,6 +52,9 @@ pub fn read_signed_vlqhex_from_string(string: &[u8]) -> Option<(i32, usize)> {
5252}
5353
5454pub fn write_postings_to_string ( postings : & [ Vec < u32 > ] , buf : & mut Vec < u8 > ) {
55+ // there's gonna be at least 1 byte pushed for every posting
56+ buf. reserve ( postings. len ( ) ) ;
57+
5558 for list in postings {
5659 if list. is_empty ( ) {
5760 buf. push ( 0 ) ;
@@ -63,29 +66,22 @@ pub fn write_postings_to_string(postings: &[Vec<u32>], buf: &mut Vec<u8>) {
6366 if len_after - len_before > 1 + ( 4 * list. len ( ) ) && list. len ( ) < 0x3a {
6467 buf. truncate ( len_before) ;
6568 buf. push ( list. len ( ) as u8 ) ;
66- for & item in list {
67- buf. push ( item as u8 ) ;
68- buf. push ( ( item >> 8 ) as u8 ) ;
69- buf. push ( ( item >> 16 ) as u8 ) ;
70- buf. push ( ( item >> 24 ) as u8 ) ;
71- }
69+ buf. extend ( list. iter ( ) . copied ( ) . map ( u32:: to_le_bytes) . flatten ( ) ) ;
7270 }
7371 }
7472}
7573
7674pub fn read_postings_from_string ( postings : & mut Vec < Vec < u32 > > , mut buf : & [ u8 ] ) {
7775 use stringdex:: internals:: decode:: RoaringBitmap ;
78- while let Some ( & c) = buf. get ( 0 ) {
76+ while let Some ( & c) = buf. first ( ) {
7977 if c < 0x3a {
8078 buf = & buf[ 1 ..] ;
8179 let buf = buf. split_off ( ..usize:: from ( c) * size_of :: < u32 > ( ) ) . unwrap ( ) ;
8280 let ( chunks, _) = buf. as_chunks ( ) ;
8381 let slot = chunks. iter ( ) . copied ( ) . map ( u32:: from_le_bytes) . collect ( ) ;
8482 postings. push ( slot) ;
8583 } else {
86- let ( bitmap, consumed_bytes_len) =
87- RoaringBitmap :: from_bytes ( buf) . unwrap_or_else ( || ( RoaringBitmap :: default ( ) , 0 ) ) ;
88- assert_ne ! ( consumed_bytes_len, 0 ) ;
84+ let ( bitmap, consumed_bytes_len) = RoaringBitmap :: from_bytes ( buf) . unwrap ( ) ;
8985 postings. push ( bitmap. to_vec ( ) ) ;
9086 buf = & buf[ consumed_bytes_len..] ;
9187 }
0 commit comments