aboutsummaryrefslogtreecommitdiff
path: root/src/google
diff options
context:
space:
mode:
authorJon Skeet <skeet@pobox.com>2015-06-12 11:19:50 +0100
committerJon Skeet <skeet@pobox.com>2015-06-12 11:19:50 +0100
commit1b71db1180953e592e134ddd509f54d6024df593 (patch)
tree66a88850f0b176802431329b9870b00858e41cfc /src/google
parent9b66768e257dd8e61895d8a2c40f078f9987b577 (diff)
downloadprotobuf-1b71db1180953e592e134ddd509f54d6024df593.tar.gz
protobuf-1b71db1180953e592e134ddd509f54d6024df593.tar.bz2
protobuf-1b71db1180953e592e134ddd509f54d6024df593.zip
Optimization of CalculateSize: avoid foreach over empty lists.
Diffstat (limited to 'src/google')
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_repeated_enum_field.cc6
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_repeated_message_field.cc6
-rw-r--r--src/google/protobuf/compiler/csharp/csharp_repeated_primitive_field.cc10
3 files changed, 10 insertions, 12 deletions
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",