1- use super :: either_iter:: EitherIter ;
21use crate :: fx:: FxHashMap ;
32use arrayvec:: ArrayVec ;
3+ use itertools:: Either ;
44use std:: fmt;
55use std:: hash:: Hash ;
66use std:: ops:: Index ;
77
8- // For pointer-sized arguments arrays
9- // are faster than set/map for up to 64
10- // arguments.
11- //
12- // On the other hand such a big array
13- // hurts cache performance, makes passing
14- // sso structures around very expensive.
15- //
16- // Biggest performance benefit is gained
17- // for reasonably small arrays that stay
18- // small in vast majority of cases.
19- //
20- // '8' is chosen as a sane default, to be
21- // reevaluated later.
8+ /// For pointer-sized arguments arrays
9+ /// are faster than set/map for up to 64
10+ /// arguments.
11+ ///
12+ /// On the other hand such a big array
13+ /// hurts cache performance, makes passing
14+ /// sso structures around very expensive.
15+ ///
16+ /// Biggest performance benefit is gained
17+ /// for reasonably small arrays that stay
18+ /// small in vast majority of cases.
19+ ///
20+ /// '8' is chosen as a sane default, to be
21+ /// reevaluated later.
2222const SSO_ARRAY_SIZE : usize = 8 ;
2323
2424/// Small-storage-optimized implementation of a map.
@@ -138,35 +138,35 @@ impl<K, V> SsoHashMap<K, V> {
138138 /// The iterator element type is `&'a K`.
139139 pub fn keys ( & self ) -> impl Iterator < Item = & ' _ K > {
140140 match self {
141- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter ( ) . map ( |( k, _v) | k) ) ,
142- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. keys ( ) ) ,
141+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter ( ) . map ( |( k, _v) | k) ) ,
142+ SsoHashMap :: Map ( map) => Either :: Right ( map. keys ( ) ) ,
143143 }
144144 }
145145
146146 /// An iterator visiting all values in arbitrary order.
147147 /// The iterator element type is `&'a V`.
148148 pub fn values ( & self ) -> impl Iterator < Item = & ' _ V > {
149149 match self {
150- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter ( ) . map ( |( _k, v) | v) ) ,
151- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. values ( ) ) ,
150+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter ( ) . map ( |( _k, v) | v) ) ,
151+ SsoHashMap :: Map ( map) => Either :: Right ( map. values ( ) ) ,
152152 }
153153 }
154154
155155 /// An iterator visiting all values mutably in arbitrary order.
156156 /// The iterator element type is `&'a mut V`.
157157 pub fn values_mut ( & mut self ) -> impl Iterator < Item = & ' _ mut V > {
158158 match self {
159- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. iter_mut ( ) . map ( |( _k, v) | v) ) ,
160- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. values_mut ( ) ) ,
159+ SsoHashMap :: Array ( array) => Either :: Left ( array. iter_mut ( ) . map ( |( _k, v) | v) ) ,
160+ SsoHashMap :: Map ( map) => Either :: Right ( map. values_mut ( ) ) ,
161161 }
162162 }
163163
164164 /// Clears the map, returning all key-value pairs as an iterator. Keeps the
165165 /// allocated memory for reuse.
166166 pub fn drain ( & mut self ) -> impl Iterator < Item = ( K , V ) > + ' _ {
167167 match self {
168- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. drain ( ..) ) ,
169- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. drain ( ) ) ,
168+ SsoHashMap :: Array ( array) => Either :: Left ( array. drain ( ..) ) ,
169+ SsoHashMap :: Map ( map) => Either :: Right ( map. drain ( ) ) ,
170170 }
171171 }
172172}
@@ -406,16 +406,16 @@ where
406406}
407407
408408impl < K , V > IntoIterator for SsoHashMap < K , V > {
409- type IntoIter = EitherIter <
410- <ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
409+ type IntoIter = Either <
410+ <ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
411411 <FxHashMap < K , V > as IntoIterator >:: IntoIter ,
412412 > ;
413413 type Item = <Self :: IntoIter as Iterator >:: Item ;
414414
415415 fn into_iter ( self ) -> Self :: IntoIter {
416416 match self {
417- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) ) ,
418- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. into_iter ( ) ) ,
417+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) ) ,
418+ SsoHashMap :: Map ( map) => Either :: Right ( map. into_iter ( ) ) ,
419419 }
420420 }
421421}
@@ -435,9 +435,9 @@ fn adapt_array_mut_it<K, V>(pair: &mut (K, V)) -> (&K, &mut V) {
435435}
436436
437437impl < ' a , K , V > IntoIterator for & ' a SsoHashMap < K , V > {
438- type IntoIter = EitherIter <
438+ type IntoIter = Either <
439439 std:: iter:: Map <
440- <& ' a ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
440+ <& ' a ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
441441 fn ( & ' a ( K , V ) ) -> ( & ' a K , & ' a V ) ,
442442 > ,
443443 <& ' a FxHashMap < K , V > as IntoIterator >:: IntoIter ,
@@ -446,16 +446,16 @@ impl<'a, K, V> IntoIterator for &'a SsoHashMap<K, V> {
446446
447447 fn into_iter ( self ) -> Self :: IntoIter {
448448 match self {
449- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) . map ( adapt_array_ref_it) ) ,
450- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. iter ( ) ) ,
449+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) . map ( adapt_array_ref_it) ) ,
450+ SsoHashMap :: Map ( map) => Either :: Right ( map. iter ( ) ) ,
451451 }
452452 }
453453}
454454
455455impl < ' a , K , V > IntoIterator for & ' a mut SsoHashMap < K , V > {
456- type IntoIter = EitherIter <
456+ type IntoIter = Either <
457457 std:: iter:: Map <
458- <& ' a mut ArrayVec < ( K , V ) , 8 > as IntoIterator >:: IntoIter ,
458+ <& ' a mut ArrayVec < ( K , V ) , SSO_ARRAY_SIZE > as IntoIterator >:: IntoIter ,
459459 fn ( & ' a mut ( K , V ) ) -> ( & ' a K , & ' a mut V ) ,
460460 > ,
461461 <& ' a mut FxHashMap < K , V > as IntoIterator >:: IntoIter ,
@@ -464,8 +464,8 @@ impl<'a, K, V> IntoIterator for &'a mut SsoHashMap<K, V> {
464464
465465 fn into_iter ( self ) -> Self :: IntoIter {
466466 match self {
467- SsoHashMap :: Array ( array) => EitherIter :: Left ( array. into_iter ( ) . map ( adapt_array_mut_it) ) ,
468- SsoHashMap :: Map ( map) => EitherIter :: Right ( map. iter_mut ( ) ) ,
467+ SsoHashMap :: Array ( array) => Either :: Left ( array. into_iter ( ) . map ( adapt_array_mut_it) ) ,
468+ SsoHashMap :: Map ( map) => Either :: Right ( map. iter_mut ( ) ) ,
469469 }
470470 }
471471}
0 commit comments