Protocol Buffers - Google's data interchange format (grpc依赖)
https://developers.google.com/protocol-buffers/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
391 lines
12 KiB
391 lines
12 KiB
1 year ago
|
// Protocol Buffers - Google's data interchange format
|
||
|
// Copyright 2023 Google LLC. All rights reserved.
|
||
|
// https://developers.google.com/protocol-buffers/
|
||
4 years ago
|
//
|
||
|
// Redistribution and use in source and binary forms, with or without
|
||
1 year ago
|
// modification, are permitted provided that the following conditions are
|
||
|
// met:
|
||
|
//
|
||
4 years ago
|
// * Redistributions of source code must retain the above copyright
|
||
1 year ago
|
// notice, this list of conditions and the following disclaimer.
|
||
|
// * Redistributions in binary form must reproduce the above
|
||
|
// copyright notice, this list of conditions and the following disclaimer
|
||
|
// in the documentation and/or other materials provided with the
|
||
|
// distribution.
|
||
|
// * Neither the name of Google LLC nor the names of its
|
||
|
// contributors may be used to endorse or promote products derived from
|
||
|
// this software without specific prior written permission.
|
||
4 years ago
|
//
|
||
1 year ago
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||
|
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||
|
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||
|
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||
|
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||
|
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||
|
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
|
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
|
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
|
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
6 years ago
|
|
||
|
#include <benchmark/benchmark.h>
|
||
2 years ago
|
|
||
4 years ago
|
#include <string.h>
|
||
|
|
||
1 year ago
|
#include <vector>
|
||
|
|
||
2 years ago
|
#include "google/ads/googleads/v13/services/google_ads_service.upbdefs.h"
|
||
2 years ago
|
#include "google/protobuf/descriptor.pb.h"
|
||
4 years ago
|
#include "absl/container/flat_hash_set.h"
|
||
2 years ago
|
#include "google/protobuf/dynamic_message.h"
|
||
4 years ago
|
#include "benchmarks/descriptor.pb.h"
|
||
|
#include "benchmarks/descriptor.upb.h"
|
||
|
#include "benchmarks/descriptor.upbdefs.h"
|
||
|
#include "benchmarks/descriptor_sv.pb.h"
|
||
1 year ago
|
#include "upb/base/internal/log2.h"
|
||
2 years ago
|
#include "upb/mem/arena.h"
|
||
2 years ago
|
#include "upb/reflection/def.hpp"
|
||
6 years ago
|
|
||
3 years ago
|
upb_StringView descriptor = benchmarks_descriptor_proto_upbdefinit.descriptor;
|
||
4 years ago
|
namespace protobuf = ::google::protobuf;
|
||
6 years ago
|
|
||
2 years ago
|
// A buffer big enough to parse descriptor.proto without going to heap.
|
||
|
// We use 64-bit ints here to force alignment.
|
||
|
int64_t buf[8191];
|
||
6 years ago
|
|
||
3 years ago
|
void CollectFileDescriptors(
|
||
|
const _upb_DefPool_Init* file,
|
||
|
std::vector<upb_StringView>& serialized_files,
|
||
|
absl::flat_hash_set<const _upb_DefPool_Init*>& seen) {
|
||
4 years ago
|
if (!seen.insert(file).second) return;
|
||
3 years ago
|
for (_upb_DefPool_Init** deps = file->deps; *deps; deps++) {
|
||
4 years ago
|
CollectFileDescriptors(*deps, serialized_files, seen);
|
||
4 years ago
|
}
|
||
4 years ago
|
serialized_files.push_back(file->descriptor);
|
||
4 years ago
|
}
|
||
|
|
||
5 years ago
|
static void BM_ArenaOneAlloc(benchmark::State& state) {
|
||
6 years ago
|
for (auto _ : state) {
|
||
3 years ago
|
upb_Arena* arena = upb_Arena_New();
|
||
|
upb_Arena_Malloc(arena, 1);
|
||
|
upb_Arena_Free(arena);
|
||
6 years ago
|
}
|
||
|
}
|
||
5 years ago
|
BENCHMARK(BM_ArenaOneAlloc);
|
||
5 years ago
|
|
||
5 years ago
|
static void BM_ArenaInitialBlockOneAlloc(benchmark::State& state) {
|
||
5 years ago
|
for (auto _ : state) {
|
||
1 year ago
|
upb_Arena* arena = upb_Arena_Init(buf, sizeof(buf), nullptr);
|
||
3 years ago
|
upb_Arena_Malloc(arena, 1);
|
||
|
upb_Arena_Free(arena);
|
||
5 years ago
|
}
|
||
|
}
|
||
5 years ago
|
BENCHMARK(BM_ArenaInitialBlockOneAlloc);
|
||
6 years ago
|
|
||
2 years ago
|
static void BM_ArenaFuseUnbalanced(benchmark::State& state) {
|
||
|
std::vector<upb_Arena*> arenas(state.range(0));
|
||
|
size_t n = 0;
|
||
|
for (auto _ : state) {
|
||
|
for (auto& arena : arenas) {
|
||
|
arena = upb_Arena_New();
|
||
|
}
|
||
|
for (auto& arena : arenas) {
|
||
|
upb_Arena_Fuse(arenas[0], arena);
|
||
|
}
|
||
|
for (auto& arena : arenas) {
|
||
|
upb_Arena_Free(arena);
|
||
|
}
|
||
|
n += arenas.size();
|
||
|
}
|
||
|
state.SetItemsProcessed(n);
|
||
|
}
|
||
|
BENCHMARK(BM_ArenaFuseUnbalanced)->Range(2, 128);
|
||
|
|
||
|
static void BM_ArenaFuseBalanced(benchmark::State& state) {
|
||
|
std::vector<upb_Arena*> arenas(state.range(0));
|
||
|
size_t n = 0;
|
||
|
|
||
|
for (auto _ : state) {
|
||
|
for (auto& arena : arenas) {
|
||
|
arena = upb_Arena_New();
|
||
|
}
|
||
|
|
||
|
// Perform a series of fuses that keeps the halves balanced.
|
||
|
size_t max = upb_Log2Ceiling(arenas.size());
|
||
|
for (size_t n = 0; n <= max; n++) {
|
||
|
size_t step = 1 << n;
|
||
|
for (size_t i = 0; i + step < arenas.size(); i += (step * 2)) {
|
||
|
upb_Arena_Fuse(arenas[i], arenas[i + step]);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
for (auto& arena : arenas) {
|
||
|
upb_Arena_Free(arena);
|
||
|
}
|
||
|
n += arenas.size();
|
||
|
}
|
||
|
state.SetItemsProcessed(n);
|
||
|
}
|
||
|
BENCHMARK(BM_ArenaFuseBalanced)->Range(2, 128);
|
||
|
|
||
3 years ago
|
enum LoadDescriptorMode {
|
||
|
NoLayout,
|
||
|
WithLayout,
|
||
|
};
|
||
|
|
||
|
// This function is mostly copied from upb/def.c, but it is modified to avoid
|
||
|
// passing in the pre-generated mini-tables, in order to force upb to compute
|
||
|
// them dynamically. Generally you would never want to do this, but we want to
|
||
|
// simulate the cost we would pay if we were loading these types purely from
|
||
|
// descriptors, with no mini-tales available.
|
||
3 years ago
|
bool LoadDefInit_BuildLayout(upb_DefPool* s, const _upb_DefPool_Init* init,
|
||
|
size_t* bytes) {
|
||
3 years ago
|
_upb_DefPool_Init** deps = init->deps;
|
||
|
google_protobuf_FileDescriptorProto* file;
|
||
|
upb_Arena* arena;
|
||
|
upb_Status status;
|
||
|
|
||
|
upb_Status_Clear(&status);
|
||
|
|
||
|
if (upb_DefPool_FindFileByName(s, init->filename)) {
|
||
|
return true;
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
|
arena = upb_Arena_New();
|
||
|
|
||
|
for (; *deps; deps++) {
|
||
3 years ago
|
if (!LoadDefInit_BuildLayout(s, *deps, bytes)) goto err;
|
||
3 years ago
|
}
|
||
|
|
||
|
file = google_protobuf_FileDescriptorProto_parse_ex(
|
||
1 year ago
|
init->descriptor.data, init->descriptor.size, nullptr,
|
||
3 years ago
|
kUpb_DecodeOption_AliasString, arena);
|
||
|
*bytes += init->descriptor.size;
|
||
|
|
||
|
if (!file) {
|
||
|
upb_Status_SetErrorFormat(
|
||
|
&status,
|
||
|
"Failed to parse compiled-in descriptor for file '%s'. This should "
|
||
|
"never happen.",
|
||
|
init->filename);
|
||
|
goto err;
|
||
|
}
|
||
|
|
||
|
// KEY DIFFERENCE: Here we pass in only the descriptor, and not the
|
||
|
// pre-generated minitables.
|
||
|
if (!upb_DefPool_AddFile(s, file, &status)) {
|
||
|
goto err;
|
||
|
}
|
||
|
|
||
|
upb_Arena_Free(arena);
|
||
|
return true;
|
||
|
|
||
|
err:
|
||
|
fprintf(stderr,
|
||
|
"Error loading compiled-in descriptor for file '%s' (this should "
|
||
|
"never happen): %s\n",
|
||
|
init->filename, upb_Status_ErrorMessage(&status));
|
||
|
exit(1);
|
||
4 years ago
|
}
|
||
4 years ago
|
|
||
3 years ago
|
template <LoadDescriptorMode Mode>
|
||
4 years ago
|
static void BM_LoadAdsDescriptor_Upb(benchmark::State& state) {
|
||
4 years ago
|
size_t bytes_per_iter = 0;
|
||
4 years ago
|
for (auto _ : state) {
|
||
2 years ago
|
upb::DefPool defpool;
|
||
3 years ago
|
if (Mode == NoLayout) {
|
||
2 years ago
|
google_ads_googleads_v13_services_SearchGoogleAdsRequest_getmsgdef(
|
||
2 years ago
|
defpool.ptr());
|
||
|
bytes_per_iter = _upb_DefPool_BytesLoaded(defpool.ptr());
|
||
3 years ago
|
} else {
|
||
|
bytes_per_iter = 0;
|
||
3 years ago
|
LoadDefInit_BuildLayout(
|
||
2 years ago
|
defpool.ptr(),
|
||
2 years ago
|
&google_ads_googleads_v13_services_google_ads_service_proto_upbdefinit,
|
||
3 years ago
|
&bytes_per_iter);
|
||
4 years ago
|
}
|
||
|
}
|
||
3 years ago
|
state.SetBytesProcessed(state.iterations() * bytes_per_iter);
|
||
4 years ago
|
}
|
||
3 years ago
|
BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Upb, NoLayout);
|
||
|
BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Upb, WithLayout);
|
||
4 years ago
|
|
||
3 years ago
|
template <LoadDescriptorMode Mode>
|
||
4 years ago
|
static void BM_LoadAdsDescriptor_Proto2(benchmark::State& state) {
|
||
3 years ago
|
extern _upb_DefPool_Init
|
||
2 years ago
|
google_ads_googleads_v13_services_google_ads_service_proto_upbdefinit;
|
||
3 years ago
|
std::vector<upb_StringView> serialized_files;
|
||
|
absl::flat_hash_set<const _upb_DefPool_Init*> seen_files;
|
||
4 years ago
|
CollectFileDescriptors(
|
||
2 years ago
|
&google_ads_googleads_v13_services_google_ads_service_proto_upbdefinit,
|
||
4 years ago
|
serialized_files, seen_files);
|
||
4 years ago
|
size_t bytes_per_iter = 0;
|
||
4 years ago
|
for (auto _ : state) {
|
||
|
bytes_per_iter = 0;
|
||
|
protobuf::Arena arena;
|
||
|
protobuf::DescriptorPool pool;
|
||
|
for (auto file : serialized_files) {
|
||
2 years ago
|
absl::string_view input(file.data, file.size);
|
||
3 years ago
|
auto proto =
|
||
|
protobuf::Arena::CreateMessage<protobuf::FileDescriptorProto>(&arena);
|
||
4 years ago
|
bool ok = proto->ParseFrom<protobuf::MessageLite::kMergePartial>(input) &&
|
||
4 years ago
|
pool.BuildFile(*proto) != nullptr;
|
||
|
if (!ok) {
|
||
|
printf("Failed to add file.\n");
|
||
|
exit(1);
|
||
|
}
|
||
4 years ago
|
bytes_per_iter += input.size();
|
||
4 years ago
|
}
|
||
3 years ago
|
|
||
|
if (Mode == WithLayout) {
|
||
|
protobuf::DynamicMessageFactory factory;
|
||
3 years ago
|
const protobuf::Descriptor* d = pool.FindMessageTypeByName(
|
||
2 years ago
|
"google.ads.googleads.v13.services.SearchGoogleAdsResponse");
|
||
3 years ago
|
if (!d) {
|
||
|
printf("Failed to find descriptor.\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
factory.GetPrototype(d);
|
||
|
}
|
||
4 years ago
|
}
|
||
|
state.SetBytesProcessed(state.iterations() * bytes_per_iter);
|
||
|
}
|
||
3 years ago
|
BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Proto2, NoLayout);
|
||
|
BENCHMARK_TEMPLATE(BM_LoadAdsDescriptor_Proto2, WithLayout);
|
||
4 years ago
|
|
||
4 years ago
|
enum CopyStrings {
|
||
|
Copy,
|
||
|
Alias,
|
||
|
};
|
||
|
|
||
|
enum ArenaMode {
|
||
|
NoArena,
|
||
|
UseArena,
|
||
|
InitBlock,
|
||
|
};
|
||
5 years ago
|
|
||
4 years ago
|
template <ArenaMode AMode, CopyStrings Copy>
|
||
|
static void BM_Parse_Upb_FileDesc(benchmark::State& state) {
|
||
5 years ago
|
for (auto _ : state) {
|
||
3 years ago
|
upb_Arena* arena;
|
||
4 years ago
|
if (AMode == InitBlock) {
|
||
1 year ago
|
arena = upb_Arena_Init(buf, sizeof(buf), nullptr);
|
||
4 years ago
|
} else {
|
||
3 years ago
|
arena = upb_Arena_New();
|
||
4 years ago
|
}
|
||
4 years ago
|
upb_benchmark_FileDescriptorProto* set =
|
||
4 years ago
|
upb_benchmark_FileDescriptorProto_parse_ex(
|
||
1 year ago
|
descriptor.data, descriptor.size, nullptr,
|
||
3 years ago
|
Copy == Alias ? kUpb_DecodeOption_AliasString : 0, arena);
|
||
5 years ago
|
if (!set) {
|
||
|
printf("Failed to parse.\n");
|
||
|
exit(1);
|
||
|
}
|
||
3 years ago
|
upb_Arena_Free(arena);
|
||
5 years ago
|
}
|
||
|
state.SetBytesProcessed(state.iterations() * descriptor.size);
|
||
|
}
|
||
4 years ago
|
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Copy);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, UseArena, Alias);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Copy);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Upb_FileDesc, InitBlock, Alias);
|
||
4 years ago
|
|
||
4 years ago
|
template <ArenaMode AMode, class P>
|
||
|
struct Proto2Factory;
|
||
|
|
||
3 years ago
|
template <class P>
|
||
4 years ago
|
struct Proto2Factory<NoArena, P> {
|
||
4 years ago
|
public:
|
||
1 year ago
|
P* GetProto() { return &proto; }
|
||
4 years ago
|
|
||
|
private:
|
||
1 year ago
|
P proto;
|
||
4 years ago
|
};
|
||
|
|
||
|
template <class P>
|
||
4 years ago
|
struct Proto2Factory<UseArena, P> {
|
||
4 years ago
|
public:
|
||
1 year ago
|
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena); }
|
||
4 years ago
|
|
||
|
private:
|
||
1 year ago
|
protobuf::Arena arena;
|
||
4 years ago
|
};
|
||
|
|
||
|
template <class P>
|
||
4 years ago
|
struct Proto2Factory<InitBlock, P> {
|
||
4 years ago
|
public:
|
||
1 year ago
|
Proto2Factory() : arena(GetOptions()) {}
|
||
|
P* GetProto() { return protobuf::Arena::CreateMessage<P>(&arena); }
|
||
4 years ago
|
|
||
|
private:
|
||
|
protobuf::ArenaOptions GetOptions() {
|
||
|
protobuf::ArenaOptions opts;
|
||
2 years ago
|
opts.initial_block = (char*)buf;
|
||
4 years ago
|
opts.initial_block_size = sizeof(buf);
|
||
|
return opts;
|
||
4 years ago
|
}
|
||
|
|
||
1 year ago
|
protobuf::Arena arena;
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
4 years ago
|
using FileDesc = ::upb_benchmark::FileDescriptorProto;
|
||
|
using FileDescSV = ::upb_benchmark::sv::FileDescriptorProto;
|
||
4 years ago
|
|
||
4 years ago
|
template <class P, ArenaMode AMode, CopyStrings kCopy>
|
||
4 years ago
|
void BM_Parse_Proto2(benchmark::State& state) {
|
||
4 years ago
|
constexpr protobuf::MessageLite::ParseFlags kParseFlags =
|
||
|
kCopy == Copy
|
||
|
? protobuf::MessageLite::ParseFlags::kMergePartial
|
||
|
: protobuf::MessageLite::ParseFlags::kMergePartialWithAliasing;
|
||
4 years ago
|
for (auto _ : state) {
|
||
4 years ago
|
Proto2Factory<AMode, P> proto_factory;
|
||
4 years ago
|
auto proto = proto_factory.GetProto();
|
||
2 years ago
|
absl::string_view input(descriptor.data, descriptor.size);
|
||
4 years ago
|
bool ok = proto->template ParseFrom<kParseFlags>(input);
|
||
4 years ago
|
if (!ok) {
|
||
|
printf("Failed to parse.\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
}
|
||
|
state.SetBytesProcessed(state.iterations() * descriptor.size);
|
||
|
}
|
||
4 years ago
|
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, NoArena, Copy);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, UseArena, Copy);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDesc, InitBlock, Copy);
|
||
|
BENCHMARK_TEMPLATE(BM_Parse_Proto2, FileDescSV, InitBlock, Alias);
|
||
4 years ago
|
|
||
|
static void BM_SerializeDescriptor_Proto2(benchmark::State& state) {
|
||
4 years ago
|
upb_benchmark::FileDescriptorProto proto;
|
||
4 years ago
|
proto.ParseFromArray(descriptor.data, descriptor.size);
|
||
|
for (auto _ : state) {
|
||
4 years ago
|
proto.SerializePartialToArray(buf, sizeof(buf));
|
||
4 years ago
|
}
|
||
|
state.SetBytesProcessed(state.iterations() * descriptor.size);
|
||
|
}
|
||
|
BENCHMARK(BM_SerializeDescriptor_Proto2);
|
||
|
|
||
|
static void BM_SerializeDescriptor_Upb(benchmark::State& state) {
|
||
4 years ago
|
int64_t total = 0;
|
||
3 years ago
|
upb_Arena* arena = upb_Arena_New();
|
||
4 years ago
|
upb_benchmark_FileDescriptorProto* set =
|
||
|
upb_benchmark_FileDescriptorProto_parse(descriptor.data, descriptor.size,
|
||
|
arena);
|
||
4 years ago
|
if (!set) {
|
||
|
printf("Failed to parse.\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
for (auto _ : state) {
|
||
1 year ago
|
upb_Arena* enc_arena = upb_Arena_Init(buf, sizeof(buf), nullptr);
|
||
4 years ago
|
size_t size;
|
||
4 years ago
|
char* data =
|
||
|
upb_benchmark_FileDescriptorProto_serialize(set, enc_arena, &size);
|
||
4 years ago
|
if (!data) {
|
||
|
printf("Failed to serialize.\n");
|
||
|
exit(1);
|
||
|
}
|
||
|
total += size;
|
||
|
}
|
||
4 years ago
|
state.SetBytesProcessed(total);
|
||
4 years ago
|
}
|
||
4 years ago
|
BENCHMARK(BM_SerializeDescriptor_Upb);
|