Eliminate a branch in Delete for grpc_core::UniquePtr.

This is showing up in microbenchmarks in the StringView PR,
becacuse strdup() retruns a grpc_core::UniquePtr now.

Removing the branch lowers the difference.
pull/19386/head
Soheil Hassas Yeganeh 6 years ago
parent 3ab7332769
commit 61886e0d73
  1. 4
      src/core/ext/filters/client_channel/lb_policy/grpclb/grpclb.cc
  2. 4
      src/core/ext/filters/client_channel/lb_policy/xds/xds.cc
  3. 5
      src/core/ext/filters/client_channel/retry_throttle.h
  4. 35
      src/core/lib/gprpp/memory.h
  5. 10
      src/core/lib/slice/slice_hash_table.h
  6. 9
      src/core/lib/slice/slice_weak_hash_table.h
  7. 9
      src/core/tsi/ssl/session_cache/ssl_session_cache.h

@ -161,9 +161,7 @@ class GrpcLb : public LoadBalancingPolicy {
bool seen_serverlist() const { return seen_serverlist_; }
private:
// So Delete() can access our private dtor.
template <typename T>
friend void grpc_core::Delete(T*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
~BalancerCallState();

@ -185,9 +185,7 @@ class XdsLb : public LoadBalancingPolicy {
bool seen_initial_response() const { return seen_initial_response_; }
private:
// So Delete() can access our private dtor.
template <typename T>
friend void grpc_core::Delete(T*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
~BalancerCallState();

@ -21,6 +21,7 @@
#include <grpc/support/port_platform.h>
#include "src/core/lib/gprpp/memory.h"
#include "src/core/lib/gprpp/ref_counted.h"
namespace grpc_core {
@ -42,9 +43,7 @@ class ServerRetryThrottleData : public RefCounted<ServerRetryThrottleData> {
intptr_t milli_token_ratio() const { return milli_token_ratio_; }
private:
// So Delete() can call our private dtor.
template <typename T>
friend void grpc_core::Delete(T*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
~ServerRetryThrottleData();

@ -22,6 +22,7 @@
#include <grpc/support/port_platform.h>
#include <grpc/support/alloc.h>
#include <grpc/support/log.h>
#include <limits>
#include <memory>
@ -29,14 +30,17 @@
// Add this to a class that want to use Delete(), but has a private or
// protected destructor.
#define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE \
template <typename T> \
friend void grpc_core::Delete(T*);
#define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE \
template <typename _Delete_T, bool _Delete_can_be_null> \
friend void ::grpc_core::Delete(_Delete_T*); \
template <typename _Delete_T> \
friend void ::grpc_core::Delete(_Delete_T*);
// Add this to a class that want to use New(), but has a private or
// protected constructor.
#define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW \
template <typename T, typename... Args> \
friend T* grpc_core::New(Args&&...);
#define GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW \
template <typename _New_T, typename... _New_Args> \
friend _New_T* grpc_core::New(_New_Args&&...);
namespace grpc_core {
@ -48,17 +52,30 @@ inline T* New(Args&&... args) {
}
// Alternative to delete, since we cannot use it (for fear of libstdc++)
template <typename T>
// We cannot add a default value for can_be_null, because they are used as
// as friend template methods where we cannot define a default value.
// Instead we simply define two variants, one with and one without the boolean
// argument.
template <typename T, bool can_be_null>
inline void Delete(T* p) {
if (p == nullptr) return;
GPR_DEBUG_ASSERT(can_be_null || p != nullptr);
if (can_be_null && p == nullptr) return;
p->~T();
gpr_free(p);
}
template <typename T>
inline void Delete(T* p) {
Delete<T, /*can_be_null=*/true>(p);
}
template <typename T>
class DefaultDelete {
public:
void operator()(T* p) { Delete(p); }
void operator()(T* p) {
// std::unique_ptr is gauranteed not to call the deleter
// if the pointer is nullptr.
Delete<T, /*can_be_null=*/false>(p);
}
};
template <typename T, typename Deleter = DefaultDelete<T>>

@ -25,6 +25,7 @@
#include <grpc/support/log.h>
#include "src/core/lib/gpr/useful.h"
#include "src/core/lib/gprpp/memory.h"
#include "src/core/lib/gprpp/ref_counted.h"
#include "src/core/lib/gprpp/ref_counted_ptr.h"
#include "src/core/lib/slice/slice_internal.h"
@ -77,13 +78,8 @@ class SliceHashTable : public RefCounted<SliceHashTable<T>> {
static int Cmp(const SliceHashTable& a, const SliceHashTable& b);
private:
// So New() can call our private ctor.
template <typename T2, typename... Args>
friend T2* New(Args&&... args);
// So Delete() can call our private dtor.
template <typename T2>
friend void Delete(T2*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
SliceHashTable(size_t num_entries, Entry* entries, ValueCmp value_cmp);
virtual ~SliceHashTable();

@ -61,13 +61,8 @@ class SliceWeakHashTable : public RefCounted<SliceWeakHashTable<T, Size>> {
}
private:
// So New() can call our private ctor.
template <typename T2, typename... Args>
friend T2* New(Args&&... args);
// So Delete() can call our private dtor.
template <typename T2>
friend void Delete(T2*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
SliceWeakHashTable() = default;
~SliceWeakHashTable() = default;

@ -67,13 +67,8 @@ class SslSessionLRUCache : public grpc_core::RefCounted<SslSessionLRUCache> {
SslSessionPtr Get(const char* key);
private:
// So New() can call our private ctor.
template <typename T, typename... Args>
friend T* grpc_core::New(Args&&... args);
// So Delete() can call our private dtor.
template <typename T>
friend void grpc_core::Delete(T*);
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_NEW
GRPC_ALLOW_CLASS_TO_USE_NON_PUBLIC_DELETE
class Node;

Loading…
Cancel
Save