Added a codegen parameter for whether fasttables are generated or not.

Example:

$ CC=clang bazel build -c opt --copt=-g benchmarks:benchmark --//:fasttable_enabled=false
INFO: Build option --//:fasttable_enabled has changed, discarding analysis cache.
INFO: Analyzed target //benchmarks:benchmark (0 packages loaded, 913 targets configured).
INFO: Found 1 target...
Target //benchmarks:benchmark up-to-date:
  bazel-bin/benchmarks/benchmark
INFO: Elapsed time: 0.760s, Critical Path: 0.58s
INFO: 7 processes: 1 internal, 6 linux-sandbox.
INFO: Build completed successfully, 7 total actions
$ bazel-bin/benchmarks/benchmark --benchmark_filter=BM_Parse_Upb
------------------------------------------------------------------------------
Benchmark                                       Time           CPU Iterations
------------------------------------------------------------------------------
BM_Parse_Upb_FileDesc_WithArena             10985 ns      10984 ns      63567   651.857MB/s
BM_Parse_Upb_FileDesc_WithInitialBlock      10556 ns      10554 ns      66138   678.458MB/s
$ CC=clang bazel build -c opt --copt=-g benchmarks:benchmark --//:fasttable_enabled=true
INFO: Build option --//:fasttable_enabled has changed, discarding analysis cache.
INFO: Analyzed target //benchmarks:benchmark (0 packages loaded, 913 targets configured).
INFO: Found 1 target...
Target //benchmarks:benchmark up-to-date:
  bazel-bin/benchmarks/benchmark
INFO: Elapsed time: 0.744s, Critical Path: 0.58s
INFO: 7 processes: 1 internal, 6 linux-sandbox.
INFO: Build completed successfully, 7 total actions
$ bazel-bin/benchmarks/benchmark --benchmark_filter=BM_Parse_Upb
------------------------------------------------------------------------------
Benchmark                                       Time           CPU Iterations
------------------------------------------------------------------------------
BM_Parse_Upb_FileDesc_WithArena              3284 ns       3284 ns     213495    2.1293GB/s
BM_Parse_Upb_FileDesc_WithInitialBlock       2882 ns       2882 ns     243069    2.4262GB/s

Biggest unknown is whether this parameter should default to true or false.
pull/13171/head
Joshua Haberman 4 years ago
parent 8e8dbb5258
commit a345af9883
  1. 7
      BUILD
  2. 28
      bazel/upb_proto_library.bzl
  3. 3
      cmake/make_cmakelists.py
  4. 18
      upbc/generator.cc

@ -5,6 +5,7 @@ load(
)
load(
"//bazel:upb_proto_library.bzl",
"upb_fasttable_enabled",
"upb_proto_library",
"upb_proto_reflection_library",
)
@ -35,6 +36,12 @@ config_setting(
constraint_values = ["@bazel_tools//platforms:windows"],
)
upb_fasttable_enabled(
name = "fasttable_enabled",
build_setting_default = True,
visibility = ["//visibility:public"],
)
# Public C/C++ libraries #######################################################
cc_library(

@ -106,6 +106,28 @@ def _cc_library_func(ctx, name, hdrs, srcs, dep_ccinfos):
linking_context = linking_context,
)
# Build setting for whether fasttable code generation is enabled ###############
_FastTableEnabled = provider(
fields = {
"enabled": "whether fasttable is enabled",
},
)
def fasttable_enabled_impl(ctx):
raw_setting = ctx.build_setting_value
if raw_setting:
# TODO(haberman): check that the target CPU supports fasttable.
pass
return _FastTableEnabled(enabled = raw_setting)
upb_fasttable_enabled = rule(
implementation = fasttable_enabled_impl,
build_setting = config.bool(flag = True)
)
# upb_proto_library / upb_proto_reflection_library shared code #################
GeneratedSrcsInfo = provider(
@ -127,6 +149,8 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext):
srcs = [_generate_output_file(ctx, name, ext + ".c") for name in proto_sources]
hdrs = [_generate_output_file(ctx, name, ext + ".h") for name in proto_sources]
transitive_sets = proto_info.transitive_descriptor_sets.to_list()
fasttable_enabled = ctx.attr._fasttable_enabled[_FastTableEnabled].enabled
codegen_params = "fasttable:" if fasttable_enabled else ""
ctx.actions.run(
inputs = depset(
direct = [proto_info.direct_descriptor_set],
@ -136,7 +160,7 @@ def _compile_upb_protos(ctx, proto_info, proto_sources, ext):
outputs = srcs + hdrs,
executable = ctx.executable._protoc,
arguments = [
"--upb_out=" + _get_real_root(srcs[0]),
"--upb_out=" + codegen_params + _get_real_root(srcs[0]),
"--plugin=protoc-gen-upb=" + ctx.executable._upbc.path,
"--descriptor_set_in=" + ctx.configuration.host_path_separator.join([f.path for f in transitive_sets]),
] +
@ -240,6 +264,7 @@ _upb_proto_library_aspect = aspect(
"//:upb",
]),
"_ext": attr.string(default = ".upb"),
"_fasttable_enabled": attr.label(default = "//:fasttable_enabled")
}),
implementation = _upb_proto_library_aspect_impl,
provides = [
@ -295,6 +320,7 @@ _upb_proto_reflection_library_aspect = aspect(
],
),
"_ext": attr.string(default = ".upbdefs"),
"_fasttable_enabled": attr.label(default = "//:fasttable_enabled")
}),
implementation = _upb_proto_reflection_library_aspect_impl,
provides = [

@ -141,6 +141,9 @@ class BuildFileFunctions(object):
def config_setting(self, **kwargs):
pass
def upb_fasttable_enabled(self, **kwargs):
pass
def select(self, arg_dict):
return []

@ -883,7 +883,8 @@ std::vector<TableEntry> FastDecodeTable(const protobuf::Descriptor* message,
return table;
}
void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
void WriteSource(const protobuf::FileDescriptor* file, Output& output,
bool fasttable_enabled) {
EmitFileWarning(file, output);
output(
@ -975,7 +976,13 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
output("};\n\n");
}
std::vector<TableEntry> table = FastDecodeTable(message, layout);
std::vector<TableEntry> table;
uint8_t table_mask = -1;
if (fasttable_enabled) {
table = FastDecodeTable(message, layout);
table_mask = (table.size() - 1) << 3;
}
output("const upb_msglayout $0 = {\n", MessageInit(message));
output(" $0,\n", submsgs_array_ref);
@ -983,7 +990,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
output(" $0, $1, $2, $3,\n", GetSizeInit(layout.message_size()),
field_number_order.size(),
"false", // TODO: extendable
(table.size() - 1) << 3
table_mask
);
output(" {\n");
for (const auto& ent : table) {
@ -1120,14 +1127,15 @@ void WriteDefSource(const protobuf::FileDescriptor* file, Output& output) {
}
bool Generator::Generate(const protobuf::FileDescriptor* file,
const std::string& /* parameter */,
const std::string& parameter,
protoc::GeneratorContext* context,
std::string* /* error */) const {
bool fasttable_enabled = parameter == "fasttable";
Output h_output(context->Open(HeaderFilename(file->name())));
WriteHeader(file, h_output);
Output c_output(context->Open(SourceFilename(file->name())));
WriteSource(file, c_output);
WriteSource(file, c_output, fasttable_enabled);
Output h_def_output(context->Open(DefHeaderFilename(file->name())));
WriteDefHeader(file, h_def_output);

Loading…
Cancel
Save