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