Skip to content

Commit

Permalink
Remove several build-time conditionals on deprecated per-thread.
Browse files Browse the repository at this point in the history
Since this is a rarely used configuration, we can specialize for per-cpu being
available.  Either way, we need to be able to use the per-thread caches if
per-cpu caches are not available, so our fallbacks get exercised that way.

PiperOrigin-RevId: 565112748
Change-Id: Ib7303fde4af31a71a0107ae99de63c6b5ad2309c
  • Loading branch information
ckennelly authored and copybara-github committed Sep 13, 2023
1 parent 03dc2d4 commit 53116a3
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 37 deletions.
4 changes: 2 additions & 2 deletions tcmalloc/cpu_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -2301,8 +2301,8 @@ inline bool UsePerCpuCache(State& state) {
// into tcmalloc.
//
// If the per-CPU cache for a thread is not initialized, we push ourselves
// onto the slow path (if !defined(TCMALLOC_DEPRECATED_PERTHREAD)) until this
// occurs. See fast_alloc's use of TryRecordAllocationFast.
// onto the slow path until this occurs. See fast_alloc's use of
// TryRecordAllocationFast.
if (ABSL_PREDICT_TRUE(subtle::percpu::IsFast())) {
ThreadCache::BecomeIdle();
return true;
Expand Down
4 changes: 0 additions & 4 deletions tcmalloc/static_vars.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ class Static final {
// this can be a common source of bugs. Suppress this by casting to
// int first.

#ifndef TCMALLOC_DEPRECATED_PERTHREAD
// When the per-cpu cache is enabled, and the thread's current cpu
// variable is initialized we will try to allocate from the per-cpu
// cache. If something fails, we bail out to the full malloc.
Expand All @@ -176,9 +175,6 @@ class Static final {
// variable is initialized.
static_cast<int>(CpuCacheActive()) &
static_cast<int>(subtle::percpu::IsFastNoInit());
#else
!CpuCacheActive();
#endif
}

static size_t metadata_bytes() ABSL_EXCLUSIVE_LOCKS_REQUIRED(pageheap_lock);
Expand Down
31 changes: 0 additions & 31 deletions tcmalloc/tcmalloc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -564,24 +564,11 @@ static inline ABSL_ATTRIBUTE_ALWAYS_INLINE void FreeSmall(void* ptr,
return;
}

#ifndef TCMALLOC_DEPRECATED_PERTHREAD
// The CPU Cache is enabled, so we're able to take the fastpath.
ASSERT(tc_globals.CpuCacheActive());
ASSERT(subtle::percpu::IsFastNoInit());

tc_globals.cpu_cache().Deallocate(ptr, size_class);
#else // TCMALLOC_DEPRECATED_PERTHREAD
ThreadCache* cache = ThreadCache::GetCacheIfPresent();

// IsOnFastPath does not track whether or not we have an active ThreadCache on
// this thread, so we need to check cache for nullptr.
if (ABSL_PREDICT_FALSE(cache == nullptr)) {
FreeSmallSlow(ptr, size_class);
return;
}

cache->Deallocate(ptr, size_class);
#endif // TCMALLOC_DEPRECATED_PERTHREAD
}

// this helper function is used when FreeSmall (defined above) hits
Expand Down Expand Up @@ -961,10 +948,7 @@ using tcmalloc::tcmalloc_internal::MallocPolicy;
using tcmalloc::tcmalloc_internal::Parameters;
using tcmalloc::tcmalloc_internal::tc_globals;
using tcmalloc::tcmalloc_internal::UsePerCpuCache;

#ifdef TCMALLOC_DEPRECATED_PERTHREAD
using tcmalloc::tcmalloc_internal::ThreadCache;
#endif // TCMALLOC_DEPRECATED_PERTHREAD

// Slow path implementation.
// This function is used by `fast_alloc` if the allocation requires page sized
Expand Down Expand Up @@ -1020,15 +1004,6 @@ static inline Pointer ABSL_ATTRIBUTE_ALWAYS_INLINE fast_alloc(Policy policy,
return slow_alloc(policy, size);
}

// When using per-thread caches, we have to check for the presence of the
// cache for this thread before we try to sample, as slow_alloc will
// also try to sample the allocation.
#ifdef TCMALLOC_DEPRECATED_PERTHREAD
ThreadCache* const cache = ThreadCache::GetCacheIfPresent();
if (ABSL_PREDICT_FALSE(cache == nullptr)) {
return slow_alloc(policy, size);
}
#endif
// TryRecordAllocationFast() returns true if no extra logic is required, e.g.:
// - this allocation does not need to be sampled
// - no new/delete hooks need to be invoked
Expand All @@ -1046,14 +1021,8 @@ static inline Pointer ABSL_ATTRIBUTE_ALWAYS_INLINE fast_alloc(Policy policy,
// - no new/delete hook is installed and required to be called.
ASSERT(size_class != 0);
Pointer ret;
#ifndef TCMALLOC_DEPRECATED_PERTHREAD
// The CPU cache should be ready.
ret = tc_globals.cpu_cache().Allocate<Policy>(size_class);
#else // !defined(TCMALLOC_DEPRECATED_PERTHREAD)
// The ThreadCache should be ready.
ASSERT(cache != nullptr);
ret = cache->Allocate<Policy>(size_class);
#endif // TCMALLOC_DEPRECATED_PERTHREAD
if (!Policy::can_return_nullptr()) {
ret = AssumeNotNull(ret);
}
Expand Down

0 comments on commit 53116a3

Please sign in to comment.