Debian Codegraph @

third_party/upb/benchmarks/benchmark.cc

raw
1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above copyright
9 // notice, this list of conditions and the following disclaimer in the
10 // documentation and/or other materials provided with the distribution.
11 // * Neither the name of Google LLC nor the
12 // names of its contributors may be used to endorse or promote products
13 // derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25
26 #include <benchmark/benchmark.h>
27
28 #include <string.h>
29
30 #include "google/ads/googleads/v11/services/google_ads_service.upbdefs.h"
31 #include "google/protobuf/descriptor.pb.h"
32 #include "google/protobuf/dynamic_message.h"
33 #include "absl/container/flat_hash_set.h"
34 #include "benchmarks/descriptor.pb.h"
35 #include "benchmarks/descriptor.upb.h"
36 #include "benchmarks/descriptor.upbdefs.h"
37 #include "benchmarks/descriptor_sv.pb.h"
38 #include "upb/def.hpp"
39
40 upb_StringView descriptor = benchmarks_descriptor_proto_upbdefinit.descriptor;
41 namespace protobuf = ::google::protobuf;
42
43 /* A buffer big enough to parse descriptor.proto without going to heap. */
44 char buf[65535];
45
46 void CollectFileDescriptors(
47 const _upb_DefPool_Init* file,
48 std::vector<upb_StringView>& serialized_files,
49 absl::flat_hash_set<const _upb_DefPool_Init*>& seen) {
50 if (!seen.insert(file).second) return;
51 for (_upb_DefPool_Init** deps = file->deps; *deps; deps++) {
52 CollectFileDescriptors(*deps, serialized_files, seen);
53 }
54 serialized_files.push_back(file->descriptor);
55 }
56
57 static void BM_ArenaOneAlloc(benchmark::State& state) {
58 for (auto _ : state) {
59 upb_Arena* arena = upb_Arena_New();
60 upb_Arena_Malloc(arena, 1);
61 upb_Arena_Free(arena);
62 }
63 }
64 BENCHMARK(BM_ArenaOneAlloc);
65
66 static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) {
67 for (auto _ : state) {
68 upb_Arena* arena = upb_Arena_Init(buf, sizeof(buf), NULL);
69 upb_Arena_Malloc(arena, 1);
70 upb_Arena_Free(arena);
71 }
72 }
73 BENCHMARK(BM_ArenaInitialBlockOneAlloc);
74
75 enum LoadDescriptorMode {
76 NoLayout,
77 WithLayout,
78 };
79
80 // This function is mostly copied from upb/def.c, but it is modified to avoid
81 // passing in the pre-generated mini-tables, in order to force upb to compute
82 // them dynamically. Generally you would never want to do this, but we want to
83 // simulate the cost we would pay if we were loading these types purely from
84 // descriptors, with no mini-tales available.
85 bool LoadDefInit_BuildLayout(upb_DefPool* s, const _upb_DefPool_Init* init,
86 size_t* bytes) {
87 _upb_DefPool_Init** deps = init->deps;
88 google_protobuf_FileDescriptorProto* file;
89 upb_Arena* arena;
90 upb_Status status;
91
92 upb_Status_Clear(&status);
93
94 if (upb_DefPool_FindFileByName(s, init->filename)) {
95 return true;
96 }
97
98 arena = upb_Arena_New();
99
100 for (; *deps; deps++) {
101 if (!LoadDefInit_BuildLayout(s, *deps, bytes)) goto err;
102 }
103
104 file = google_protobuf_FileDescriptorProto_parse_ex(
105 init->descriptor.data, init->descriptor.size, NULL,
106 kUpb_DecodeOption_AliasString, arena);
107 *bytes += init->descriptor.size;
108
109 if (!file) {
110 upb_Status_SetErrorFormat(
111 &status,
112 "Failed to parse compiled-in descriptor for file '%s'. This should "
113 "never happen.",
114 init->filename);
115 goto err;
116 }
117
118 // KEY DIFFERENCE: Here we pass in only the descriptor, and not the
119 // pre-generated minitables.
120 if (!upb_DefPool_AddFile(s, file, &status)) {
121 goto err;
122 }
123
124 upb_Arena_Free(arena);
125 return true;
126
127 err:
128 fprintf(stderr,
129 "Error loading compiled-in descriptor for file '%s' (this should "
130 "never happen): %s\n",
131 init->filename, upb_Status_ErrorMessage(&status));
132 exit(1);
133 }
134
135 template <LoadDescriptorMode Mode>
136 static void BM_LoadAdsDescriptor_Upb(benchmark::State& state) {
137 size_t bytes_per_iter = 0;
138 for (auto _ : state) {
139 upb::DefPool defpool;
140 if (Mode == NoLayout) {
141 google_ads_googleads_v11_services_SearchGoogleAdsRequest_getmsgdef(
142 defpool.ptr());
143 bytes_per_iter = _upb_DefPool_BytesLoaded(defpool.ptr());
144 } else {
145 bytes_per_iter = 0;
146 LoadDefInit_BuildLayout(
147 defpool.ptr(),
148 &google_ads_googleads_v11_services_google_ads_service_proto_upbdefinit,
149 &bytes_per_iter);
150 }
151 }
152 state.SetBytesProcessed(state.iterations() * bytes_per_iter);
153 }
154 BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Upb, NoLayout);
155 BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Upb, WithLayout);
156
157 template <LoadDescriptorMode Mode>
158 static void BM_LoadAdsDescriptor_Proto2(benchmark::State& state) {
159 extern _upb_DefPool_Init
160 google_ads_googleads_v11_services_google_ads_service_proto_upbdefinit;
161 std::vector<upb_StringView> serialized_files;
162 absl::flat_hash_set<const _upb_DefPool_Init*> seen_files;
163 CollectFileDescriptors(
164 &google_ads_googleads_v11_services_google_ads_service_proto_upbdefinit,
165 serialized_files, seen_files);
166 size_t bytes_per_iter = 0;
167 for (auto _ : state) {
168 bytes_per_iter = 0;
169 protobuf::Arena arena;
170 protobuf::DescriptorPool pool;
171 for (auto file : serialized_files) {
172 protobuf::StringPiece input(file.data, file.size);
173 auto proto =
174 protobuf::Arena::CreateMessage<protobuf::FileDescriptorProto>(&arena);
175 bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input) &&
176 pool.BuildFile(*proto) != nullptr;
177 if (!ok) {
178 printf("Failed to add file.\n");
179 exit(1);
180 }
181 bytes_per_iter += input.size();
182 }
183
184 if (Mode == WithLayout) {
185 protobuf::DynamicMessageFactory factory;
186 const protobuf::Descriptor* d = pool.FindMessageTypeByName(
187 "google.ads.googleads.v11.services.SearchGoogleAdsResponse");
188 if (!d) {
189 printf("Failed to find descriptor.\n");
190 exit(1);
191 }
192 factory.GetPrototype(d);
193 }
194 }
195 state.SetBytesProcessed(state.iterations() * bytes_per_iter);
196 }
197 BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Proto2, NoLayout);
198 BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Proto2, WithLayout);
199
200 enum CopyStrings {
201 Copy,
202 Alias,
203 };
204
205 enum ArenaMode {
206 NoArena,
207 UseArena,
208 InitBlock,
209 };
210
211 template <ArenaMode AMode, CopyStrings Copy>
212 static void BM_Parse_Upb_FileDesc(benchmark::State& state) {
213 for (auto _ : state) {
214 upb_Arena* arena;
215 if (AMode == InitBlock) {
216 arena = upb_Arena_Init(buf, sizeof(buf), NULL);
217 } else {
218 arena = upb_Arena_New();
219 }
220 upb_benchmark_FileDescriptorProto* set =
221 upb_benchmark_FileDescriptorProto_parse_ex(
222 descriptor.data, descriptor.size, NULL,
223 Copy == Alias ? kUpb_DecodeOption_AliasString : 0, arena);
224 if (!set) {
225 printf("Failed to parse.\n");
226 exit(1);
227 }
228 upb_Arena_Free(arena);
229 }
230 state.SetBytesProcessed(state.iterations() * descriptor.size);
231 }
232 BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Copy);
233 BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Alias);
234 BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Copy);
235 BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Alias);
236
237 template <ArenaMode AMode, class P>
238 struct Proto2Factory;
239
240 template <class P>
241 struct Proto2Factory<NoArena, P> {
242 public:
243 P* GetProto() { return &proto_; }
244
245 private:
246 P proto_;
247 };
248
249 template <class P>
250 struct Proto2Factory<UseArena, P> {
251 public:
252 P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
253
254 private:
255 protobuf::Arena arena_;
256 };
257
258 template <class P>
259 struct Proto2Factory<InitBlock, P> {
260 public:
261 Proto2Factory() : arena_(GetOptions()) {}
262 P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
263
264 private:
265 protobuf::ArenaOptions GetOptions() {
266 protobuf::ArenaOptions opts;
267 opts.initial_block = buf;
268 opts.initial_block_size = sizeof(buf);
269 return opts;
270 }
271
272 protobuf::Arena arena_;
273 };
274
275 using FileDesc = ::upb_benchmark::FileDescriptorProto;
276 using FileDescSV = ::upb_benchmark::sv::FileDescriptorProto;
277
278 template <class P, ArenaMode AMode, CopyStrings kCopy>
279 void BM_Parse_Proto2(benchmark::State& state) {
280 constexpr protobuf::MessageLite::ParseFlags kParseFlags =
281 kCopy == Copy
282 ? protobuf::MessageLite::ParseFlags::kMergePartial
283 : protobuf::MessageLite::ParseFlags::kMergePartialWithAliasing;
284 for (auto _ : state) {
285 Proto2Factory<AMode, P> proto_factory;
286 auto proto = proto_factory.GetProto();
287 protobuf::StringPiece input(descriptor.data, descriptor.size);
288 bool ok = proto->template ParseFrom<kParseFlags>(input);
289 if (!ok) {
290 printf("Failed to parse.\n");
291 exit(1);
292 }
293 }
294 state.SetBytesProcessed(state.iterations() * descriptor.size);
295 }
296 BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, NoArena, Copy);
297 BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, UseArena, Copy);
298 BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, InitBlock, Copy);
299 BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescSV, InitBlock, Alias);
300
301 static void BM_SerializeDescriptor_Proto2(benchmark::State& state) {
302 upb_benchmark::FileDescriptorProto proto;
303 proto.ParseFromArray(descriptor.data, descriptor.size);
304 for (auto _ : state) {
305 proto.SerializePartialToArray(buf, sizeof(buf));
306 }
307 state.SetBytesProcessed(state.iterations() * descriptor.size);
308 }
309 BENCHMARK(BM_SerializeDescriptor_Proto2);
310
311 static void BM_SerializeDescriptor_Upb(benchmark::State& state) {
312 int64_t total = 0;
313 upb_Arena* arena = upb_Arena_New();
314 upb_benchmark_FileDescriptorProto* set =
315 upb_benchmark_FileDescriptorProto_parse(descriptor.data, descriptor.size,
316 arena);
317 if (!set) {
318 printf("Failed to parse.\n");
319 exit(1);
320 }
321 for (auto _ : state) {
322 upb_Arena* enc_arena = upb_Arena_Init(buf, sizeof(buf), NULL);
323 size_t size;
324 char* data =
325 upb_benchmark_FileDescriptorProto_serialize(set, enc_arena, &size);
326 if (!data) {
327 printf("Failed to serialize.\n");
328 exit(1);
329 }
330 total += size;
331 }
332 state.SetBytesProcessed(total);
333 }
334 BENCHMARK(BM_SerializeDescriptor_Upb);