@@ -111,16 +111,28 @@ fn criterion_benchmark(c: &mut Criterion) {
111111 } ) ;
112112}
113113
114- pub fn read_all_vs_count ( c : & mut Criterion ) {
114+ pub fn read_all_vs_count_vs_read_exact ( c : & mut Criterion ) {
115115 #[ derive( DekuRead , DekuWrite ) ]
116116 pub struct AllWrapper {
117117 #[ deku( read_all) ]
118118 pub data : Vec < u8 > ,
119119 }
120120
121121 #[ derive( DekuRead , DekuWrite ) ]
122- #[ deku( ctx = "len: usize" ) ]
123122 pub struct CountWrapper {
123+ #[ deku( count = "1500" ) ]
124+ pub data : Vec < u8 > ,
125+ }
126+
127+ #[ derive( DekuRead , DekuWrite ) ]
128+ pub struct CountNonSpecialize {
129+ #[ deku( count = "(1500/2)" ) ]
130+ pub data : Vec < u16 > ,
131+ }
132+
133+ #[ derive( DekuRead , DekuWrite ) ]
134+ #[ deku( ctx = "len: usize" ) ]
135+ pub struct CountFromCtxWrapper {
124136 #[ deku( count = "len" ) ]
125137 pub data : Vec < u8 > ,
126138 }
@@ -137,14 +149,34 @@ pub fn read_all_vs_count(c: &mut Criterion) {
137149 } )
138150 } ) ;
139151
140- c. bench_function ( "count" , |b| {
152+ c. bench_function ( "count_specialize" , |b| {
153+ b. iter ( || {
154+ let mut cursor = Cursor :: new ( [ 1u8 ; 1500 ] . as_ref ( ) ) ;
155+ let mut reader = Reader :: new ( & mut cursor) ;
156+ CountWrapper :: from_reader_with_ctx ( black_box ( & mut reader) , ( ) )
157+ } )
158+ } ) ;
159+
160+ c. bench_function ( "count_from_u8_specialize" , |b| {
161+ b. iter ( || {
162+ let mut cursor = Cursor :: new ( [ 1u8 ; 1500 ] . as_ref ( ) ) ;
163+ let mut reader = Reader :: new ( & mut cursor) ;
164+ CountWrapper :: from_reader_with_ctx ( black_box ( & mut reader) , ( ) )
165+ } )
166+ } ) ;
167+
168+ c. bench_function ( "count_no_specialize" , |b| {
141169 b. iter ( || {
142170 let mut cursor = Cursor :: new ( [ 1u8 ; 1500 ] . as_ref ( ) ) ;
143171 let mut reader = Reader :: new ( & mut cursor) ;
144- CountWrapper :: from_reader_with_ctx ( black_box ( & mut reader) , 1500 )
172+ CountNonSpecialize :: from_reader_with_ctx ( black_box ( & mut reader) , ( ) )
145173 } )
146174 } ) ;
147175}
148176
149- criterion_group ! ( benches, criterion_benchmark, read_all_vs_count) ;
177+ criterion_group ! (
178+ benches,
179+ criterion_benchmark,
180+ read_all_vs_count_vs_read_exact
181+ ) ;
150182criterion_main ! ( benches) ;
0 commit comments