@@ -11,18 +11,18 @@ extern crate alloc;
1111#[ cfg( feature = "std" ) ]
1212extern crate std;
1313
14- use alloc:: boxed:: Box ;
1514#[ cfg( not( feature = "std" ) ) ]
16- use alloc:: collections:: BTreeSet as AllocSet ;
15+ use alloc:: collections:: BTreeSet ;
1716use alloc:: string:: String ;
17+ use alloc:: vec;
1818use alloc:: vec:: Vec ;
1919use core:: cmp:: Ordering ;
2020use core:: convert:: TryFrom ;
2121use core:: fmt;
2222use core:: iter;
2323use core:: ops:: BitXorAssign ;
2424#[ cfg( feature = "std" ) ]
25- use std:: collections:: HashSet as AllocSet ;
25+ use std:: collections:: HashSet ;
2626
2727mod hex;
2828
@@ -380,7 +380,11 @@ impl Negentropy {
380380 }
381381 Mode :: IdList => {
382382 let num_ids: u64 = self . decode_var_int ( & mut query) ?;
383- let mut their_elems: AllocSet < Vec < u8 > > = AllocSet :: new ( ) ;
383+ #[ cfg( feature = "std" ) ]
384+ let mut their_elems: HashSet < Vec < u8 > > =
385+ HashSet :: with_capacity ( num_ids as usize ) ;
386+ #[ cfg( not( feature = "std" ) ) ]
387+ let mut their_elems: BTreeSet < Vec < u8 > > = BTreeSet :: new ( ) ;
384388
385389 for _ in 0 ..num_ids {
386390 let e: Vec < u8 > = self . get_bytes ( & mut query, self . id_size ) ?;
@@ -403,10 +407,10 @@ impl Negentropy {
403407 need_ids. push ( hex:: encode ( k) ?) ;
404408 }
405409 } else {
406- let mut response_have_ids: Vec < & [ u8 ] > = Vec :: new ( ) ;
407- let mut it = lower;
408- let mut did_split = false ;
409- let mut split_bound = XorElem :: new ( ) ;
410+ let mut response_have_ids: Vec < & [ u8 ] > = Vec :: with_capacity ( 100 ) ;
411+ let mut it: usize = lower;
412+ let mut did_split: bool = false ;
413+ let mut split_bound: XorElem = XorElem :: new ( ) ;
410414
411415 while it < upper {
412416 let k: & [ u8 ] = self . items [ it] . get_id ( ) ;
@@ -466,9 +470,10 @@ impl Negentropy {
466470 curr_bound : & XorElem ,
467471 response_have_ids : & mut Vec < & [ u8 ] > ,
468472 ) -> Result < ( ) , Error > {
469- let mut payload: Vec < u8 > = Vec :: new ( ) ;
473+ let len: usize = response_have_ids. len ( ) ;
474+ let mut payload: Vec < u8 > = Vec :: with_capacity ( 10 + 10 + len) ;
470475 payload. extend ( self . encode_mode ( Mode :: IdList ) ) ;
471- payload. extend ( self . encode_var_int ( response_have_ids . len ( ) as u64 ) ) ;
476+ payload. extend ( self . encode_var_int ( len as u64 ) ) ;
472477
473478 for id in response_have_ids. iter ( ) {
474479 payload. extend_from_slice ( id) ;
@@ -504,7 +509,7 @@ impl Negentropy {
504509 let num_elems: usize = items. len ( ) ;
505510
506511 if num_elems < DOUBLE_BUCKETS {
507- let mut payload: Vec < u8 > = Vec :: new ( ) ;
512+ let mut payload: Vec < u8 > = Vec :: with_capacity ( 10 + 10 + num_elems ) ;
508513 payload. extend ( self . encode_mode ( Mode :: IdList ) ) ;
509514 payload. extend ( self . encode_var_int ( num_elems as u64 ) ) ;
510515
@@ -520,25 +525,25 @@ impl Negentropy {
520525 } else {
521526 let items_per_bucket: usize = num_elems / BUCKETS ;
522527 let buckets_with_extra: usize = num_elems % BUCKETS ;
523- let lower: XorElem = items. first ( ) . cloned ( ) . unwrap_or_default ( ) ;
528+ let lower: XorElem = items. first ( ) . copied ( ) . unwrap_or_default ( ) ;
524529 let mut prev_bound: XorElem = lower;
525- let bucket_end = items. iter ( ) . take ( items_per_bucket) ;
530+ let bucket_end = items. iter ( ) . copied ( ) . take ( items_per_bucket) ;
526531
527532 for i in 0 ..BUCKETS {
528533 let mut our_xor_set: XorElem = XorElem :: new ( ) ;
529534
530535 let bucket_end = bucket_end. clone ( ) ;
531536 if i < buckets_with_extra {
532- for elem in bucket_end. chain ( iter:: once ( & lower) ) {
533- our_xor_set ^= * elem;
537+ for elem in bucket_end. chain ( iter:: once ( lower) ) {
538+ our_xor_set ^= elem;
534539 }
535540 } else {
536541 for elem in bucket_end {
537- our_xor_set ^= * elem;
542+ our_xor_set ^= elem;
538543 }
539544 } ;
540545
541- let mut payload: Vec < u8 > = Vec :: new ( ) ;
546+ let mut payload: Vec < u8 > = Vec :: with_capacity ( 10 + self . id_size as usize ) ;
542547 payload. extend ( self . encode_mode ( Mode :: Fingerprint ) ) ;
543548 payload. extend ( our_xor_set. get_id_subsize ( self . id_size ) ) ;
544549
@@ -668,30 +673,26 @@ impl Negentropy {
668673 XorElem :: with_timestamp_and_id ( timestamp, id)
669674 }
670675
671- fn encode_mode ( & self , mode : Mode ) -> Box < dyn Iterator < Item = u8 > + ' _ > {
676+ fn encode_mode ( & self , mode : Mode ) -> Vec < u8 > {
672677 self . encode_var_int ( mode. as_u64 ( ) )
673678 }
674679
675- fn encode_var_int ( & self , mut n : u64 ) -> Box < dyn Iterator < Item = u8 > + ' _ > {
680+ fn encode_var_int ( & self , mut n : u64 ) -> Vec < u8 > {
676681 if n == 0 {
677- return Box :: new ( iter :: once ( 0 ) ) ;
682+ return vec ! [ 0 ] ;
678683 }
679684
680- let mut o: Vec < u8 > = Vec :: new ( ) ;
685+ let mut o: Vec < u8 > = Vec :: with_capacity ( 10 ) ;
681686
682687 while n > 0 {
683688 o. push ( ( n & 0x7F ) as u8 ) ;
684689 n >>= 7 ;
685690 }
686691
687- Box :: new ( o . into_iter ( ) . rev ( ) )
692+ o
688693 }
689694
690- fn encode_timestamp_out (
691- & self ,
692- timestamp : u64 ,
693- last_timestamp_out : & mut u64 ,
694- ) -> Box < dyn Iterator < Item = u8 > + ' _ > {
695+ fn encode_timestamp_out ( & self , timestamp : u64 , last_timestamp_out : & mut u64 ) -> Vec < u8 > {
695696 if timestamp == MAX_U64 {
696697 * last_timestamp_out = MAX_U64 ;
697698 return self . encode_var_int ( 0 ) ;
0 commit comments