Remove unnecessary allocation during String field emptiness checks (#7526)

pull/8714/head
Stanislav Kashirin 4 years ago committed by GitHub
parent 4644980d81
commit 37b18c5f12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 8
      java/core/src/main/java/com/google/protobuf/GeneratedMessageV3.java
  2. 4
      src/google/protobuf/compiler/java/java_string_field.cc

@ -3068,6 +3068,14 @@ public abstract class GeneratedMessageV3 extends AbstractMessage
return (Extension<MessageType, T>) extension;
}
protected static boolean isStringEmpty(final Object value) {
if (value instanceof String) {
return ((String) value).isEmpty();
} else {
return ((ByteString) value).isEmpty();
}
}
protected static int computeStringSize(final int fieldNumber, final Object value) {
if (value instanceof String) {
return CodedOutputStream.computeStringSize(fieldNumber, (String) value);

@ -80,6 +80,8 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
" if (value == null) {\n"
" throw new NullPointerException();\n"
" }\n";
(*variables)["isStringEmpty"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() + ".isStringEmpty";
(*variables)["writeString"] = "com.google.protobuf.GeneratedMessage" +
GeneratedCodeVersionSuffix() + ".writeString";
(*variables)["computeStringSize"] = "com.google.protobuf.GeneratedMessage" +
@ -117,7 +119,7 @@ void SetPrimitiveVariables(const FieldDescriptor* descriptor,
(*variables)["clear_has_field_bit_builder"] = "";
(*variables)["is_field_present_message"] =
"!get" + (*variables)["capitalized_name"] + "Bytes().isEmpty()";
"!" + (*variables)["isStringEmpty"] + "(" + (*variables)["name"] + "_)";
}
// For repeated builders, one bit is used for whether the array is immutable.

Loading…
Cancel
Save