Skip to content

Commit

Permalink
Merge pull request #13369 from zhangskz/backport-24-x
Browse files Browse the repository at this point in the history
In OSS mode omit some extern template specializations. We have seen r…
  • Loading branch information
zhangskz authored Jul 21, 2023
2 parents 87ffe68 + a71357d commit 980050e
Show file tree
Hide file tree
Showing 29 changed files with 28 additions and 383 deletions.
4 changes: 0 additions & 4 deletions src/google/protobuf/any.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/any.pb.h

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

12 changes: 0 additions & 12 deletions src/google/protobuf/api.pb.cc

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

6 changes: 0 additions & 6 deletions src/google/protobuf/api.pb.h

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

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.

Loading

0 comments on commit 980050e

Please sign in to comment.