add hasbit to proto3 fields even when 'optional' is missing

PiperOrigin-RevId: 513956259
pull/12142/head
Eric Salo 2 years ago committed by Copybara-Service
parent 63895855d7
commit 208f0ad455
  1. 13
      src/google/protobuf/descriptor.cc

@ -8417,18 +8417,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 (field->has_optional_keyword() || field->is_required()) &&
return field->has_presence() && !field->real_containing_oneof() &&
!field->options().weak();
}

Loading…
Cancel
Save