Reduce inlined part of the destructor.
We do not expect well-behaved code to ever rely on this check and well-behaved code should not being paying the price for it. We still need to keep it until b/290091828 is fixed but we can move it to slow out-of-line function.
```
name old cpu/op new cpu/op delta
BM_RepeatedPtrField_Ctor 75.3ns ± 4% 63.8ns ± 1% -15.27% (p=0.000 n=99+86)
```
PiperOrigin-RevId: 590139004
diff --git a/src/google/protobuf/repeated_ptr_field.h b/src/google/protobuf/repeated_ptr_field.h
index f48d41d..644bdb4 100644
--- a/src/google/protobuf/repeated_ptr_field.h
+++ b/src/google/protobuf/repeated_ptr_field.h
@@ -207,6 +207,11 @@
template <typename TypeHandler>
void Destroy() {
ABSL_DCHECK(NeedsDestroy());
+
+ // TODO: arena check is redundant once all `RepeatedPtrField`s
+ // with non-null arena are owned by the arena.
+ if (PROTOBUF_PREDICT_FALSE(arena_ != nullptr)) return;
+
using H = CommonHandler<TypeHandler>;
int n = allocated_size();
void** elems = elements();
@@ -219,13 +224,12 @@
}
}
- bool NeedsDestroy() const {
- // TODO: arena check is redundant once all `RepeatedPtrField`s
- // with non-null arena are owned by the arena.
- return tagged_rep_or_elem_ != nullptr &&
- PROTOBUF_PREDICT_FALSE(arena_ == nullptr);
+ inline bool NeedsDestroy() const {
+ // Either there is an allocated element in SSO buffer or there is an
+ // allocated Rep.
+ return tagged_rep_or_elem_ != nullptr;
}
- void DestroyProtos(); // implemented in the cc file
+ void DestroyProtos();
public:
// The next few methods are public so that they can be called from generated