@@ -12,35 +12,7 @@ using namespace std::chrono_literals;
1212using msgpackcpp::serialize;
1313using msgpackcpp::deserialize;
1414using msgpackcpp::sink;
15-
16- template <class T , class Generator >
17- T random (Generator& gen)
18- {
19- if constexpr (std::is_same_v<T, char > || std::is_same_v<T, int8_t > || std::is_same_v<T, uint8_t >)
20- return std::uniform_int_distribution<int >{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
21- else if constexpr (std::is_integral_v<T>)
22- return std::uniform_int_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
23- else if constexpr (std::is_floating_point_v<T>)
24- return std::uniform_real_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
25- }
26-
27- std::string make_string ()
28- {
29- return " and talk gravely to each"
30- " other; he read of the Obelisk in the Place de la Concorde that weeps"
31- " tears of granite in its lonely sunless exile and longs to be back by"
32- " the hot, lotus-covered Nile, where there are Sphinxes, and rose-red"
33- " ibises, and white vultures with gilded claws, and crocodiles with small"
34- " beryl eyes that crawl over the green steaming mud; he began to brood"
35- " over those verses which, drawing music from kiss-stained marble, tell"
36- " of that curious statue that Gautier compares to a contralto voice, the"
37- " _monstre charmant_ that couches in the porphyry-room of the Louvre."
38- " But after a time the book fell from his hand. He grew nervous, and a"
39- " horrible fit of terror came over him. What if Alan Campbell should be"
40- " out of England? Days would elapse before he could come back. Perhaps he"
41- " might refuse to come. What could he do then? Every moment was of vital"
42- " importance." ;
43- }
15+ using msgpackcpp::source;
4416
4517template <class Byte , class Allocator >
4618struct vector_sink
@@ -50,32 +22,6 @@ struct vector_sink
5022 void write (const char * data, size_t len) {buf.insert (end (buf), data, data + len);}
5123};
5224
53- template <
54- class T ,
55- class D1 = boost::describe::describe_members<T, boost::describe::mod_any_access>
56- >
57- void serialize_msgpack_cxx (std::vector<uint8_t >& buf, const T& obj, bool as_map = false )
58- {
59- vector_sink out{buf};
60- msgpack::packer pack{&out};
61-
62- if (as_map)
63- {
64- pack.pack_map (boost::mp11::mp_size<D1>::value);
65- boost::mp11::mp_for_each<D1>([&](auto D) {
66- pack.pack (D.name );
67- pack.pack (obj.*D.pointer );
68- });
69- }
70- else
71- {
72- pack.pack_array (boost::mp11::mp_size<D1>::value);
73- boost::mp11::mp_for_each<D1>([&](auto D) {
74- pack.pack (obj.*D.pointer );
75- });
76- }
77- }
78-
7925namespace custom_namespace
8026{
8127 struct custom_struct
@@ -92,60 +38,115 @@ namespace custom_namespace
9238 float f32 ;
9339 double f64 ;
9440 std::string str;
41+
42+ MSGPACK_DEFINE (c, i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , f32 , f64 , str)
9543 };
9644
97- BOOST_DESCRIBE_STRUCT (custom_struct, (), (
98- c, i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , f32 , f64 ,
99- str
100- ))
45+ BOOST_DESCRIBE_STRUCT (custom_struct, (), (c, i8 , u8 , i16 , u16 , i32 , u32 , i64 , u64 , f32 , f64 , str))
10146
10247 struct custom_struct2
10348 {
10449 std::vector<custom_struct> array;
10550 std::map<int , custom_struct> map;
10651 std::vector<uint8_t > binary;
52+ MSGPACK_DEFINE (array, map, binary)
10753 };
10854
10955 BOOST_DESCRIBE_STRUCT (custom_struct2, (), (array, map, binary))
11056}
11157
58+ std::string make_string ()
59+ {
60+ return " and talk gravely to each"
61+ " other; he read of the Obelisk in the Place de la Concorde that weeps"
62+ " tears of granite in its lonely sunless exile and longs to be back by"
63+ " the hot, lotus-covered Nile, where there are Sphinxes, and rose-red"
64+ " ibises, and white vultures with gilded claws, and crocodiles with small"
65+ " beryl eyes that crawl over the green steaming mud; he began to brood"
66+ " over those verses which, drawing music from kiss-stained marble, tell"
67+ " of that curious statue that Gautier compares to a contralto voice, the"
68+ " _monstre charmant_ that couches in the porphyry-room of the Louvre."
69+ " But after a time the book fell from his hand. He grew nervous, and a"
70+ " horrible fit of terror came over him. What if Alan Campbell should be"
71+ " out of England? Days would elapse before he could come back. Perhaps he"
72+ " might refuse to come. What could he do then? Every moment was of vital"
73+ " importance." ;
74+ }
75+
76+ template <class T >
77+ void random (T& obj, std::mt19937_64& gen)
78+ {
79+ if constexpr (std::is_same_v<T, char > || std::is_same_v<T, int8_t > || std::is_same_v<T, uint8_t >)
80+ obj = std::uniform_int_distribution<int >{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
81+ else if constexpr (std::is_integral_v<T>)
82+ obj = std::uniform_int_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
83+ else if constexpr (std::is_floating_point_v<T>)
84+ obj = std::uniform_real_distribution<T>{std::numeric_limits<T>::min (), std::numeric_limits<T>::max ()}(gen);
85+ }
86+
87+ void random (custom_namespace::custom_struct& data, std::mt19937_64& eng)
88+ {
89+ random (data.c , eng);
90+ random (data.i8 , eng);
91+ random (data.u8 , eng);
92+ random (data.i16 , eng);
93+ random (data.u16 , eng);
94+ random (data.i32 , eng);
95+ random (data.u32 , eng);
96+ random (data.i64 , eng);
97+ random (data.u64 , eng);
98+ random (data.f32 , eng);
99+ random (data.f64 , eng);
100+ data.str = make_string ();
101+ }
102+
112103int main ()
113104{
114105 std::mt19937_64 eng (std::chrono::high_resolution_clock::now ().time_since_epoch ().count ());
115106
116- custom_namespace::custom_struct data;
117- data.c = random<char >(eng);
118- data.i8 = random<int8_t >(eng);
119- data.u8 = random<uint8_t >(eng);
120- data.i16 = random<int16_t >(eng);
121- data.u16 = random<uint16_t >(eng);
122- data.i32 = random<int32_t >(eng);
123- data.u32 = random<uint32_t >(eng);
124- data.i64 = random<int64_t >(eng);
125- data.u64 = random<uint64_t >(eng);
126- data.f32 = random<float >(eng);
127- data.f64 = random<double >(eng);
128- data.str = make_string ();
107+ custom_namespace::custom_struct2 data;
108+ data.array .resize (10 );
109+ for (auto & obj : data.array ) random (obj, eng);
110+ random (data.map [0 ], eng);
111+ random (data.map [-42 ], eng);
112+ random (data.map [100000 ], eng);
113+ data.binary .resize (1024 );
114+ std::generate (begin (data.binary ), end (data.binary ), [&]{uint8_t v; random (v, eng); return v;});
129115
130116 // Warmup
131117 std::vector<uint8_t > buf0;
132118 auto out = sink (buf0);
133119 serialize (out, data);
134120
135121 std::vector<uint8_t > buf1;
136- serialize_msgpack_cxx (buf1, data);
122+ vector_sink out1 (buf1);
123+ msgpack::pack (out1, data);
137124 printf (" buf0.size() %zu buf1.size() %zu\n " , buf0.size (), buf1.size ());
138125
139- ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (50 ).run (" msgpackcpp::serialize" , [&] {
126+ ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (20 ).run (" msgpackcpp::deserialize" , [&] {
127+ custom_namespace::custom_struct2 obj;
128+ auto in = source (buf0);
129+ deserialize (in, obj);
130+ // ankerl::nanobench::doNotOptimizeAway(d);
131+ });
132+
133+ ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (20 ).run (" msgpackcpp::serialize" , [&] {
140134 buf0.clear ();
141135 auto out = sink (buf0);
142136 serialize (out, data);
143137 // ankerl::nanobench::doNotOptimizeAway(d);
144138 });
145139
146- ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (50 ).run (" msgpack_c::serialize" , [&] {
140+ ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (20 ).run (" msgpack_c::deserialize" , [&] {
141+ msgpack::object_handle oh = msgpack::unpack ((const char *)buf1.data (), buf1.size ());
142+ custom_namespace::custom_struct2 obj = oh.get ().as <custom_namespace::custom_struct2>();
143+ // ankerl::nanobench::doNotOptimizeAway(d);
144+ });
145+
146+ ankerl::nanobench::Bench ().minEpochTime (100ms).epochs (20 ).run (" msgpack_c::serialize" , [&] {
147147 buf1.clear ();
148- serialize_msgpack_cxx (buf1, data);
148+ vector_sink out (buf1);
149+ msgpack::pack (out, data);
149150 // ankerl::nanobench::doNotOptimizeAway(d);
150151 });
151152}
0 commit comments