Cleanup ThreadCache and LifeCycledId

PiperOrigin-RevId: 494254080
pull/11227/head
Martijn Vels 2 years ago committed by Copybara-Service
parent a49bfa9825
commit 821b0732f2
  1. 17
      src/google/protobuf/arena.cc
  2. 29
      src/google/protobuf/arena_impl.h

@ -441,8 +441,8 @@ ThreadSafeArena::SerialArenaChunk* ThreadSafeArena::SentrySerialArenaChunk() {
}
ThreadSafeArena::CacheAlignedLifecycleIdGenerator
ThreadSafeArena::lifecycle_id_generator_;
ABSL_CONST_INIT alignas(kCacheAlignment)
std::atomic<ThreadSafeArena::LifecycleId> ThreadSafeArena::lifecycle_id_{0};
#if defined(PROTOBUF_NO_THREADLOCAL)
ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() {
static internal::ThreadLocalStorage<ThreadCache>* thread_cache_ =
@ -451,14 +451,12 @@ ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() {
}
#elif defined(PROTOBUF_USE_DLLS)
ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() {
static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_ = {
0, static_cast<LifecycleIdAtomic>(-1), nullptr};
return thread_cache_;
static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache;
return thread_cache;
}
#else
PROTOBUF_THREAD_LOCAL ThreadSafeArena::ThreadCache
ThreadSafeArena::thread_cache_ = {0, static_cast<LifecycleIdAtomic>(-1),
nullptr};
ABSL_CONST_INIT PROTOBUF_THREAD_LOCAL
ThreadSafeArena::ThreadCache ThreadSafeArena::thread_cache_;
#endif
ThreadSafeArena::ThreadSafeArena() : first_arena_(*this) { Init(); }
@ -549,8 +547,7 @@ uint64_t ThreadSafeArena::GetNextLifeCycleId() {
// On platforms that don't support uint64_t atomics we can certainly not
// afford to increment by large intervals and expect uniqueness due to
// wrapping, hence we only add by 1.
id = lifecycle_id_generator_.id.fetch_add(1, std::memory_order_relaxed) *
kInc;
id = lifecycle_id_.fetch_add(1, std::memory_order_relaxed) * kInc;
}
tc.next_lifecycle_id = id + kDelta;
return id;

@ -109,8 +109,6 @@ struct ArenaBlock {
// data follows
};
using LifecycleIdAtomic = uint64_t;
enum class AllocationClient { kDefault, kArray };
class ThreadSafeArena;
@ -582,16 +580,6 @@ class PROTOBUF_EXPORT ThreadSafeArena {
#pragma warning(disable : 4324)
#endif
struct alignas(kCacheAlignment) ThreadCache {
#if defined(PROTOBUF_NO_THREADLOCAL)
// If we are using the ThreadLocalStorage class to store the ThreadCache,
// then the ThreadCache's default constructor has to be responsible for
// initializing it.
ThreadCache()
: next_lifecycle_id(0),
last_lifecycle_id_seen(-1),
last_serial_arena(nullptr) {}
#endif
// Number of per-thread lifecycle IDs to reserve. Must be power of two.
// To reduce contention on a global atomic, each thread reserves a batch of
// IDs. The following number is calculated based on a stress test with
@ -599,11 +587,11 @@ class PROTOBUF_EXPORT ThreadSafeArena {
static constexpr size_t kPerThreadIds = 256;
// Next lifecycle ID available to this thread. We need to reserve a new
// batch, if `next_lifecycle_id & (kPerThreadIds - 1) == 0`.
uint64_t next_lifecycle_id;
uint64_t next_lifecycle_id{0};
// The ThreadCache is considered valid as long as this matches the
// lifecycle_id of the arena being used.
uint64_t last_lifecycle_id_seen;
SerialArena* last_serial_arena;
uint64_t last_lifecycle_id_seen{static_cast<uint64_t>(-1)};
SerialArena* last_serial_arena{nullptr};
};
// Lifecycle_id can be highly contended variable in a situation of lots of
@ -612,12 +600,9 @@ class PROTOBUF_EXPORT ThreadSafeArena {
#ifdef _MSC_VER
#pragma warning(disable : 4324)
#endif
struct alignas(kCacheAlignment) CacheAlignedLifecycleIdGenerator {
constexpr CacheAlignedLifecycleIdGenerator() : id{0} {}
std::atomic<LifecycleIdAtomic> id;
};
static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_;
using LifecycleId = uint64_t;
ABSL_CONST_INIT alignas(
kCacheAlignment) static std::atomic<LifecycleId> lifecycle_id_;
#if defined(PROTOBUF_NO_THREADLOCAL)
// iOS does not support __thread keyword so we use a custom thread local
// storage class we implemented.
@ -627,7 +612,7 @@ class PROTOBUF_EXPORT ThreadSafeArena {
// wrap them in static functions.
static ThreadCache& thread_cache();
#else
static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_;
ABSL_CONST_INIT static PROTOBUF_THREAD_LOCAL ThreadCache thread_cache_;
static ThreadCache& thread_cache() { return thread_cache_; }
#endif

Loading…
Cancel
Save