From 821b0732f23af1c73155e71d4c46b8023664aacf Mon Sep 17 00:00:00 2001 From: Martijn Vels Date: Fri, 9 Dec 2022 13:44:40 -0800 Subject: [PATCH] Cleanup ThreadCache and LifeCycledId PiperOrigin-RevId: 494254080 --- src/google/protobuf/arena.cc | 17 +++++++---------- src/google/protobuf/arena_impl.h | 29 +++++++---------------------- 2 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/google/protobuf/arena.cc b/src/google/protobuf/arena.cc index 2a76aa13f5..c47cfbf85a 100644 --- a/src/google/protobuf/arena.cc +++ b/src/google/protobuf/arena.cc @@ -441,8 +441,8 @@ ThreadSafeArena::SerialArenaChunk* ThreadSafeArena::SentrySerialArenaChunk() { } -ThreadSafeArena::CacheAlignedLifecycleIdGenerator - ThreadSafeArena::lifecycle_id_generator_; +ABSL_CONST_INIT alignas(kCacheAlignment) + std::atomic ThreadSafeArena::lifecycle_id_{0}; #if defined(PROTOBUF_NO_THREADLOCAL) ThreadSafeArena::ThreadCache& ThreadSafeArena::thread_cache() { static internal::ThreadLocalStorage* 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(-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(-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; diff --git a/src/google/protobuf/arena_impl.h b/src/google/protobuf/arena_impl.h index c4810d5de9..a9ca93ba99 100644 --- a/src/google/protobuf/arena_impl.h +++ b/src/google/protobuf/arena_impl.h @@ -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(-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 id; - }; - static CacheAlignedLifecycleIdGenerator lifecycle_id_generator_; + using LifecycleId = uint64_t; + ABSL_CONST_INIT alignas( + kCacheAlignment) static std::atomic 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