@@ -110,15 +110,15 @@ where
110
110
unsafe fn resolve_index_to_str_unchecked ( & self , index : usize ) -> & str {
111
111
// SAFETY: The function is marked unsafe so that the caller guarantees
112
112
// that required invariants are checked.
113
- let slice_len = unsafe { self . buffer . get_unchecked ( index..) } ;
113
+ let bytes = unsafe { self . buffer . get_unchecked ( index..) } ;
114
114
// SAFETY: The function is marked unsafe so that the caller guarantees
115
115
// that required invariants are checked.
116
- let ( str_len, str_len_bytes) = unsafe { decode_var_usize_unchecked ( slice_len ) } ;
117
- let start_str = index + str_len_bytes;
116
+ let ( str_len, str_len_bytes) = unsafe { decode_var_usize_unchecked ( bytes ) } ;
117
+ let index_str = index + str_len_bytes;
118
118
let str_bytes =
119
119
// SAFETY: The function is marked unsafe so that the caller guarantees
120
120
// that required invariants are checked.
121
- unsafe { self . buffer . get_unchecked ( start_str..start_str + str_len) } ;
121
+ unsafe { self . buffer . get_unchecked ( index_str..index_str + str_len) } ;
122
122
// SAFETY: It is guaranteed by the backend that only valid strings
123
123
// are stored in this portion of the buffer.
124
124
unsafe { str:: from_utf8_unchecked ( str_bytes) }
@@ -227,9 +227,26 @@ fn encode_var_usize(buffer: &mut Vec<u8>, mut value: usize) -> usize {
227
227
#[ inline]
228
228
unsafe fn decode_var_usize_unchecked ( buffer : & [ u8 ] ) -> ( usize , usize ) {
229
229
let first = unsafe { * buffer. get_unchecked ( 0 ) } ;
230
- if first <= 0x7F_u8 {
231
- return ( first as usize , 1 ) ;
230
+ match first {
231
+ byte if byte <= 0x7F_u8 => ( byte as usize , 1 ) ,
232
+ _ => unsafe { decode_var_usize_unchecked_cold ( buffer) } ,
232
233
}
234
+ }
235
+
236
+ /// Decodes from a variable length encoded `usize` from the buffer.
237
+ ///
238
+ /// Returns the decoded value as first return value.
239
+ /// Returns the number of decoded bytes as second return value.
240
+ ///
241
+ /// # Safety
242
+ ///
243
+ /// The caller has to make sure that the buffer contains the necessary
244
+ /// bytes needed to properly decode a valid `usize` value.
245
+ ///
246
+ /// Uncommon case for string lengths of 254 or greater.
247
+ #[ inline]
248
+ #[ cold]
249
+ unsafe fn decode_var_usize_unchecked_cold ( buffer : & [ u8 ] ) -> ( usize , usize ) {
233
250
let mut result: usize = 0 ;
234
251
let mut i = 0 ;
235
252
loop {
@@ -248,11 +265,24 @@ unsafe fn decode_var_usize_unchecked(buffer: &[u8]) -> (usize, usize) {
248
265
///
249
266
/// Returns the decoded value as first return value.
250
267
/// Returns the number of decoded bytes as second return value.
268
+ #[ inline]
251
269
fn decode_var_usize ( buffer : & [ u8 ] ) -> Option < ( usize , usize ) > {
252
- if !buffer. is_empty ( ) && buffer[ 0 ] <= 0x7F_u8 {
253
- // Shortcut the common case for low values.
254
- return Some ( ( buffer[ 0 ] as usize , 1 ) ) ;
270
+ match buffer. first ( ) {
271
+ None => None ,
272
+ Some ( & byte) if byte <= 0x7F_u8 => Some ( ( byte as usize , 1 ) ) ,
273
+ _ => decode_var_usize_cold ( buffer) ,
255
274
}
275
+ }
276
+
277
+ /// Decodes from a variable length encoded `usize` from the buffer.
278
+ ///
279
+ /// Returns the decoded value as first return value.
280
+ /// Returns the number of decoded bytes as second return value.
281
+ ///
282
+ /// Uncommon case for string lengths of 254 or greater.
283
+ #[ inline]
284
+ #[ cold]
285
+ fn decode_var_usize_cold ( buffer : & [ u8 ] ) -> Option < ( usize , usize ) > {
256
286
let mut result: usize = 0 ;
257
287
let mut i = 0 ;
258
288
loop {
@@ -410,17 +440,17 @@ where
410
440
411
441
pub struct Iter < ' a , S > {
412
442
backend : & ' a BufferBackend < S > ,
413
- yielded : usize ,
414
- current : usize ,
443
+ remaining : usize ,
444
+ next : usize ,
415
445
}
416
446
417
447
impl < ' a , S > Iter < ' a , S > {
418
448
#[ cfg_attr( feature = "inline-more" , inline) ]
419
449
pub fn new ( backend : & ' a BufferBackend < S > ) -> Self {
420
450
Self {
421
451
backend,
422
- yielded : 0 ,
423
- current : 0 ,
452
+ remaining : backend . len_strings ,
453
+ next : 0 ,
424
454
}
425
455
}
426
456
}
@@ -440,11 +470,11 @@ where
440
470
#[ inline]
441
471
fn next ( & mut self ) -> Option < Self :: Item > {
442
472
self . backend
443
- . resolve_index_to_str ( self . current )
444
- . and_then ( |( string, next_string_index ) | {
445
- let symbol = S :: try_from_usize ( self . current ) ?;
446
- self . current = next_string_index ;
447
- self . yielded + = 1 ;
473
+ . resolve_index_to_str ( self . next )
474
+ . and_then ( |( string, next ) | {
475
+ let symbol = S :: try_from_usize ( self . next ) ?;
476
+ self . next = next ;
477
+ self . remaining - = 1 ;
448
478
Some ( ( symbol, string) )
449
479
} )
450
480
}
@@ -454,7 +484,8 @@ impl<'a, S> ExactSizeIterator for Iter<'a, S>
454
484
where
455
485
S : Symbol ,
456
486
{
487
+ #[ inline]
457
488
fn len ( & self ) -> usize {
458
- self . backend . len_strings - self . yielded
489
+ self . remaining
459
490
}
460
491
}
0 commit comments