From 1b71db1180953e592e134ddd509f54d6024df593 Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 12 Jun 2015 11:19:50 +0100 Subject: Optimization of CalculateSize: avoid foreach over empty lists. --- .../protobuf/compiler/csharp/csharp_repeated_enum_field.cc | 6 +----- .../protobuf/compiler/csharp/csharp_repeated_message_field.cc | 6 ++++-- .../compiler/csharp/csharp_repeated_primitive_field.cc | 10 +++++----- 3 files changed, 10 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc index 15e92ab9..3fe5ab21 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc @@ -89,13 +89,11 @@ void RepeatedEnumFieldGenerator::GenerateSerializationCode(io::Printer* printer) void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer) { // TODO(jonskeet): Move all this code into CodedOutputStream? It's a lot to repeat everywhere... - printer->Print("{\n"); - printer->Indent(); printer->Print( variables_, - "int dataSize = 0;\n" "if ($name$_.Count > 0) {\n"); printer->Indent(); + printer->Print("int dataSize = 0;\n"); printer->Print( variables_, "foreach ($type_name$ element in $name$_) {\n" @@ -115,8 +113,6 @@ void RepeatedEnumFieldGenerator::GenerateSerializedSizeCode(io::Printer* printer } printer->Outdent(); printer->Print("}\n"); - printer->Outdent(); - printer->Print("}\n"); } void RepeatedEnumFieldGenerator::WriteHash(io::Printer* printer) { diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc index 6228aa67..d7e4d80f 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc @@ -90,8 +90,10 @@ void RepeatedMessageFieldGenerator::GenerateSerializedSizeCode(io::Printer* prin // TODO(jonskeet): Put this into CodedOutputStream. printer->Print( variables_, - "foreach ($type_name$ element in $name$_) {\n" - " size += pb::CodedOutputStream.ComputeMessageSize($number$, element);\n" + "if ($name$_.Count > 0) {\n" + " foreach ($type_name$ element in $name$_) {\n" + " size += pb::CodedOutputStream.ComputeMessageSize($number$, element);\n" + " }\n" "}\n"); } diff --git a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc index f62ea09d..2dd879fe 100644 --- a/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc +++ b/src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc @@ -93,7 +93,9 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializationCode( void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( io::Printer* printer) { // TODO(jonskeet): Do this in the runtime if possible. It's a pain, but it must be feasible... - printer->Print("{\n"); + printer->Print( + "if ($name$_.Count > 0) {\n", + "name", name()); printer->Indent(); printer->Print("int dataSize = 0;\n"); int fixedSize = GetFixedSize(descriptor_->type()); @@ -112,10 +114,8 @@ void RepeatedPrimitiveFieldGenerator::GenerateSerializedSizeCode( int tagSize = internal::WireFormat::TagSize(descriptor_->number(), descriptor_->type()); if (descriptor_->is_packed()) { printer->Print( - "if ($name$_.Count != 0) {\n" - " size += $tag_size$ + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);\n" - "}\n", - "name", name(), "tag_size", SimpleItoa(tagSize)); + "size += $tag_size$ + pb::CodedOutputStream.ComputeInt32SizeNoTag(dataSize);\n", + "tag_size", SimpleItoa(tagSize)); } else { printer->Print( "size += $tag_size$ * $name$_.Count;\n", -- cgit v1.2.3