@@ -397,9 +397,21 @@ pub struct HashMap<K, V, S = RandomState> {
397
397
resize_policy : DefaultResizePolicy ,
398
398
}
399
399
400
+ /// Search for a pre-hashed key when the hash map is known to be non-empty.
401
+ #[ inline]
402
+ fn search_hashed_nonempty < K , V , M , F > ( table : M , hash : SafeHash , is_match : F )
403
+ -> InternalEntry < K , V , M >
404
+ where M : Deref < Target = RawTable < K , V > > ,
405
+ F : FnMut ( & K ) -> bool
406
+ {
407
+ // Do not check the capacity as an extra branch could slow the lookup.
408
+ search_hashed_body ( table, hash, is_match)
409
+ }
410
+
400
411
/// Search for a pre-hashed key.
412
+ /// If you don't already know the hash, use search or search_mut instead
401
413
#[ inline]
402
- fn search_hashed < K , V , M , F > ( table : M , hash : SafeHash , mut is_match : F ) -> InternalEntry < K , V , M >
414
+ fn search_hashed < K , V , M , F > ( table : M , hash : SafeHash , is_match : F ) -> InternalEntry < K , V , M >
403
415
where M : Deref < Target = RawTable < K , V > > ,
404
416
F : FnMut ( & K ) -> bool
405
417
{
@@ -410,6 +422,16 @@ fn search_hashed<K, V, M, F>(table: M, hash: SafeHash, mut is_match: F) -> Inter
410
422
return InternalEntry :: TableIsEmpty ;
411
423
}
412
424
425
+ search_hashed_body ( table, hash, is_match)
426
+ }
427
+
428
+ /// The body of the search_hashed[_nonempty] functions
429
+ #[ inline]
430
+ fn search_hashed_body < K , V , M , F > ( table : M , hash : SafeHash , mut is_match : F )
431
+ -> InternalEntry < K , V , M >
432
+ where M : Deref < Target = RawTable < K , V > > ,
433
+ F : FnMut ( & K ) -> bool
434
+ {
413
435
let size = table. size ( ) ;
414
436
let mut probe = Bucket :: new ( table, hash) ;
415
437
let mut displacement = 0 ;
@@ -550,17 +572,25 @@ impl<K, V, S> HashMap<K, V, S>
550
572
where K : Borrow < Q > ,
551
573
Q : Eq + Hash
552
574
{
553
- let hash = self . make_hash ( q) ;
554
- search_hashed ( & self . table , hash, |k| q. eq ( k. borrow ( ) ) )
575
+ if self . table . capacity ( ) != 0 {
576
+ let hash = self . make_hash ( q) ;
577
+ search_hashed_nonempty ( & self . table , hash, |k| q. eq ( k. borrow ( ) ) )
578
+ } else {
579
+ InternalEntry :: TableIsEmpty
580
+ }
555
581
}
556
582
557
583
#[ inline]
558
584
fn search_mut < ' a , Q : ?Sized > ( & ' a mut self , q : & Q ) -> InternalEntry < K , V , & ' a mut RawTable < K , V > >
559
585
where K : Borrow < Q > ,
560
586
Q : Eq + Hash
561
587
{
562
- let hash = self . make_hash ( q) ;
563
- search_hashed ( & mut self . table , hash, |k| q. eq ( k. borrow ( ) ) )
588
+ if self . table . capacity ( ) != 0 {
589
+ let hash = self . make_hash ( q) ;
590
+ search_hashed_nonempty ( & mut self . table , hash, |k| q. eq ( k. borrow ( ) ) )
591
+ } else {
592
+ InternalEntry :: TableIsEmpty
593
+ }
564
594
}
565
595
566
596
// The caller should ensure that invariants by Robin Hood Hashing hold
0 commit comments