upb/benchmarks/benchmark.cc
Go to the documentation of this file.
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 #include <string.h>
28 
29 #include "absl/container/flat_hash_set.h"
30 #include "benchmarks/descriptor.pb.h"
31 #include "benchmarks/descriptor.upb.h"
32 #include "benchmarks/descriptor.upbdefs.h"
33 #include "benchmarks/descriptor_sv.pb.h"
34 #include "google/ads/googleads/v7/services/google_ads_service.upbdefs.h"
35 #include "google/protobuf/descriptor.pb.h"
36 #include "google/protobuf/dynamic_message.h"
37 #include "upb/def.hpp"
38 
39 upb_StringView descriptor = benchmarks_descriptor_proto_upbdefinit.descriptor;
40 namespace protobuf = ::google::protobuf;
41 
42 /* A buffer big enough to parse descriptor.proto without going to heap. */
43 char buf[65535];
44 
46  const _upb_DefPool_Init* file,
47  std::vector<upb_StringView>& serialized_files,
49  if (!seen.insert(file).second) return;
50  for (_upb_DefPool_Init** deps = file->deps; *deps; deps++) {
51  CollectFileDescriptors(*deps, serialized_files, seen);
52  }
53  serialized_files.push_back(file->descriptor);
54 }
55 
57  for (auto _ : state) {
61  }
62 }
64 
66  for (auto _ : state) {
67  upb_Arena* arena = upb_Arena_Init(buf, sizeof(buf), NULL);
70  }
71 }
73 
77 };
78 
79 // This function is mostly copied from upb/def.c, but it is modified to avoid
80 // passing in the pre-generated mini-tables, in order to force upb to compute
81 // them dynamically. Generally you would never want to do this, but we want to
82 // simulate the cost we would pay if we were loading these types purely from
83 // descriptors, with no mini-tales available.
85  size_t* bytes) {
86  _upb_DefPool_Init** deps = init->deps;
90 
92 
93  if (upb_DefPool_FindFileByName(s, init->filename)) {
94  return true;
95  }
96 
97  arena = upb_Arena_New();
98 
99  for (; *deps; deps++) {
100  if (!LoadDefInit_BuildLayout(s, *deps, bytes)) goto err;
101  }
102 
104  init->descriptor.data, init->descriptor.size, NULL,
106  *bytes += init->descriptor.size;
107 
108  if (!file) {
110  &status,
111  "Failed to parse compiled-in descriptor for file '%s'. This should "
112  "never happen.",
113  init->filename);
114  goto err;
115  }
116 
117  // KEY DIFFERENCE: Here we pass in only the descriptor, and not the
118  // pre-generated minitables.
119  if (!upb_DefPool_AddFile(s, file, &status)) {
120  goto err;
121  }
122 
124  return true;
125 
126 err:
127  fprintf(stderr,
128  "Error loading compiled-in descriptor for file '%s' (this should "
129  "never happen): %s\n",
130  init->filename, upb_Status_ErrorMessage(&status));
131  exit(1);
132 }
133 
134 template <LoadDescriptorMode Mode>
136  size_t bytes_per_iter = 0;
137  for (auto _ : state) {
139  if (Mode == NoLayout) {
140  google_ads_googleads_v7_services_SearchGoogleAdsRequest_getmsgdef(
141  symtab.ptr());
142  bytes_per_iter = _upb_DefPool_BytesLoaded(symtab.ptr());
143  } else {
144  bytes_per_iter = 0;
146  symtab.ptr(),
147  &google_ads_googleads_v7_services_google_ads_service_proto_upbdefinit,
148  &bytes_per_iter);
149  }
150  }
151  state.SetBytesProcessed(state.iterations() * bytes_per_iter);
152 }
155 
156 template <LoadDescriptorMode Mode>
158  extern _upb_DefPool_Init
159  google_ads_googleads_v7_services_google_ads_service_proto_upbdefinit;
160  std::vector<upb_StringView> serialized_files;
163  &google_ads_googleads_v7_services_google_ads_service_proto_upbdefinit,
164  serialized_files, seen_files);
165  size_t bytes_per_iter = 0;
166  for (auto _ : state) {
167  bytes_per_iter = 0;
170  for (auto file : serialized_files) {
172  auto proto =
173  protobuf::Arena::CreateMessage<protobuf::FileDescriptorProto>(&arena);
174  bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input) &&
175  pool.BuildFile(*proto) != nullptr;
176  if (!ok) {
177  printf("Failed to add file.\n");
178  exit(1);
179  }
180  bytes_per_iter += input.size();
181  }
182 
183  if (Mode == WithLayout) {
185  const protobuf::Descriptor* d = pool.FindMessageTypeByName(
186  "google.ads.googleads.v7.services.SearchGoogleAdsResponse");
187  if (!d) {
188  printf("Failed to find descriptor.\n");
189  exit(1);
190  }
191  factory.GetPrototype(d);
192  }
193  }
194  state.SetBytesProcessed(state.iterations() * bytes_per_iter);
195 }
198 
202 };
203 
204 enum ArenaMode {
208 };
209 
210 template <ArenaMode AMode, CopyStrings Copy>
212  for (auto _ : state) {
213  upb_Arena* arena;
214  if (AMode == InitBlock) {
215  arena = upb_Arena_Init(buf, sizeof(buf), NULL);
216  } else {
217  arena = upb_Arena_New();
218  }
219  upb_benchmark_FileDescriptorProto* set =
220  upb_benchmark_FileDescriptorProto_parse_ex(
223  if (!set) {
224  printf("Failed to parse.\n");
225  exit(1);
226  }
228  }
229  state.SetBytesProcessed(state.iterations() * descriptor.size);
230 }
235 
236 template <ArenaMode AMode, class P>
238 
239 template <class P>
241  public:
242  P* GetProto() { return &proto_; }
243 
244  private:
246 };
247 
248 template <class P>
250  public:
251  P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
252 
253  private:
255 };
256 
257 template <class P>
259  public:
261  P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena_); }
262 
263  private:
266  opts.initial_block = buf;
267  opts.initial_block_size = sizeof(buf);
268  return opts;
269  }
270 
272 };
273 
276 
277 template <class P, ArenaMode AMode, CopyStrings kCopy>
279  constexpr protobuf::MessageLite::ParseFlags kParseFlags =
280  kCopy == Copy
281  ? protobuf::MessageLite::ParseFlags::kMergePartial
282  : protobuf::MessageLite::ParseFlags::kMergePartialWithAliasing;
283  for (auto _ : state) {
284  Proto2Factory<AMode, P> proto_factory;
285  auto proto = proto_factory.GetProto();
287  bool ok = proto->template ParseFrom<kParseFlags>(input);
288  if (!ok) {
289  printf("Failed to parse.\n");
290  exit(1);
291  }
292  }
293  state.SetBytesProcessed(state.iterations() * descriptor.size);
294 }
299 
302  proto.ParseFromArray(descriptor.data, descriptor.size);
303  for (auto _ : state) {
304  proto.SerializePartialToArray(buf, sizeof(buf));
305  }
306  state.SetBytesProcessed(state.iterations() * descriptor.size);
307 }
309 
311  int64_t total = 0;
313  upb_benchmark_FileDescriptorProto* set =
314  upb_benchmark_FileDescriptorProto_parse(descriptor.data, descriptor.size,
315  arena);
316  if (!set) {
317  printf("Failed to parse.\n");
318  exit(1);
319  }
320  for (auto _ : state) {
321  upb_Arena* enc_arena = upb_Arena_Init(buf, sizeof(buf), NULL);
322  size_t size;
323  char* data =
324  upb_benchmark_FileDescriptorProto_serialize(set, enc_arena, &size);
325  if (!data) {
326  printf("Failed to serialize.\n");
327  exit(1);
328  }
329  total += size;
330  }
331  state.SetBytesProcessed(total);
332 }
ArenaMode
ArenaMode
Definition: upb/benchmarks/benchmark.cc:204
google::protobuf::MessageLite::ParseFlags
ParseFlags
Definition: bloaty/third_party/protobuf/src/google/protobuf/message_lite.h:449
deps
static _upb_DefPool_Init * deps[4]
Definition: certs.upbdefs.c:72
Arena
struct Arena Arena
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/arena.h:189
init
const char * init
Definition: upb/upb/bindings/lua/main.c:49
upb::SymbolTable
Definition: def.hpp:377
file
const grpc_generator::File * file
Definition: python_private_generator.h:38
buf
char buf[65535]
Definition: upb/benchmarks/benchmark.cc:43
upb_StringView::data
const char * data
Definition: upb/upb/upb.h:73
total
size_t total
Definition: cord_analysis.cc:59
string.h
UseArena
@ UseArena
Definition: upb/benchmarks/benchmark.cc:206
upb_Arena_Init
upb_Arena * upb_Arena_Init(void *mem, size_t n, upb_alloc *alloc)
Definition: upb/upb/upb.c:216
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
printf
_Use_decl_annotations_ int __cdecl printf(const char *_Format,...)
Definition: cs_driver.c:91
BM_ArenaInitialBlockOneAlloc
static void BM_ArenaInitialBlockOneAlloc(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:65
Proto2Factory< NoArena, P >::proto_
P proto_
Definition: upb/benchmarks/benchmark.cc:245
error_ref_leak.err
err
Definition: error_ref_leak.py:35
file
Definition: bloaty/third_party/zlib/examples/gzappend.c:170
cstest_report.opts
opts
Definition: cstest_report.py:81
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
status
absl::Status status
Definition: rls.cc:251
Proto2Factory< InitBlock, P >::GetProto
P * GetProto()
Definition: upb/benchmarks/benchmark.cc:261
def.hpp
BENCHMARK
BENCHMARK(BM_ArenaOneAlloc)
BM_SerializeDescriptor_Upb
static void BM_SerializeDescriptor_Upb(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:310
upb_Status_ErrorMessage
const char * upb_Status_ErrorMessage(const upb_Status *status)
Definition: upb/upb/upb.c:52
upb_Arena_New
UPB_INLINE upb_Arena * upb_Arena_New(void)
Definition: upb/upb/upb.h:267
upb_Status_Clear
void upb_Status_Clear(upb_Status *status)
Definition: upb/upb/upb.c:44
arena
grpc_core::ScopedArenaPtr arena
Definition: binder_transport_test.cc:237
python_utils.port_server.stderr
stderr
Definition: port_server.py:51
BM_LoadAdsDescriptor_Upb
static void BM_LoadAdsDescriptor_Upb(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:135
CopyStrings
CopyStrings
Definition: upb/benchmarks/benchmark.cc:199
Proto2Factory< NoArena, P >::GetProto
P * GetProto()
Definition: upb/benchmarks/benchmark.cc:242
arena_
Arena * arena_
Definition: client_channel.cc:391
NoLayout
@ NoLayout
Definition: upb/benchmarks/benchmark.cc:75
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
upb_StringView::size
size_t size
Definition: upb/upb/upb.h:74
Alias
@ Alias
Definition: upb/benchmarks/benchmark.cc:201
Proto2Factory< UseArena, P >::GetProto
P * GetProto()
Definition: upb/benchmarks/benchmark.cc:251
upb_Arena_Malloc
UPB_INLINE void * upb_Arena_Malloc(upb_Arena *a, size_t size)
Definition: upb/upb/upb.h:222
google::protobuf::StringPiece
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stringpiece.h:180
_upb_DefPool_Init
Definition: upb/upb/def.h:386
descriptor
upb_StringView descriptor
Definition: upb/benchmarks/benchmark.cc:39
gmock_output_test._
_
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
absl::flat_hash_set
Definition: abseil-cpp/absl/container/flat_hash_set.h:105
FileDescSV
::upb_benchmark::sv::FileDescriptorProto FileDescSV
Definition: upb/benchmarks/benchmark.cc:275
upb_Status_SetErrorFormat
void upb_Status_SetErrorFormat(upb_Status *status, const char *fmt,...)
Definition: upb/upb/upb.c:63
google::protobuf::DescriptorPool
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:1539
kUpb_DecodeOption_AliasString
@ kUpb_DecodeOption_AliasString
Definition: decode.h:47
BM_LoadAdsDescriptor_Proto2
static void BM_LoadAdsDescriptor_Proto2(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:157
upb_Status
Definition: upb/upb/upb.h:52
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
LoadDescriptorMode
LoadDescriptorMode
Definition: upb/benchmarks/benchmark.cc:74
google::protobuf::MessageLite::kMergePartial
@ kMergePartial
Definition: bloaty/third_party/protobuf/src/google/protobuf/message_lite.h:452
d
static const fe d
Definition: curve25519_tables.h:19
LoadDefInit_BuildLayout
bool LoadDefInit_BuildLayout(upb_DefPool *s, const _upb_DefPool_Init *init, size_t *bytes)
Definition: upb/benchmarks/benchmark.cc:84
Proto2Factory< InitBlock, P >::Proto2Factory
Proto2Factory()
Definition: upb/benchmarks/benchmark.cc:260
symtab
upb_symtab * symtab
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:774
google::protobuf::DynamicMessageFactory
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message.h:80
BM_ArenaOneAlloc
static void BM_ArenaOneAlloc(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:56
WithLayout
@ WithLayout
Definition: upb/benchmarks/benchmark.cc:76
CollectFileDescriptors
void CollectFileDescriptors(const _upb_DefPool_Init *file, std::vector< upb_StringView > &serialized_files, absl::flat_hash_set< const _upb_DefPool_Init * > &seen)
Definition: upb/benchmarks/benchmark.cc:45
google::protobuf::ArenaOptions
Definition: third_party/bloaty/third_party/protobuf/src/google/protobuf/arena.h:117
Proto2Factory< UseArena, P >::arena_
protobuf::Arena arena_
Definition: upb/benchmarks/benchmark.cc:254
P
Definition: miscompile_with_no_unique_address_test.cc:29
BM_Parse_Proto2
void BM_Parse_Proto2(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:278
upb_StringView
Definition: upb/upb/upb.h:72
bytes
uint8 bytes[10]
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/coded_stream_unittest.cc:153
Proto2Factory
Definition: upb/benchmarks/benchmark.cc:237
benchmark::State
Definition: benchmark/include/benchmark/benchmark.h:503
Copy
@ Copy
Definition: upb/benchmarks/benchmark.cc:200
google::protobuf::python::message_descriptor::GetOptions
static PyObject * GetOptions(PyBaseDescriptor *self)
Definition: bloaty/third_party/protobuf/python/google/protobuf/pyext/descriptor.cc:600
BENCHMARK_TEMPLATE
BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Upb, NoLayout)
file::size
int size
Definition: bloaty/third_party/zlib/examples/gzappend.c:172
BM_Parse_Upb_FileDesc
static void BM_Parse_Upb_FileDesc(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:211
Proto2Factory< InitBlock, P >::GetOptions
protobuf::ArenaOptions GetOptions()
Definition: upb/benchmarks/benchmark.cc:264
cpp.gmock_class.set
set
Definition: bloaty/third_party/googletest/googlemock/scripts/generator/cpp/gmock_class.py:44
ok
bool ok
Definition: async_end2end_test.cc:197
_upb_DefPool_BytesLoaded
size_t _upb_DefPool_BytesLoaded(const upb_DefPool *s)
Definition: upb/upb/def.c:3201
state
Definition: bloaty/third_party/zlib/contrib/blast/blast.c:41
google::protobuf::Descriptor
Definition: bloaty/third_party/protobuf/src/google/protobuf/descriptor.h:231
upb_DefPool_AddFile
const upb_FileDef * upb_DefPool_AddFile(upb_DefPool *s, const google_protobuf_FileDescriptorProto *file_proto, upb_Status *status)
Definition: upb/upb/def.c:3140
input
std::string input
Definition: bloaty/third_party/protobuf/src/google/protobuf/io/tokenizer_unittest.cc:197
pool
InternalDescriptorPool * pool
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:807
grpc::protobuf::FileDescriptorProto
GRPC_CUSTOM_FILEDESCRIPTORPROTO FileDescriptorProto
Definition: include/grpcpp/impl/codegen/config_protobuf.h:86
Proto2Factory< InitBlock, P >::arena_
protobuf::Arena arena_
Definition: upb/benchmarks/benchmark.cc:271
FileDesc
::upb_benchmark::FileDescriptorProto FileDesc
Definition: upb/benchmarks/benchmark.cc:274
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
seen
bool * seen
Definition: async_end2end_test.cc:198
google_protobuf_FileDescriptorProto_parse_ex
UPB_INLINE google_protobuf_FileDescriptorProto * google_protobuf_FileDescriptorProto_parse_ex(const char *buf, size_t size, const upb_ExtensionRegistry *extreg, int options, upb_Arena *arena)
Definition: descriptor.upb.h:231
NoArena
@ NoArena
Definition: upb/benchmarks/benchmark.cc:205
google_protobuf_FileDescriptorProto
struct google_protobuf_FileDescriptorProto google_protobuf_FileDescriptorProto
Definition: descriptor.upb.h:51
upb_DefPool
Definition: upb/upb/def.c:217
upb_Arena
Definition: upb_internal.h:36
upb_Arena_Free
void upb_Arena_Free(upb_Arena *a)
Definition: upb/upb/upb.c:273
InitBlock
@ InitBlock
Definition: upb/benchmarks/benchmark.cc:207
google::protobuf::DynamicMessageFactory::GetPrototype
const Message * GetPrototype(const Descriptor *type) override
Definition: bloaty/third_party/protobuf/src/google/protobuf/dynamic_message.cc:653
BM_SerializeDescriptor_Proto2
static void BM_SerializeDescriptor_Proto2(benchmark::State &state)
Definition: upb/benchmarks/benchmark.cc:300
upb_DefPool_FindFileByName
const upb_FileDef * upb_DefPool_FindFileByName(const upb_DefPool *s, const char *name)
Definition: upb/upb/def.c:1145


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:46