Skip to content

Commit 69a6fb0

Browse files
committed
[libc] Add a config option to disable slab reclaiming
Summary: Without slab reclaiming this interface is much simpler and it can speed up cases with a lot of churn. Basically, wastes memory for performance.
1 parent 62ac4e3 commit 69a6fb0

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

libc/src/__support/GPU/allocator.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ constexpr static uint32_t MAX_TRIES = 1024;
4343
// The number of previously allocated slabs we will keep in memory.
4444
constexpr static uint32_t CACHED_SLABS = 8;
4545

46+
// Configuration for whether or not we will return unused slabs to memory.
47+
constexpr static bool RECLAIM = true;
48+
4649
static_assert(!(ARRAY_SIZE & (ARRAY_SIZE - 1)), "Must be a power of two");
4750

4851
namespace impl {
@@ -399,7 +402,7 @@ struct GuardPtr {
399402
// and obtain exclusive rights to deconstruct it. If the CAS failed either
400403
// another thread resurrected the counter and we quit, or a parallel read
401404
// helped us invalidating it. For the latter, claim that flag and return.
402-
if (counter.fetch_sub(n, cpp::MemoryOrder::RELAXED) == n) {
405+
if (counter.fetch_sub(n, cpp::MemoryOrder::RELAXED) == n && RECLAIM) {
403406
uint32_t expected = 0;
404407
if (counter.compare_exchange_strong(expected, INVALID,
405408
cpp::MemoryOrder::RELAXED,
@@ -417,8 +420,9 @@ struct GuardPtr {
417420
// thread.
418421
uint64_t read() {
419422
auto val = counter.load(cpp::MemoryOrder::RELAXED);
420-
if (val == 0 && counter.compare_exchange_strong(
421-
val, INVALID | HELPED, cpp::MemoryOrder::RELAXED))
423+
if (val == 0 && RECLAIM &&
424+
counter.compare_exchange_strong(val, INVALID | HELPED,
425+
cpp::MemoryOrder::RELAXED))
422426
return 0;
423427
return (val & INVALID) ? 0 : val;
424428
}
@@ -463,7 +467,7 @@ struct GuardPtr {
463467
return nullptr;
464468

465469
cpp::atomic_thread_fence(cpp::MemoryOrder::ACQUIRE);
466-
return ptr.load(cpp::MemoryOrder::RELAXED);
470+
return RECLAIM ? ptr.load(cpp::MemoryOrder::RELAXED) : expected;
467471
}
468472

469473
// Finalize the associated memory and signal that it is ready to use by

0 commit comments

Comments
 (0)