Skip to content

Commit 2ed49c9

Browse files
protobuf-github-botzhangskz
authored andcommitted
In OSS mode omit some extern template specializations. We have seen reports of
compilers falling over due to the size of translation units. PiperOrigin-RevId: 549653990
1 parent 87ffe68 commit 2ed49c9

File tree

9 files changed

+28
-221
lines changed

9 files changed

+28
-221
lines changed

src/google/protobuf/compiler/cpp/file.cc

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,11 +1242,18 @@ class FileGenerator::ForwardDeclarations {
12421242
}
12431243

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

src/google/protobuf/compiler/cpp/helpers.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,14 @@ void GenerateUtf8CheckCodeForCord(io::Printer* p, const FieldDescriptor* field,
10081008
const Options& options, bool for_parse,
10091009
absl::string_view parameters);
10101010

1011+
inline bool ShouldGenerateExternSpecializations(const Options& options) {
1012+
// For OSS we omit the specializations to reduce codegen size.
1013+
// Some compilers can't handle that much input in a single translation unit.
1014+
// These specializations are just a link size optimization and do not affect
1015+
// correctness or performance, so it is ok to omit them.
1016+
return !options.opensource_runtime;
1017+
}
1018+
10111019
struct OneOfRangeImpl {
10121020
struct Iterator {
10131021
using iterator_category = std::forward_iterator_tag;

src/google/protobuf/compiler/cpp/message.cc

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2953,12 +2953,14 @@ void MessageGenerator::GenerateSourceInProto2Namespace(io::Printer* p) {
29532953
auto v = p->WithVars(ClassVars(descriptor_, options_));
29542954
auto t = p->WithVars(MakeTrackerCalls(descriptor_, options_));
29552955
Formatter format(p);
2956-
format(
2957-
"template<> "
2958-
"PROTOBUF_NOINLINE $classtype$*\n"
2959-
"Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n"
2960-
" return Arena::CreateMessageInternal< $classtype$ >(arena);\n"
2961-
"}\n");
2956+
if (ShouldGenerateExternSpecializations(options_)) {
2957+
format(
2958+
"template<> "
2959+
"PROTOBUF_NOINLINE $classtype$*\n"
2960+
"Arena::CreateMaybeMessage< $classtype$ >(Arena* arena) {\n"
2961+
" return Arena::CreateMessageInternal< $classtype$ >(arena);\n"
2962+
"}\n");
2963+
}
29622964
}
29632965

29642966
void MessageGenerator::GenerateClear(io::Printer* p) {

src/google/protobuf/compiler/plugin.pb.cc

Lines changed: 0 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/compiler/plugin.pb.h

Lines changed: 0 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.cc

Lines changed: 0 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/cpp_features.pb.h

Lines changed: 0 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/descriptor.pb.cc

Lines changed: 0 additions & 120 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/google/protobuf/descriptor.pb.h

Lines changed: 0 additions & 60 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)