@@ -163,14 +163,14 @@ impl<T> DictInner<T> {
163163 let mut idxs = GenIndexes :: new ( entry. hash , mask) ;
164164 loop {
165165 let index_index = idxs. next ( ) ;
166- let idx = & mut self . indices [ index_index ] ;
167- if * idx == IndexEntry :: FREE {
168- * idx = unsafe {
169- // entry_idx never grow up to usize::MAX
170- IndexEntry :: from_index_unchecked ( entry_idx)
171- } ;
172- entry . index = index_index ;
173- break ;
166+ unsafe {
167+ // index is always valid here
168+ let idx = self . indices . get_unchecked_mut ( index_index ) ;
169+ if * idx == IndexEntry :: FREE {
170+ * idx = IndexEntry :: from_index_unchecked ( entry_idx) ;
171+ entry . index = index_index ;
172+ break ;
173+ }
174174 }
175175 }
176176 } else {
@@ -531,7 +531,7 @@ impl<T: Clone> Dict<T> {
531531 mut lock : Option < PyRwLockReadGuard < DictInner < T > > > ,
532532 ) -> PyResult < LookupResult > {
533533 let mut idxs = None ;
534- let mut freeslot = None ;
534+ let mut free_slot = None ;
535535 let ret = ' outer: loop {
536536 let ( entry_key, ret) = {
537537 let inner = lock. take ( ) . unwrap_or_else ( || self . read ( ) ) ;
@@ -540,25 +540,27 @@ impl<T: Clone> Dict<T> {
540540 } ) ;
541541 loop {
542542 let index_index = idxs. next ( ) ;
543- match inner. indices [ index_index] {
543+ let index_entry = * unsafe { inner. indices . get_unchecked ( index_index) } ;
544+ match index_entry {
544545 IndexEntry :: DUMMY => {
545- if freeslot . is_none ( ) {
546- freeslot = Some ( index_index) ;
546+ if free_slot . is_none ( ) {
547+ free_slot = Some ( index_index) ;
547548 }
548549 }
549550 IndexEntry :: FREE => {
550- let idxs = match freeslot {
551+ let idxs = match free_slot {
551552 Some ( free) => ( IndexEntry :: DUMMY , free) ,
552553 None => ( IndexEntry :: FREE , index_index) ,
553554 } ;
554555 return Ok ( idxs) ;
555556 }
556557 idx => {
557- let i = idx. index ( ) . unwrap_or_else ( || unsafe {
558- // DUMMY and FREE is already checked above.
559- std:: hint:: unreachable_unchecked ( )
560- } ) ;
561- let entry = & inner. entries [ i] . as_ref ( ) . unwrap ( ) ;
558+ let entry = unsafe {
559+ // DUMMY and FREE are already handled above.
560+ // i is always valid and entry always exists.
561+ let i = idx. index ( ) . unwrap_unchecked ( ) ;
562+ inner. entries . get_unchecked ( i) . as_ref ( ) . unwrap_unchecked ( )
563+ } ;
562564 let ret = ( idx, index_index) ;
563565 if key. key_is ( & entry. key ) {
564566 break ' outer ret;
@@ -617,7 +619,7 @@ impl<T: Clone> Dict<T> {
617619 // The dict was changed since we did lookup. Let's try again.
618620 _ => return Ok ( ControlFlow :: Continue ( ( ) ) ) ,
619621 }
620- inner. indices [ index_index] = IndexEntry :: DUMMY ;
622+ * unsafe { inner. indices . get_unchecked_mut ( index_index) } = IndexEntry :: DUMMY ;
621623 inner. used -= 1 ;
622624 let removed = slot. take ( ) ;
623625 Ok ( ControlFlow :: Break ( removed) )
@@ -645,7 +647,7 @@ impl<T: Clone> Dict<T> {
645647 }
646648 } ;
647649 inner. used -= 1 ;
648- inner. indices [ entry. index ] = IndexEntry :: DUMMY ;
650+ * unsafe { inner. indices . get_unchecked_mut ( entry. index ) } = IndexEntry :: DUMMY ;
649651 Some ( ( entry. key , entry. value ) )
650652 }
651653
0 commit comments