Add hasbit to proto3 fields even when 'optional' is missing.

PiperOrigin-RevId: 516687577
pull/12221/head
Mike Kruskal 2 years ago committed by Copybara-Service
parent 309c50e8f3
commit 1d7bda4a78
  1. 6
      src/google/protobuf/compiler/java/message_field.cc
  2. 14
      src/google/protobuf/descriptor.cc

@ -322,8 +322,10 @@ void ImmutableMessageFieldGenerator::GenerateBuilderMembers(
"$name$Builder_.mergeFrom(value);\n",
"$set_has_field_bit_builder$\n"
"$on_changed$\n"
"if ($name$_ != null) {\n"
" $set_has_field_bit_builder$\n"
" $on_changed$\n"
"}\n"
"return this;\n");
// Message.Builder clearField()

@ -8426,19 +8426,7 @@ bool HasPreservingUnknownEnumSemantics(const FieldDescriptor* field) {
}
bool HasHasbit(const FieldDescriptor* field) {
// This predicate includes proto3 message fields only if they have "optional".
// Foo submsg1 = 1; // HasHasbit() == false
// optional Foo submsg2 = 2; // HasHasbit() == true
// This is slightly odd, as adding "optional" to a singular proto3 field does
// not change the semantics or API. However whenever any field in a message
// has a hasbit, it forces reflection to include hasbit offsets for *all*
// fields, even if almost all of them are set to -1 (no hasbit). So to avoid
// causing a sudden size regression for ~all proto3 messages, we give proto3
// message fields a hasbit only if "optional" is present. If the user is
// explicitly writing "optional", it is likely they are writing it on
// primitive fields also.
return (FieldDescriptorLegacy(field).has_optional_keyword() ||
field->is_required()) &&
return field->has_presence() && !field->real_containing_oneof() &&
!field->options().weak();
}

Loading…
Cancel
Save