[optimization] Always inline `Construct` and `Destruct` (#36951)

These are helpers to make it slightly easier to spell some kinds of code... and should never ever generate a function body.

(that said, I've seen them do so... fixing now)

Closes #36951

COPYBARA_INTEGRATE_REVIEW=https://github.com/grpc/grpc/pull/36951 from ctiller:construct 26725911d7
PiperOrigin-RevId: 644132869
dependabot/pip/urllib3-1.26.19
Craig Tiller 7 months ago committed by Copybara-Service
parent 2e4e92bb87
commit f04eee5cae
  1. 5
      include/grpc/support/port_platform.h
  2. 4
      src/core/lib/gprpp/construct_destruct.h

@ -747,6 +747,7 @@ extern void gpr_unreachable_code(const char* reason, const char* file,
#endif /* GPR_ATTRIBUTE_NOINLINE */
#ifndef GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
#ifdef __cplusplus
#if GPR_HAS_CPP_ATTRIBUTE(clang::always_inline)
#define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION [[clang::always_inline]]
#elif GPR_HAS_ATTRIBUTE(always_inline)
@ -755,6 +756,10 @@ extern void gpr_unreachable_code(const char* reason, const char* file,
// TODO(ctiller): add __forceinline for MSVC
#define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
#endif
#else
// Disable for C code
#define GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION
#endif
#endif /* GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION */
#ifndef GPR_NO_UNIQUE_ADDRESS

@ -24,14 +24,14 @@ namespace grpc_core {
// Call the destructor of p without having to name the type of p.
template <typename T>
void Destruct(T* p) {
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Destruct(T* p) {
p->~T();
}
// Call the constructor of p without having to name the type of p and forward
// any arguments
template <typename T, typename... Args>
void Construct(T* p, Args&&... args) {
GPR_ATTRIBUTE_ALWAYS_INLINE_FUNCTION void Construct(T* p, Args&&... args) {
new (p) T(std::forward<Args>(args)...);
}

Loading…
Cancel
Save