Skip to content

Commit 34dfd3f

Browse files
rlyerlymeta-codesync[bot]
authored andcommitted
Small SlabAllocator fixes
Summary: - Pull from back of vector (back() + pop_back()) when reclaiming slabs since we're using a vector - Remove unused headers (and add necessary ones in other .cpp files) - Fix pointer provenance lint (verified via [godbolt](https://gcc.godbolt.org/z/YPrccWfvd) that it should produce identical assembly) Reviewed By: AlnisM Differential Revision: D99835775 fbshipit-source-id: c46e9ac3c0463bc9ae7f24fe5c0efd6d2d395d1b
1 parent c8e028f commit 34dfd3f

File tree

4 files changed

+8
-8
lines changed

4 files changed

+8
-8
lines changed

cachelib/allocator/memory/MemoryAllocator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
#include <algorithm>
2222
#include <utility>
2323

24+
#include "cachelib/common/Utils.h"
25+
2426
using namespace facebook::cachelib;
2527

2628
namespace {

cachelib/allocator/memory/MemoryPool.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
#include "cachelib/allocator/memory/MemoryPool.h"
1818

1919
#include <algorithm>
20-
#include <string>
20+
#include <numeric>
2121
#include <utility>
2222

2323
#include "cachelib/allocator/memory/AllocationClass.h"

cachelib/allocator/memory/SlabAllocator.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,8 @@ Slab* FOLLY_NULLABLE SlabAllocator::reclaimSlab(PoolId id) {
400400
{
401401
LockHolder l(lock_);
402402
if (!advisedSlabs_.empty()) {
403-
auto it = advisedSlabs_.begin();
404-
slab = *it;
405-
advisedSlabs_.erase(it);
403+
slab = advisedSlabs_.back();
404+
advisedSlabs_.pop_back();
406405
}
407406
}
408407

cachelib/allocator/memory/SlabAllocator.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
#include <atomic>
2222
#include <mutex>
2323
#include <thread>
24-
#include <unordered_map>
2524
#include <vector>
2625

2726
#pragma GCC diagnostic push
@@ -35,7 +34,6 @@
3534

3635
#include "cachelib/allocator/memory/CompressedPtr.h"
3736
#include "cachelib/allocator/memory/Slab.h"
38-
#include "cachelib/common/Utils.h"
3937

4038
namespace facebook {
4139
namespace cachelib {
@@ -214,8 +212,9 @@ class SlabAllocator {
214212
FOLLY_ALWAYS_INLINE const Slab* getSlabForMemory(
215213
const void* memory) const noexcept {
216214
// returns the closest slab boundary for the memory address.
217-
return reinterpret_cast<const Slab*>(reinterpret_cast<uintptr_t>(memory) &
218-
kAddressMask);
215+
auto offset = reinterpret_cast<uintptr_t>(memory) & ~kAddressMask;
216+
return reinterpret_cast<const Slab*>(
217+
reinterpret_cast<const uint8_t*>(memory) - offset);
219218
}
220219

221220
using SlabIdx = uint32_t;

0 commit comments

Comments
 (0)