Improve Clear codegen.

This:
- eliminates the "equivalence" predicate passed to CollectFields, which wasn't transitive, commutative, maintained the state and wasn't an equivalence operator
- unconditionally zeroes the zero-defaulted primitives (even if they're longer than 4 bytes)
- avoid wrapping primitive-only chnks with BatchCheckHasBit
- avoid BatchCheckHasBit checks with only one CheckHasBit in its body
PiperOrigin-RevId: 1000704772
diff --git a/src/google/protobuf/compiler/cpp/message.cc b/src/google/protobuf/compiler/cpp/message.cc
index 260034d..5a9e56c 100644
--- a/src/google/protobuf/compiler/cpp/message.cc
+++ b/src/google/protobuf/compiler/cpp/message.cc
@@ -3395,40 +3395,74 @@
 }
 
 void MessageGenerator::EmitClearChunks(io::Printer* p, bool is_split) {
-  // The maximum number of bytes we will memset to zero without checking their
-  // hasbit to see if a zero-init is necessary.
-  const int kMaxUnconditionalPrimitiveBytesClear = 4;
+  // It's faster to just overwrite primitive types, but we should only
+  // clear strings and messages if they were set.
+  //
+  // TODO:  Let the CppFieldGenerator decide this somehow.
+  const auto emit_memset = [&](const FieldDescriptor* start,
+                               const FieldDescriptor* end) {
+    p->Emit(
+        {
+            {"Impl", is_split ? "Impl_::Split" : "Impl_"},
+            {"start", FieldMemberName(start, is_split)},
+            {"start_field", FieldName(start)},
+            {"end_field", FieldName(end)},
+        },
+        R"cc(
+          ::memset(&this_.$start$, 0,
+                   PROTOBUF_FIELD_OFFSET($Impl$, $end_field$_) -
+                       PROTOBUF_FIELD_OFFSET($Impl$, $start_field$_) +
+                       sizeof($Impl$::$end_field$_));
+        )cc");
+  };
+  const auto emit_clear_field = [&](const FieldDescriptor* field) {
+    field_generators_.get(field).GenerateMessageClearingCode(p);
+  };
+  const auto needs_enclosing_if = [&](const FieldDescriptor* f) {
+    return ShouldGenerateEnclosingIf(*f);
+  };
+
+  std::vector<const FieldDescriptor*> fields;
+  absl::c_copy_if(field_layout_.optimized_order(), std::back_inserter(fields),
+                  [&](const FieldDescriptor* f) {
+                    return ShouldSplit(f, options_) == is_split;
+                  });
+
+  // Group zero-initializable and non-zero-initializable
+  // spans of fields without hasbits.
+  std::vector<FieldChunk> chunks_without_enclosed_ifs = CollectFields(
+      fields, options_,
+      [](const FieldDescriptor* a, const FieldDescriptor* b) {
+        return CanClearByZeroing(a) == CanClearByZeroing(b);
+      },
+      std::not_fn(needs_enclosing_if));
+
+  for (const FieldChunk& chunk : chunks_without_enclosed_ifs) {
+    if (CanClearByZeroing(chunk.fields.front()) && chunk.fields.size() > 1) {
+      emit_memset(chunk.fields.front(), chunk.fields.back());
+    } else {
+      for (const FieldDescriptor* field : chunk.fields) {
+        emit_clear_field(field);
+      }
+    }
+  }
 
   // Collect fields into chunks. Each chunk may have an if() condition that
   // checks all hasbits in the chunk and skips it if none are set.
-  int zero_init_bytes = 0;
-  for (const auto& field : field_layout_.optimized_order()) {
-    if (CanClearByZeroing(field)) {
-      zero_init_bytes += EstimateAlignmentSize(field);
-    }
-  }
-  bool merge_zero_init = zero_init_bytes > kMaxUnconditionalPrimitiveBytesClear;
-  int chunk_count = 0;
-
-  std::vector<FieldChunk> chunks = CollectFields(
-      field_layout_.optimized_order(), options_,
+  std::vector<const FieldDescriptor*> enclosing_if_fields;
+  absl::c_copy_if(fields, std::back_inserter(enclosing_if_fields),
+                  needs_enclosing_if);
+  std::vector<FieldChunk> enclosing_if_chunks = CollectFields(
+      enclosing_if_fields, options_,
       [&](const FieldDescriptor* a, const FieldDescriptor* b) -> bool {
-        chunk_count++;
-        // This predicate guarantees that there is only a single zero-init
-        // (memset) per chunk, and if present it will be at the beginning.
-        bool same =
-            field_layout_.GetHasWordIndex(a) ==
-                field_layout_.GetHasWordIndex(b) &&
-            (CanClearByZeroing(a) == CanClearByZeroing(b) ||
-             (CanClearByZeroing(a) && (chunk_count == 1 || merge_zero_init)));
-        if (!same) chunk_count = 0;
-        return same;
-      },
-      [&](auto* f) { return ShouldSplit(f, options_) == is_split; });
-  chunks = PartitionToMinimizeExpectedBranches(std::move(chunks), options_);
+        return field_layout_.GetHasWordIndex(a) ==
+               field_layout_.GetHasWordIndex(b);
+      });
+  enclosing_if_chunks = PartitionToMinimizeExpectedBranches(
+      std::move(enclosing_if_chunks), options_);
 
-  auto it = chunks.begin();
-  auto end = chunks.end();
+  auto it = enclosing_if_chunks.begin();
+  auto end = enclosing_if_chunks.end();
   int cached_has_word_index = -1;
 
   while (it != end) {
@@ -3439,106 +3473,39 @@
     while (it != next) {
       const std::vector<const FieldDescriptor*>& fields = it->fields;
 
-      const FieldDescriptor* memset_start = nullptr;
-      const FieldDescriptor* memset_end = nullptr;
-      bool saw_non_zero_init = false;
-
-      for (const auto& field : fields) {
-        if (CanClearByZeroing(field)) {
-          ABSL_CHECK(!saw_non_zero_init);
-          if (!memset_start) memset_start = field;
-          memset_end = field;
-        } else {
-          saw_non_zero_init = true;
-        }
+      const int has_word_index =
+          field_layout_.GetHasWordIndex(fields.front()).value();
+      if (cached_has_word_index != has_word_index) {
+        cached_has_word_index = has_word_index;
+        p->Emit({{"cached_has_word_index", cached_has_word_index}}, R"cc(
+          cached_has_bits = this_.$has_bits$[$cached_has_word_index$];
+        )cc");
       }
 
-      const auto maybe_emit_update_cached_hasbits = [&](int has_word_index) {
-        if (cached_has_word_index != has_word_index) {
-          cached_has_word_index = has_word_index;
-          p->Emit({{"cached_has_word_index", cached_has_word_index}}, R"cc(
-            cached_has_bits = this_.$has_bits$[$cached_has_word_index$];
-          )cc");
-        }
-      };
-
       const auto print_clear_field_chunk = [&] {
-        if (memset_start) {
-          if (memset_start == memset_end) {
-            // For clarity, do not memset a single field.
-            field_generators_.get(memset_start).GenerateMessageClearingCode(p);
-          } else {
-            p->Emit(
-                {
-                    {"start", FieldMemberName(memset_start, is_split)},
-                    {"end", FieldMemberName(memset_end, is_split)},
-                },
-                R"cc(
-                  ::memset(&this_.$start$, 0,
-                           static_cast<::size_t>(
-                               reinterpret_cast<char*>(&this_.$end$) -
-                               reinterpret_cast<char*>(&this_.$start$)) +
-                               sizeof($end$));
-                )cc");
-          }
-        }
-
-        // Clear all non-zero-initializable fields in the chunk.
         for (const auto& field : fields) {
-          if (CanClearByZeroing(field)) continue;
-          // It's faster to just overwrite primitive types, but we should only
-          // clear strings and messages if they were set.
-          //
-          // TODO:  Let the CppFieldGenerator decide this somehow.
-          const bool have_enclosing_if = ShouldGenerateEnclosingIf(*field);
-
-          auto print_clear_field = [&] {
-            field_generators_.get(field).GenerateMessageClearingCode(p);
-          };
-
-          if (have_enclosing_if) {
-            absl::optional<int> has_word_index =
-                field_layout_.GetHasWordIndex(field);
-            if (has_word_index.has_value()) {
-              maybe_emit_update_cached_hasbits(*has_word_index);
-            }
-
-            p->Emit({{"cond",
-                      [&] {
-                        PrintPresenceCheckCondition(field, field_layout_, p,
-                                                    options_);
-                      }},
-                     {"clear_field", print_clear_field}},
-                    R"cc(
-                      if ($cond$) {
-                        $clear_field$;
-                      }
-                    )cc");
-          } else {
-            print_clear_field();
-          }
+          p->Emit({{"cond",
+                    [&] {
+                      PrintPresenceCheckCondition(field, field_layout_, p,
+                                                  options_);
+                    }},
+                   {"clear_field", [&] { emit_clear_field(field); }}},
+                  R"cc(
+                    if ($cond$) {
+                      $clear_field$;
+                    }
+                  )cc");
         }
       };
 
       // Whether we wrap this chunk in:
-      //   if (cached_has_bits & <chunk hasbits) { /* chunk. */ }
-      // We can omit the if() for chunk size 1, or if our fields do not have
-      // hasbits. I don't understand the rationale for the last part of the
-      // condition, but it matches the old logic.
-      const bool check_has_word =
-          field_layout_.GetHasBitIndex(fields.front()).has_value() &&
-          fields.size() > 1 && (memset_end != fields.back() || merge_zero_init);
-
-      if (check_has_word) {
+      //   if (cached_has_bits & <chunk hasbits>) { /* chunk. */ }
+      if (fields.size() > 1) {
         // Emit an if() that will let us skip the whole chunk if none are set.
         uint32_t chunk_mask = GenChunkMask(fields, field_layout_);
 
         ABSL_DCHECK_GE(absl::popcount(chunk_mask), 2);
 
-        const int has_word_index =
-            field_layout_.GetHasWordIndex(fields.front()).value();
-        maybe_emit_update_cached_hasbits(has_word_index);
-
         const std::string cond = GenerateConditionMaybeWithProbabilityForGroup(
             chunk_mask, fields, options_);
         p->Emit(
diff --git a/src/google/protobuf/compiler/java/java_features.pb.cc b/src/google/protobuf/compiler/java/java_features.pb.cc
index f6f0e2f..d4f4dc8 100644
--- a/src/google/protobuf/compiler/java/java_features.pb.cc
+++ b/src/google/protobuf/compiler/java/java_features.pb.cc
@@ -485,14 +485,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
-    ::memset(&this_._impl_.utf8_validation_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.nest_in_file_class_) -
-                 reinterpret_cast<char*>(&this_._impl_.utf8_validation_)) +
-                 sizeof(_impl_.nest_in_file_class_));
-  }
+  ::memset(&this_._impl_.utf8_validation_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, nest_in_file_class_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, utf8_validation_) +
+               sizeof(Impl_::nest_in_file_class_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
diff --git a/src/google/protobuf/compiler/plugin.pb.cc b/src/google/protobuf/compiler/plugin.pb.cc
index 60b23fc..ee9e553 100644
--- a/src/google/protobuf/compiler/plugin.pb.cc
+++ b/src/google/protobuf/compiler/plugin.pb.cc
@@ -827,17 +827,14 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.major_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, patch_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, major_) +
+               sizeof(Impl_::patch_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (CheckHasBit(cached_has_bits, 0x00000001U)) {
     this_._impl_.suffix_.ClearNonDefaultToEmpty();
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000000eU)) {
-    ::memset(&this_._impl_.major_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.patch_) -
-                 reinterpret_cast<char*>(&this_._impl_.major_)) +
-                 sizeof(_impl_.patch_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -1728,6 +1725,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.supported_features_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, maximum_edition_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, supported_features_) +
+               sizeof(Impl_::maximum_edition_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -1737,13 +1738,6 @@
       this_._impl_.error_.ClearNonDefaultToEmpty();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000001cU)) {
-    ::memset(&this_._impl_.supported_features_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.maximum_edition_) -
-                 reinterpret_cast<char*>(&this_._impl_.supported_features_)) +
-                 sizeof(_impl_.maximum_edition_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
diff --git a/src/google/protobuf/cpp_features.pb.cc b/src/google/protobuf/cpp_features.pb.cc
index afcf67a..73f768d 100644
--- a/src/google/protobuf/cpp_features.pb.cc
+++ b/src/google/protobuf/cpp_features.pb.cc
@@ -324,14 +324,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
-    ::memset(&this_._impl_.string_type_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.repeated_type_) -
-                 reinterpret_cast<char*>(&this_._impl_.string_type_)) +
-                 sizeof(_impl_.repeated_type_));
-  }
+  ::memset(&this_._impl_.string_type_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, repeated_type_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, string_type_) +
+               sizeof(Impl_::repeated_type_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
diff --git a/src/google/protobuf/descriptor.pb.cc b/src/google/protobuf/descriptor.pb.cc
index d195105..39f9aa4 100644
--- a/src/google/protobuf/descriptor.pb.cc
+++ b/src/google/protobuf/descriptor.pb.cc
@@ -7066,6 +7066,7 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.edition_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00001fffU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -7110,7 +7111,6 @@
       this_._impl_.source_code_info_->Clear();
     }
   }
-  this_._impl_.edition_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -7604,18 +7604,15 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.start_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, end_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, start_) +
+               sizeof(Impl_::end_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (CheckHasBit(cached_has_bits, 0x00000001U)) {
     ABSL_DCHECK(this_._impl_.options_ != nullptr);
     this_._impl_.options_->Clear();
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x00000006U)) {
-    ::memset(&this_._impl_.start_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.end_) -
-                 reinterpret_cast<char*>(&this_._impl_.start_)) +
-                 sizeof(_impl_.end_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -7846,14 +7843,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
-    ::memset(&this_._impl_.start_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.end_) -
-                 reinterpret_cast<char*>(&this_._impl_.start_)) +
-                 sizeof(_impl_.end_));
-  }
+  ::memset(&this_._impl_.start_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, end_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, start_) +
+               sizeof(Impl_::end_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -8165,6 +8158,7 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.visibility_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x000003ffU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -8199,7 +8193,6 @@
       this_._impl_.options_->Clear();
     }
   }
-  this_._impl_.visibility_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -8637,6 +8630,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.number_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, repeated_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, number_) +
+               sizeof(Impl_::repeated_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -8646,13 +8643,6 @@
       this_._impl_.type_.ClearNonDefaultToEmpty();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000001cU)) {
-    ::memset(&this_._impl_.number_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.repeated_) -
-                 reinterpret_cast<char*>(&this_._impl_.number_)) +
-                 sizeof(_impl_.repeated_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -8930,8 +8920,9 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  this_._impl_.verification_ = 1;
   cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
+  if (BatchCheckHasBit(cached_has_bits, 0x00000007U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
       this_._impl_.declaration_.Clear();
     }
@@ -8942,7 +8933,6 @@
       ABSL_DCHECK(this_._impl_.features_ != nullptr);
       this_._impl_.features_->Clear();
     }
-    this_._impl_.verification_ = 1;
   }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
@@ -9256,6 +9246,12 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.number_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, proto3_optional_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, number_) +
+               sizeof(Impl_::proto3_optional_));
+  this_._impl_.label_ = 1;
+  this_._impl_.type_ = 1;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x0000003fU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -9278,15 +9274,6 @@
       this_._impl_.options_->Clear();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x000007c0U)) {
-    ::memset(&this_._impl_.number_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.proto3_optional_) -
-                 reinterpret_cast<char*>(&this_._impl_.number_)) +
-                 sizeof(_impl_.proto3_optional_));
-    this_._impl_.label_ = 1;
-    this_._impl_.type_ = 1;
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -9870,14 +9857,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
-    ::memset(&this_._impl_.start_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.end_) -
-                 reinterpret_cast<char*>(&this_._impl_.start_)) +
-                 sizeof(_impl_.end_));
-  }
+  ::memset(&this_._impl_.start_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, end_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, start_) +
+               sizeof(Impl_::end_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -10129,6 +10112,7 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.visibility_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x0000001fU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -10148,7 +10132,6 @@
       this_._impl_.options_->Clear();
     }
   }
-  this_._impl_.visibility_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -10467,6 +10450,7 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.number_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -10477,7 +10461,6 @@
       this_._impl_.options_->Clear();
     }
   }
-  this_._impl_.number_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -11014,6 +10997,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.client_streaming_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, server_streaming_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, client_streaming_) +
+               sizeof(Impl_::server_streaming_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x0000000fU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -11030,11 +11017,6 @@
       this_._impl_.options_->Clear();
     }
   }
-  ::memset(&this_._impl_.client_streaming_, 0,
-           static_cast<::size_t>(
-               reinterpret_cast<char*>(&this_._impl_.server_streaming_) -
-               reinterpret_cast<char*>(&this_._impl_.client_streaming_)) +
-               sizeof(_impl_.server_streaming_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -11376,8 +11358,14 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  ::memset(&this_._impl_.java_multiple_files_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, java_string_check_utf8_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, java_multiple_files_) +
+               sizeof(Impl_::java_string_check_utf8_));
+  this_._impl_.optimize_for_ = 1;
+  this_._impl_.cc_enable_arenas_ = true;
   cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0x000007ffU)) {
+  if (BatchCheckHasBit(cached_has_bits, 0x001007ffU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
       this_._impl_.java_package_.ClearNonDefaultToEmpty();
     }
@@ -11412,15 +11400,6 @@
       ABSL_DCHECK(this_._impl_.features_ != nullptr);
       this_._impl_.features_->Clear();
     }
-  }
-  if (BatchCheckHasBit(cached_has_bits, 0x001ff800U)) {
-    ::memset(&this_._impl_.java_multiple_files_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.java_string_check_utf8_) -
-                 reinterpret_cast<char*>(&this_._impl_.java_multiple_files_)) +
-                 sizeof(_impl_.java_string_check_utf8_));
-    this_._impl_.optimize_for_ = 1;
-    this_._impl_.cc_enable_arenas_ = true;
     if (CheckHasBit(cached_has_bits, 0x00100000U)) {
       this_._impl_.uninterpreted_option_.Clear();
     }
@@ -11952,17 +11931,16 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  ::memset(&this_._impl_.message_set_wire_format_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, deprecated_legacy_json_field_conflicts_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, message_set_wire_format_) +
+               sizeof(Impl_::deprecated_legacy_json_field_conflicts_));
   cached_has_bits = this_._impl_._has_bits_[0];
-  if (CheckHasBit(cached_has_bits, 0x00000001U)) {
-    ABSL_DCHECK(this_._impl_.features_ != nullptr);
-    this_._impl_.features_->Clear();
-  }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000007eU)) {
-    ::memset(&this_._impl_.message_set_wire_format_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.deprecated_legacy_json_field_conflicts_) -
-                 reinterpret_cast<char*>(&this_._impl_.message_set_wire_format_)) +
-                 sizeof(_impl_.deprecated_legacy_json_field_conflicts_));
+  if (BatchCheckHasBit(cached_has_bits, 0x00000041U)) {
+    if (CheckHasBit(cached_has_bits, 0x00000001U)) {
+      ABSL_DCHECK(this_._impl_.features_ != nullptr);
+      this_._impl_.features_->Clear();
+    }
     if (CheckHasBit(cached_has_bits, 0x00000040U)) {
       this_._impl_.uninterpreted_option_.Clear();
     }
@@ -12264,11 +12242,11 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.edition_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (CheckHasBit(cached_has_bits, 0x00000001U)) {
     this_._impl_.value_.ClearNonDefaultToEmpty();
   }
-  this_._impl_.edition_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -12485,6 +12463,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.edition_introduced_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, edition_removed_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, edition_introduced_) +
+               sizeof(Impl_::edition_removed_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -12494,13 +12476,6 @@
       this_._impl_.removal_error_.ClearNonDefaultToEmpty();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000001cU)) {
-    ::memset(&this_._impl_.edition_introduced_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.edition_removed_) -
-                 reinterpret_cast<char*>(&this_._impl_.edition_introduced_)) +
-                 sizeof(_impl_.edition_removed_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -12813,8 +12788,12 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  ::memset(&this_._impl_.ctype_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, retention_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, ctype_) +
+               sizeof(Impl_::retention_));
   cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0x0000000fU)) {
+  if (BatchCheckHasBit(cached_has_bits, 0x0000200fU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
       this_._impl_.edition_defaults_.Clear();
     }
@@ -12829,13 +12808,6 @@
       ABSL_DCHECK(this_._impl_.feature_support_ != nullptr);
       this_._impl_.feature_support_->Clear();
     }
-  }
-  if (BatchCheckHasBit(cached_has_bits, 0x00003ff0U)) {
-    ::memset(&this_._impl_.ctype_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.retention_) -
-                 reinterpret_cast<char*>(&this_._impl_.ctype_)) +
-                 sizeof(_impl_.retention_));
     if (CheckHasBit(cached_has_bits, 0x00002000U)) {
       this_._impl_.targets_.Clear();
     }
@@ -13560,18 +13532,19 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (CheckHasBit(cached_has_bits, 0x00000001U)) {
-    ABSL_DCHECK(this_._impl_.features_ != nullptr);
-    this_._impl_.features_->Clear();
-  }
   ::memset(&this_._impl_.allow_alias_, 0,
-           static_cast<::size_t>(
-               reinterpret_cast<char*>(&this_._impl_.deprecated_legacy_json_field_conflicts_) -
-               reinterpret_cast<char*>(&this_._impl_.allow_alias_)) +
-               sizeof(_impl_.deprecated_legacy_json_field_conflicts_));
-  if (CheckHasBit(cached_has_bits, 0x00000010U)) {
-    this_._impl_.uninterpreted_option_.Clear();
+           PROTOBUF_FIELD_OFFSET(Impl_, deprecated_legacy_json_field_conflicts_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, allow_alias_) +
+               sizeof(Impl_::deprecated_legacy_json_field_conflicts_));
+  cached_has_bits = this_._impl_._has_bits_[0];
+  if (BatchCheckHasBit(cached_has_bits, 0x00000011U)) {
+    if (CheckHasBit(cached_has_bits, 0x00000001U)) {
+      ABSL_DCHECK(this_._impl_.features_ != nullptr);
+      this_._impl_.features_->Clear();
+    }
+    if (CheckHasBit(cached_has_bits, 0x00000010U)) {
+      this_._impl_.uninterpreted_option_.Clear();
+    }
   }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
@@ -13886,6 +13859,10 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  ::memset(&this_._impl_.deprecated_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, debug_redact_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, deprecated_) +
+               sizeof(Impl_::debug_redact_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000007U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -13900,11 +13877,6 @@
       this_._impl_.feature_support_->Clear();
     }
   }
-  ::memset(&this_._impl_.deprecated_, 0,
-           static_cast<::size_t>(
-               reinterpret_cast<char*>(&this_._impl_.debug_redact_) -
-               reinterpret_cast<char*>(&this_._impl_.deprecated_)) +
-               sizeof(_impl_.debug_redact_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -14223,6 +14195,7 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  this_._impl_.deprecated_ = false;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -14233,7 +14206,6 @@
       this_._impl_.features_->Clear();
     }
   }
-  this_._impl_.deprecated_ = false;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -14523,6 +14495,10 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
+  ::memset(&this_._impl_.deprecated_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, idempotency_level_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, deprecated_) +
+               sizeof(Impl_::idempotency_level_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -14533,13 +14509,6 @@
       this_._impl_.features_->Clear();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000000cU)) {
-    ::memset(&this_._impl_.deprecated_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.idempotency_level_) -
-                 reinterpret_cast<char*>(&this_._impl_.deprecated_)) +
-                 sizeof(_impl_.idempotency_level_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -14812,11 +14781,11 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.is_extension_ = false;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (CheckHasBit(cached_has_bits, 0x00000001U)) {
     this_._impl_.name_part_.ClearNonDefaultToEmpty();
   }
-  this_._impl_.is_extension_ = false;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -15052,6 +15021,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.positive_int_value_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, double_value_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, positive_int_value_) +
+               sizeof(Impl_::double_value_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x0000000fU)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -15067,13 +15040,6 @@
       this_._impl_.aggregate_value_.ClearNonDefaultToEmpty();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x00000070U)) {
-    ::memset(&this_._impl_.positive_int_value_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.double_value_) -
-                 reinterpret_cast<char*>(&this_._impl_.positive_int_value_)) +
-                 sizeof(_impl_.double_value_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -15467,14 +15433,10 @@
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
   this_._impl_._extensions_.Clear();
-  cached_has_bits = this_._impl_._has_bits_[0];
-  if (BatchCheckHasBit(cached_has_bits, 0xffffffffU)) {
-    ::memset(&this_._impl_.field_presence_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.enforce_proto_limits_) -
-                 reinterpret_cast<char*>(&this_._impl_.field_presence_)) +
-                 sizeof(_impl_.enforce_proto_limits_));
-  }
+  ::memset(&this_._impl_.field_presence_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, enforce_proto_limits_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, field_presence_) +
+               sizeof(Impl_::enforce_proto_limits_));
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -15814,6 +15776,7 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  this_._impl_.edition_ = 0;
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -15825,7 +15788,6 @@
       this_._impl_.fixed_features_->Clear();
     }
   }
-  this_._impl_.edition_ = 0;
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -16098,17 +16060,14 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.minimum_edition_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, maximum_edition_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, minimum_edition_) +
+               sizeof(Impl_::maximum_edition_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (CheckHasBit(cached_has_bits, 0x00000001U)) {
     this_._impl_.defaults_.Clear();
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x00000006U)) {
-    ::memset(&this_._impl_.minimum_edition_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.maximum_edition_) -
-                 reinterpret_cast<char*>(&this_._impl_.minimum_edition_)) +
-                 sizeof(_impl_.maximum_edition_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }
@@ -16911,6 +16870,10 @@
   ::google::protobuf::internal::TSanWrite(&this_._impl_);
   ::uint32_t cached_has_bits [[maybe_unused]] = 0;
 
+  ::memset(&this_._impl_.begin_, 0,
+           PROTOBUF_FIELD_OFFSET(Impl_, semantic_) -
+               PROTOBUF_FIELD_OFFSET(Impl_, begin_) +
+               sizeof(Impl_::semantic_));
   cached_has_bits = this_._impl_._has_bits_[0];
   if (BatchCheckHasBit(cached_has_bits, 0x00000003U)) {
     if (CheckHasBit(cached_has_bits, 0x00000001U)) {
@@ -16920,13 +16883,6 @@
       this_._impl_.source_file_.ClearNonDefaultToEmpty();
     }
   }
-  if (BatchCheckHasBit(cached_has_bits, 0x0000001cU)) {
-    ::memset(&this_._impl_.begin_, 0,
-             static_cast<::size_t>(
-                 reinterpret_cast<char*>(&this_._impl_.semantic_) -
-                 reinterpret_cast<char*>(&this_._impl_.begin_)) +
-                 sizeof(_impl_.semantic_));
-  }
   this_._impl_._has_bits_.Clear();
   this_._internal_metadata_.Clear<::google::protobuf::UnknownFieldSet>();
 }