diff options
author | Ulas Kirazci <ulas@google.com> | 2013-07-31 23:11:08 +0000 |
---|---|---|
committer | Gerrit Code Review <noreply-gerritcodereview@google.com> | 2013-07-31 23:11:08 +0000 |
commit | 87c8e31b8c0baa78f979863a9ed10654a1cdca1f (patch) | |
tree | 779734f1949ccb91200511e602ba953c48bddd90 /src/google/protobuf | |
parent | be47f55e2f93df84abb55759e49cf381d2745cc5 (diff) | |
parent | 14dd1f02ac95f61087e311bf61c85047fb218a29 (diff) | |
download | external_protobuf-87c8e31b8c0baa78f979863a9ed10654a1cdca1f.zip external_protobuf-87c8e31b8c0baa78f979863a9ed10654a1cdca1f.tar.gz external_protobuf-87c8e31b8c0baa78f979863a9ed10654a1cdca1f.tar.bz2 |
Merge "Fixed packed repeated serialization."
Diffstat (limited to 'src/google/protobuf')
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_primitive_field.cc | 57 | ||||
-rw-r--r-- | src/google/protobuf/compiler/javanano/javanano_primitive_field.h | 2 |
2 files changed, 29 insertions, 30 deletions
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc index 20ea6b0..79357e9 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc @@ -409,10 +409,6 @@ void RepeatedPrimitiveFieldGenerator:: GenerateMembers(io::Printer* printer) const { printer->Print(variables_, "public $type$[] $name$ = $default$;\n"); - if (descriptor_->options().packed()) { - printer->Print(variables_, - "private int $name$MemoizedSerializedSize;\n"); - } } void RepeatedPrimitiveFieldGenerator:: @@ -462,12 +458,33 @@ GenerateParsingCode(io::Printer* printer) const { } void RepeatedPrimitiveFieldGenerator:: +GenerateRepeatedDataSizeCode(io::Printer* printer) const { + // Creates a variable dataSize and puts the serialized size in + // there. + if (FixedSize(descriptor_->type()) == -1) { + printer->Print(variables_, + "int dataSize = 0;\n" + "for ($type$ element : this.$name$) {\n" + " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n" + " .compute$capitalized_type$SizeNoTag(element);\n" + "}\n"); + } else { + printer->Print(variables_, + "int dataSize = $fixed_size$ * this.$name$.length;\n"); + } +} + +void RepeatedPrimitiveFieldGenerator:: GenerateSerializationCode(io::Printer* printer) const { if (descriptor_->options().packed()) { printer->Print(variables_, - "if (this.$name$.length > 0) {\n" + "if (this.$name$.length > 0) {\n"); + printer->Indent(); + GenerateRepeatedDataSizeCode(printer); + printer->Outdent(); + printer->Print(variables_, " output.writeRawVarint32($tag$);\n" - " output.writeRawVarint32($name$MemoizedSerializedSize);\n" + " output.writeRawVarint32(dataSize);\n" "}\n"); printer->Print(variables_, "for ($type$ element : this.$name$) {\n" @@ -487,27 +504,15 @@ GenerateSerializedSizeCode(io::Printer* printer) const { "if (this.$name$.length > 0) {\n"); printer->Indent(); - if (FixedSize(descriptor_->type()) == -1) { - printer->Print(variables_, - "int dataSize = 0;\n" - "for ($type$ element : this.$name$) {\n" - " dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n" - " .compute$capitalized_type$SizeNoTag(element);\n" - "}\n"); - } else { - printer->Print(variables_, - "int dataSize = $fixed_size$ * this.$name$.length;\n"); - } + GenerateRepeatedDataSizeCode(printer); printer->Print( "size += dataSize;\n"); if (descriptor_->options().packed()) { - // cache the data size for packed fields. printer->Print(variables_, "size += $tag_size$;\n" "size += com.google.protobuf.nano.CodedOutputByteBufferNano\n" - " .computeRawVarint32Size(dataSize);\n" - "$name$MemoizedSerializedSize = dataSize;\n"); + " .computeRawVarint32Size(dataSize);\n"); } else { printer->Print(variables_, "size += $tag_size$ * this.$name$.length;\n"); @@ -515,16 +520,8 @@ GenerateSerializedSizeCode(io::Printer* printer) const { printer->Outdent(); - // set cached size to 0 for empty packed fields. - if (descriptor_->options().packed()) { - printer->Print(variables_, - "} else {\n" - " $name$MemoizedSerializedSize = 0;\n" - "}\n"); - } else { - printer->Print( - "}\n"); - } + printer->Print( + "}\n"); } string RepeatedPrimitiveFieldGenerator::GetBoxedType() const { diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h index aa6355b..a56082d 100644 --- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h +++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h @@ -80,6 +80,8 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator { string GetBoxedType() const; private: + void GenerateRepeatedDataSizeCode(io::Printer* printer) const; + const FieldDescriptor* descriptor_; map<string, string> variables_; |