Move Count checks into the serialization code

I wouldn't expect this to affect anything, but it appears to.
pull/515/head
Jon Skeet 10 years ago
parent 5685e98711
commit 5a33827eec
  1. 6
      src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc
  2. 4
      src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc
  3. 15
      src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc

@ -80,11 +80,11 @@ void RepeatedEnumFieldGenerator::GenerateParsingCode(io::Printer* printer) {
} }
void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) {
// TODO(jonskeet): Originally, this checked for Count > 0 first.
// The Write* call should make that cheap though - no need to generate it every time.
printer->Print( printer->Print(
variables_, variables_,
"output.Write$packed$EnumArray($number$, $name$_);\n"); "if ($name$_.Count > 0) {\n"
" output.Write$packed$EnumArray($number$, $name$_);\n"
"}\n");
} }
void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {

@ -83,7 +83,9 @@ void RepeatedMessageFieldGenerator::GenerateSerializationCode(io::Printer* print
// The Write* call should make that cheap though - no need to generate it every time. // The Write* call should make that cheap though - no need to generate it every time.
printer->Print( printer->Print(
variables_, variables_,
"output.WriteMessageArray($number$, $name$_);\n"); "if ($name$_.Count > 0) {\n"
" output.WriteMessageArray($number$, $name$_);\n"
"}\n");
} }
void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) {

@ -49,6 +49,7 @@ namespace csharp {
RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator( RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
const FieldDescriptor* descriptor, int fieldOrdinal) const FieldDescriptor* descriptor, int fieldOrdinal)
: FieldGeneratorBase(descriptor, fieldOrdinal) { : FieldGeneratorBase(descriptor, fieldOrdinal) {
variables_["packed"] = descriptor->is_packed() ? "Packed" : "";
} }
RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() { RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {
@ -79,15 +80,11 @@ void RepeatedPrimitiveFieldGenerator::GenerateParsingCode(io::Printer* printer)
void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode(
io::Printer* printer) { io::Printer* printer) {
// TODO(jonskeet): Originally, this checked for Count > 0 first. printer->Print(
// The Write* call should make that cheap though - no need to generate it every time. variables_,
if (descriptor_->is_packed()) { "if ($name$_.Count > 0) {\n"
printer->Print(variables_, " output.Write$packed$$capitalized_type_name$Array($number$, $name$_);\n"
"output.WritePacked$capitalized_type_name$Array($number$, $name$_);\n"); "}\n");
} else {
printer->Print(variables_,
"output.Write$capitalized_type_name$Array($number$, $name$_);\n");
}
} }
void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode(

Loading…
Cancel
Save