@@ -4,9 +4,10 @@ use commonware_utils::{NZUsize, NZU16};
44use criterion:: { criterion_main, BatchSize , Criterion } ;
55use rand:: { RngCore , SeedableRng as _} ;
66use rand_chacha:: ChaCha8Rng ;
7+ use shard_selection:: SELECTIONS ;
78
8- mod no_coding;
99mod reed_solomon;
10+ mod shard_selection;
1011mod zoda;
1112
1213pub ( crate ) fn bench_encode_generic < S : Scheme > ( name : & str , c : & mut Criterion ) {
@@ -48,52 +49,6 @@ pub(crate) fn bench_encode_generic<S: Scheme>(name: &str, c: &mut Criterion) {
4849 }
4950}
5051
51- #[ derive( Clone , Copy ) ]
52- pub ( crate ) enum ShardSelection {
53- Best ,
54- Exception ,
55- Worst ,
56- Interleaved ,
57- }
58-
59- impl ShardSelection {
60- const fn label ( self ) -> & ' static str {
61- match self {
62- Self :: Best => "best" ,
63- Self :: Exception => "exception" ,
64- Self :: Worst => "worst" ,
65- Self :: Interleaved => "interleaved" ,
66- }
67- }
68-
69- fn indices ( self , min : u16 ) -> Vec < u16 > {
70- match self {
71- Self :: Best => ( 0 ..min) . collect ( ) ,
72- Self :: Exception => ( 1 ..=min) . collect ( ) ,
73- Self :: Worst => ( min..min + min) . collect ( ) ,
74- Self :: Interleaved => ( 0 ..min)
75- . map ( |i| {
76- let k = i / 2 ;
77- // Alternate between original shard indices [0, min) and
78- // recovery shard indices [min, 2 * min).
79- if i % 2 == 0 {
80- k
81- } else {
82- min + k
83- }
84- } )
85- . collect ( ) ,
86- }
87- }
88- }
89-
90- const SELECTIONS : [ ShardSelection ; 4 ] = [
91- ShardSelection :: Best ,
92- ShardSelection :: Exception ,
93- ShardSelection :: Worst ,
94- ShardSelection :: Interleaved ,
95- ] ;
96-
9752pub ( crate ) fn bench_decode_generic < S : Scheme > ( name : & str , c : & mut Criterion ) {
9853 let mut rng = ChaCha8Rng :: seed_from_u64 ( 0 ) ;
9954 let cases = [ 20 , 22 , 23 ] . map ( |i| 2usize . pow ( i) ) ;
@@ -120,74 +75,44 @@ pub(crate) fn bench_decode_generic<S: Scheme>(name: &str, c: &mut Criterion) {
12075 rng. fill_bytes ( & mut data) ;
12176
12277 // Encode data
123- let ( commitment, mut shards) = if conc > 1 {
78+ let ( commitment, shards) = if conc > 1 {
12479 S :: encode ( & config, data. as_slice ( ) , & strategy) . unwrap ( )
12580 } else {
12681 S :: encode ( & config, data. as_slice ( ) , & Sequential ) . unwrap ( )
12782 } ;
12883
129- // Get my shard
130- let my_shard = shards. pop ( ) . unwrap ( ) ;
13184 let indices = selection. indices ( min) ;
132- let mut opt_shards: Vec < Option < _ > > =
133- shards. into_iter ( ) . map ( Some ) . collect ( ) ;
134- let weak_shards: Vec < ( u16 , _ ) > = indices
85+ let selected_shards: Vec < ( u16 , _ ) > = indices
13586 . iter ( )
13687 . map ( |& i| {
137- let shard =
138- opt_shards[ i as usize ] . take ( ) . unwrap ( ) ;
139- let ( _, _, weak_shard) =
140- S :: weaken ( & config, & commitment, i, shard)
141- . unwrap ( ) ;
142- ( i, weak_shard)
88+ ( i, shards[ i as usize ] . clone ( ) )
14389 } )
14490 . collect ( ) ;
14591
146- ( commitment, my_shard, weak_shards)
147- } ,
148- |( commitment, my_shard, weak_shards) | {
149- // Weaken my shard
150- let ( checking_data, _, _) = S :: weaken (
151- & config,
152- & commitment,
153- config. minimum_shards . get ( )
154- + config. extra_shards . get ( )
155- - 1 ,
156- my_shard,
157- )
158- . unwrap ( ) ;
159-
160- // Check shards
161- let checked_shards = weak_shards
162- . into_iter ( )
163- . map ( |( idx, weak_shard) | {
164- S :: check (
165- & config,
166- & commitment,
167- & checking_data,
168- idx,
169- weak_shard,
170- )
171- . unwrap ( )
92+ let checked_shards: Vec < _ > = selected_shards
93+ . iter ( )
94+ . map ( |( idx, shard) | {
95+ S :: check ( & config, & commitment, * idx, shard) . unwrap ( )
17296 } )
173- . collect :: < Vec < _ > > ( ) ;
97+ . collect ( ) ;
17498
99+ ( commitment, checked_shards)
100+ } ,
101+ |( commitment, checked_shards) | {
175102 // Decode data
176103 if conc > 1 {
177104 S :: decode (
178105 & config,
179106 & commitment,
180- checking_data,
181- & checked_shards,
107+ checked_shards. iter ( ) ,
182108 & strategy,
183109 )
184- . unwrap ( )
110+ . unwrap ( )
185111 } else {
186112 S :: decode (
187113 & config,
188114 & commitment,
189- checking_data,
190- & checked_shards,
115+ checked_shards. iter ( ) ,
191116 & Sequential ,
192117 )
193118 . unwrap ( )
@@ -203,4 +128,4 @@ pub(crate) fn bench_decode_generic<S: Scheme>(name: &str, c: &mut Criterion) {
203128 }
204129}
205130
206- criterion_main ! ( reed_solomon:: benches, no_coding :: benches , zoda:: benches) ;
131+ criterion_main ! ( reed_solomon:: benches, zoda:: benches) ;
0 commit comments