@@ -303,6 +303,8 @@ fn find_publisher_index(comps: &[PriceComponent], key: &Pubkey) -> Option<usize>
303303 let mut right = comps. len ( ) ;
304304 while left < right {
305305 let mid = left + ( right - left) / 2 ;
306+ // sol_memcmp is much faster than rust default comparison of Pubkey. It costs
307+ // 10CU whereas rust default comparison costs a few times more.
306308 match sol_memcmp ( comps[ mid] . pub_ . as_ref ( ) , key. as_ref ( ) , 32 ) {
307309 i if i < 0 => {
308310 left = mid + 1 ;
@@ -362,7 +364,12 @@ mod test {
362364 assert_eq ! ( find_publisher_index( & comps, & comp. pub_) , Some ( idx) ) ;
363365 } ) ;
364366
365- assert_eq ! ( find_publisher_index( & comps, & Pubkey :: new_unique( ) ) , None ) ;
367+ let mut key_not_in_list = Pubkey :: new_unique ( ) ;
368+ while comps. iter ( ) . any ( |comp| comp. pub_ == key_not_in_list) {
369+ key_not_in_list = Pubkey :: new_unique ( ) ;
370+ }
371+
372+ assert_eq ! ( find_publisher_index( & comps, & key_not_in_list) , None ) ;
366373 }
367374
368375 /// Test the find_publisher_index method works with a sorted list of components.
@@ -374,6 +381,11 @@ mod test {
374381 assert_eq ! ( find_publisher_index( & comps, & comp. pub_) , Some ( idx) ) ;
375382 } ) ;
376383
377- assert_eq ! ( find_publisher_index( & comps, & Pubkey :: new_unique( ) ) , None ) ;
384+ let mut key_not_in_list = Pubkey :: new_unique ( ) ;
385+ while comps. iter ( ) . any ( |comp| comp. pub_ == key_not_in_list) {
386+ key_not_in_list = Pubkey :: new_unique ( ) ;
387+ }
388+
389+ assert_eq ! ( find_publisher_index( & comps, & key_not_in_list) , None ) ;
378390 }
379391}
0 commit comments