@@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize};
10
10
use std:: hint:: black_box;
11
11
use test:: Bencher ;
12
12
13
- #[ derive( Serialize , Deserialize ) ]
13
+ #[ derive( Serialize , Deserialize , bincode :: Encode , bincode :: Decode ) ]
14
14
struct Foo {
15
15
bar : String ,
16
16
baz : u64 ,
@@ -28,25 +28,49 @@ impl Default for Foo {
28
28
}
29
29
30
30
#[ bench]
31
- fn bincode_deserialize ( b : & mut Bencher ) {
31
+ fn bincode_serde_deserialize ( b : & mut Bencher ) {
32
32
let foo = Foo :: default ( ) ;
33
- let bytes = bincode:: serialize ( & foo) . unwrap ( ) ;
33
+ let bytes = bincode:: serde :: encode_to_vec ( & foo, bincode :: config :: standard ( ) ) . unwrap ( ) ;
34
34
35
35
b. iter ( || {
36
36
let bytes = black_box ( & bytes) ;
37
- bincode:: deserialize :: < Foo > ( bytes) . unwrap ( )
37
+ bincode:: serde :: decode_from_slice :: < Foo , _ > ( bytes, bincode :: config :: standard ( ) ) . unwrap ( )
38
38
} ) ;
39
39
}
40
40
41
41
#[ bench]
42
- fn bincode_serialize ( b : & mut Bencher ) {
42
+ fn bincode_serde_serialize ( b : & mut Bencher ) {
43
43
let foo = Foo :: default ( ) ;
44
44
let mut bytes = Vec :: with_capacity ( 128 ) ;
45
45
46
46
b. iter ( || {
47
47
let foo = black_box ( & foo) ;
48
48
bytes. clear ( ) ;
49
- bincode:: serialize_into ( & mut bytes, foo) . unwrap ( ) ;
49
+ bincode:: serde:: encode_into_std_write ( foo, & mut bytes, bincode:: config:: standard ( ) )
50
+ . unwrap ( ) ;
51
+ } ) ;
52
+ }
53
+
54
+ #[ bench]
55
+ fn bincode_decode ( b : & mut Bencher ) {
56
+ let foo = Foo :: default ( ) ;
57
+ let bytes = bincode:: encode_to_vec ( & foo, bincode:: config:: standard ( ) ) . unwrap ( ) ;
58
+
59
+ b. iter ( || {
60
+ let bytes = black_box ( & bytes) ;
61
+ bincode:: decode_from_slice :: < Foo , _ > ( bytes, bincode:: config:: standard ( ) ) . unwrap ( )
62
+ } ) ;
63
+ }
64
+
65
+ #[ bench]
66
+ fn bincode_encode ( b : & mut Bencher ) {
67
+ let foo = Foo :: default ( ) ;
68
+ let mut bytes = Vec :: with_capacity ( 128 ) ;
69
+
70
+ b. iter ( || {
71
+ let foo = black_box ( & foo) ;
72
+ bytes. clear ( ) ;
73
+ bincode:: encode_into_std_write ( foo, & mut bytes, bincode:: config:: standard ( ) ) . unwrap ( ) ;
50
74
} ) ;
51
75
}
52
76
0 commit comments