Skip to content

Commit

Permalink
In OSS mode omit some extern template specializations. We have seen r…
Browse files Browse the repository at this point in the history
…eports of

compilers falling over due to the size of translation units.

PiperOrigin-RevId: 549653990
  • Loading branch information
protobuf-github-bot authored and zhangskz committed Jul 21, 2023
1 parent 87ffe68 commit 2ed49c9
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 221 deletions.
17 changes: 12 additions & 5 deletions src/google/protobuf/compiler/cpp/file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1242,11 +1242,18 @@ class FileGenerator::ForwardDeclarations {
}

void PrintTopLevelDecl(io::Printer* p, const Options& options) const {
for (const auto& c : classes_) {
p->Emit({{"class", QualifiedClassName(c.second, options)}}, R"cc(
template <>
$dllexport_decl $$class$* Arena::CreateMaybeMessage<$class$>(Arena*);
)cc");
if (ShouldGenerateExternSpecializations(options)) {
for (const auto& c : classes_) {
// To reduce total linker input size in large binaries we make these
// functions extern and define then in the pb.cc file. This avoids bloat
// in callers by having duplicate definitions of the template.
// However, it increases the size of the pb.cc translation units so it
// is a tradeoff.
p->Emit({{"class", QualifiedClassName(c.second, options)}}, R"cc(
template <>
$dllexport_decl $$class$* Arena::CreateMaybeMessage<$class$>(Arena*);
)cc");
}
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/google/protobuf/compiler/cpp/helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,14 @@ void GenerateUtf8CheckCodeForCord(io::Printer* p, const FieldDescriptor* field,
const Options& options, bool for_parse,
absl::string_view parameters);

inline bool ShouldGenerateExternSpecializations(const Options& options) {
// For OSS we omit the specializations to reduce codegen size.
// Some compilers can't handle that much input in a single translation unit.
// These specializations are just a link size optimization and do not affect
// correctness or performance, so it is ok to omit them.
return !options.opensource_runtime;
}

struct OneOfRangeImpl {
struct Iterator {
using iterator_category = std::forward_iterator_tag;
Expand Down
14 changes: 8 additions & 6 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2953,12 +2953,14 @@ void MessageGenerator::GenerateSourceInProto2Namespace(io::Printer* p) {
auto v = p->WithVars(ClassVars(descriptor_, options_));
auto t = p->WithVars(MakeTrackerCalls(descriptor_, options_));
Formatter format(p);
format(
"template<> "
"PROTOBUF_NOINLINE $classtype$*\n"
"Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n"
" return Arena::CreateMessageInternal< $classtype$ >(arena);\n"
"}\n");
if (ShouldGenerateExternSpecializations(options_)) {
format(
"template<> "
"PROTOBUF_NOINLINE $classtype$*\n"
"Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n"
" return Arena::CreateMessageInternal< $classtype$ >(arena);\n"
"}\n");
}
}

void MessageGenerator::GenerateClear(io::Printer* p) {
Expand Down
16 changes: 0 additions & 16 deletions src/google/protobuf/compiler/plugin.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions src/google/protobuf/compiler/plugin.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions src/google/protobuf/cpp_features.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/google/protobuf/cpp_features.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

120 changes: 0 additions & 120 deletions src/google/protobuf/descriptor.pb.cc

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 0 additions & 60 deletions src/google/protobuf/descriptor.pb.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2ed49c9

Please sign in to comment.