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

This commit is contained in:
Stanislav Kashirin 2021-06-08 23:48:00 +03:00 committed by GitHub
parent 4644980d81
commit 37b18c5f12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 1 deletions

View File

@ -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);

View File

@ -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.