Skip to content

Commit

Permalink
Move has bit machinations from field to message in prep for switching…
Browse files Browse the repository at this point in the history
… to emit calls.

PiperOrigin-RevId: 531820191
  • Loading branch information
fowles authored and Copybara-Service committed May 14, 2023
1 parent 02d133c commit e6adc6d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
7 changes: 0 additions & 7 deletions src/google/protobuf/compiler/cpp/field.cc
Original file line number Diff line number Diff line change
Expand Up @@ -145,13 +145,6 @@ void FieldGeneratorBase::GenerateCopyConstructorCode(io::Printer* p) const {
}
}

void FieldGeneratorBase::GenerateIfHasField(io::Printer* p) const {
ABSL_CHECK(internal::cpp::HasHasbit(descriptor_));

Formatter format(p);
format("if (($has_hasbit$) != 0) {\n");
}

namespace {
std::unique_ptr<FieldGeneratorBase> MakeGenerator(const FieldDescriptor* field,
const Options& options,
Expand Down
8 changes: 0 additions & 8 deletions src/google/protobuf/compiler/cpp/field.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,8 +120,6 @@ class FieldGeneratorBase {

virtual void GenerateIsInitialized(io::Printer* p) const {}

virtual void GenerateIfHasField(io::Printer* p) const;

virtual bool IsInlined() const { return false; }

virtual ArenaDtorNeeds NeedsArenaDestructor() const {
Expand Down Expand Up @@ -359,12 +357,6 @@ class FieldGenerator {
impl_->GenerateIsInitialized(p);
}

// TODO(b/245791219): Document this properly.
void GenerateIfHasField(io::Printer* p) const {
auto vars = PushVarsForCall(p);
impl_->GenerateIfHasField(p);
}

// TODO(b/245791219): Document this properly.
bool IsInlined() const { return impl_->IsInlined(); }

Expand Down
39 changes: 19 additions & 20 deletions src/google/protobuf/compiler/cpp/message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -521,14 +521,6 @@ absl::flat_hash_map<absl::string_view, std::string> ClassVars(
return vars;
}

absl::flat_hash_map<absl::string_view, std::string> HasbitVars(
int has_bit_index) {
return {
{"has_array_index", absl::StrCat(has_bit_index / 32)},
{"has_mask", absl::StrFormat("0x%08xu", 1u << (has_bit_index % 32))},
};
}

} // anonymous namespace

// ===================================================================
Expand Down Expand Up @@ -605,6 +597,16 @@ size_t MessageGenerator::InlinedStringDonatedSize() const {
return (max_inlined_string_index_ + 31) / 32;
}

absl::flat_hash_map<absl::string_view, std::string>
MessageGenerator::HasBitVars(const FieldDescriptor* field) const {
int has_bit_index = HasBitIndex(field);
ABSL_CHECK_NE(has_bit_index, kNoHasbit);
return {
{"has_array_index", absl::StrCat(has_bit_index / 32)},
{"has_mask", absl::StrFormat("0x%08xu", 1u << (has_bit_index % 32))},
};
}

int MessageGenerator::HasBitIndex(const FieldDescriptor* field) const {
return has_bit_indices_.empty() ? kNoHasbit
: has_bit_indices_[field->index()];
Expand Down Expand Up @@ -973,10 +975,7 @@ void MessageGenerator::GenerateSingularFieldHasBits(
return;
}
if (HasHasbit(field)) {
int has_bit_index = HasBitIndex(field);
ABSL_CHECK_NE(has_bit_index, kNoHasbit);

auto v = p->WithVars(HasbitVars(has_bit_index));
auto v = p->WithVars(HasBitVars(field));
p->Emit(
{Sub{"ASSUME",
[&] {
Expand Down Expand Up @@ -1102,8 +1101,7 @@ void MessageGenerator::GenerateFieldClear(const FieldDescriptor* field,
}
field_generators_.get(field).GenerateClearingCode(p);
if (HasHasbit(field)) {
int has_bit_index = HasBitIndex(field);
auto v = p->WithVars(HasbitVars(has_bit_index));
auto v = p->WithVars(HasBitVars(field));
p->Emit(R"cc(
$has_bits$[$has_array_index$] &= ~$has_mask$;
)cc");
Expand Down Expand Up @@ -3455,8 +3453,7 @@ void MessageGenerator::GenerateClassSpecificMergeImpl(io::Printer* p) {
} else if (field->options().weak() ||
cached_has_word_index != HasWordIndex(field)) {
// Check hasbit, not using cached bits.
ABSL_CHECK(HasHasbit(field));
auto v = p->WithVars(HasbitVars(HasBitIndex(field)));
auto v = p->WithVars(HasBitVars(field));
format(
"if ((from.$has_bits$[$has_array_index$] & $has_mask$) != 0) {\n");
format.Indent();
Expand Down Expand Up @@ -3654,11 +3651,11 @@ void MessageGenerator::GenerateSerializeOneField(io::Printer* p,
} else if (HasHasbit(field)) {
// Attempt to use the state of cached_has_bits, if possible.
int has_bit_index = HasBitIndex(field);
auto v = p->WithVars(HasbitVars(has_bit_index));
auto v = p->WithVars(HasBitVars(field));
if (cached_has_bits_index == has_bit_index / 32) {
format("if (cached_has_bits & $has_mask$) {\n");
} else {
field_generators_.get(field).GenerateIfHasField(p);
format("if (($has_bits$[$has_array_index$] & $has_mask$) != 0) {\n");
}

have_enclosing_if = true;
Expand Down Expand Up @@ -4089,7 +4086,8 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
for (auto field : optimized_order_) {
if (field->is_required()) {
format("\n");
field_generators_.get(field).GenerateIfHasField(p);
auto v = p->WithVars(HasBitVars(field));
format("if (($has_bits$[$has_array_index$] & $has_mask$) != 0) {\n");
format.Indent();
PrintFieldComment(format, field);
field_generators_.get(field).GenerateByteSize(p);
Expand Down Expand Up @@ -4146,7 +4144,8 @@ void MessageGenerator::GenerateByteSize(io::Printer* p) {
for (auto field : optimized_order_) {
if (!field->is_required()) continue;
PrintFieldComment(format, field);
field_generators_.get(field).GenerateIfHasField(p);
auto v = p->WithVars(HasBitVars(field));
format("if (($has_bits$[$has_array_index$] & $has_mask$) != 0) {\n");
format.Indent();
field_generators_.get(field).GenerateByteSize(p);
format.Outdent();
Expand Down
2 changes: 2 additions & 0 deletions src/google/protobuf/compiler/cpp/message.h
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ class MessageGenerator {

size_t HasBitsSize() const;
size_t InlinedStringDonatedSize() const;
absl::flat_hash_map<absl::string_view, std::string> HasBitVars(
const FieldDescriptor* field) const;
int HasBitIndex(const FieldDescriptor* field) const;
int HasByteIndex(const FieldDescriptor* field) const;
int HasWordIndex(const FieldDescriptor* field) const;
Expand Down

0 comments on commit e6adc6d

Please sign in to comment.