diff options
author | Feng Xiao <xfxyjwf@gmail.com> | 2016-01-06 18:06:43 -0800 |
---|---|---|
committer | Feng Xiao <xfxyjwf@gmail.com> | 2016-01-06 18:10:24 -0800 |
commit | 76195058e25d19fc918996d55d3ad69ee55cb77e (patch) | |
tree | 3b93b3195a1a411e11b93644429cc6c96668b128 /src/google/protobuf/compiler/cpp/cpp_message.cc | |
parent | 363316a8d79ad3bebf47c6347a038b6130212e28 (diff) | |
download | protobuf-76195058e25d19fc918996d55d3ad69ee55cb77e.tar.gz protobuf-76195058e25d19fc918996d55d3ad69ee55cb77e.tar.bz2 protobuf-76195058e25d19fc918996d55d3ad69ee55cb77e.zip |
Patch internal change 111557819.
Defer calls to mutable_unknown_fields() until it is actually required to
save memory for C++ lite runtime.
Change-Id: Ica9c1fd276cdb164942d1e7b6e098c83ee3ffdc5
Diffstat (limited to 'src/google/protobuf/compiler/cpp/cpp_message.cc')
-rw-r--r-- | src/google/protobuf/compiler/cpp/cpp_message.cc | 26 |
1 files changed, 22 insertions, 4 deletions
diff --git a/src/google/protobuf/compiler/cpp/cpp_message.cc b/src/google/protobuf/compiler/cpp/cpp_message.cc index af409c29..8304ebbd 100644 --- a/src/google/protobuf/compiler/cpp/cpp_message.cc +++ b/src/google/protobuf/compiler/cpp/cpp_message.cc @@ -1772,6 +1772,17 @@ GenerateShutdownCode(io::Printer* printer) { void MessageGenerator:: GenerateClassMethods(io::Printer* printer) { + // mutable_unknown_fields wrapper function for LazyStringOutputStream + // callback. + if (!UseUnknownFieldSet(descriptor_->file())) { + printer->Print( + "static ::std::string* MutableUnknownFieldsFor$classname$(\n" + " $classname$* ptr) {\n" + " return ptr->mutable_unknown_fields();\n" + "}\n" + "\n", + "classname", classname_); + } if (IsAnyMessage(descriptor_)) { printer->Print( "void $classname$::PackFrom(const ::google::protobuf::Message& message) {\n" @@ -2814,7 +2825,9 @@ GenerateMergeFrom(io::Printer* printer) { "}\n"); } else { printer->Print( - "mutable_unknown_fields()->append(from.unknown_fields());\n"); + "if (!from.unknown_fields().empty()) {\n" + " mutable_unknown_fields()->append(from.unknown_fields());\n" + "}\n"); } } @@ -2889,11 +2902,16 @@ GenerateMergeFromCodedStream(io::Printer* printer) { "classname", classname_); if (!UseUnknownFieldSet(descriptor_->file())) { + // Use LazyStringOutputString to avoid initializing unknown fields string + // unless it is actually needed. For the same reason, disable eager refresh + // on the CodedOutputStream. printer->Print( - " ::google::protobuf::io::StringOutputStream unknown_fields_string(\n" - " mutable_unknown_fields());\n" + " ::google::protobuf::io::LazyStringOutputStream unknown_fields_string(\n" + " ::google::protobuf::internal::NewPermanentCallback(\n" + " &MutableUnknownFieldsFor$classname$, this));\n" " ::google::protobuf::io::CodedOutputStream unknown_fields_stream(\n" - " &unknown_fields_string);\n"); + " &unknown_fields_string, false);\n", + "classname", classname_); } printer->Print( |