Skip to content

Commit

Permalink
[ur] optimize align allocation in DisjointPool
Browse files Browse the repository at this point in the history
Do not increase size by Alignment - 1 if the requested
Alignment is smaller or equal to coarse-grain allocation
alignemnt - in such case, Bucket for the allocation will
already be properly aligned.
  • Loading branch information
igchor committed Jun 15, 2023
1 parent 7320835 commit 9c07701
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions source/common/uma_pools/disjoint_pool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,13 @@ static void *AlignPtrUp(void *Ptr, const size_t Alignment) {
return static_cast<char *>(AlignedPtr) + Alignment;
}

// Aligns the value up to the specified alignment
// (e.g. returns 16 for Size = 13, Alignment = 8)
static size_t AlignUp(size_t Val, size_t Alignment) {
assert(Alignment > 0);
return (Val + Alignment - 1) & (~(Alignment - 1));
}

DisjointPoolConfig::DisjointPoolConfig()
: limits(std::make_shared<DisjointPoolConfig::SharedLimits>()) {}

Expand Down Expand Up @@ -272,6 +279,9 @@ class DisjointPool::AllocImpl {
// Configuration for this instance
DisjointPoolConfig params;

// Coarse-grain allocation min alignment
size_t ProviderMinPageSize;

public:
AllocImpl(uma_memory_provider_handle_t hProvider, DisjointPoolConfig params)
: MemHandle{hProvider}, params(params) {
Expand All @@ -285,6 +295,12 @@ class DisjointPool::AllocImpl {
Buckets.push_back(std::make_unique<Bucket>(Size2, *this));
}
Buckets.push_back(std::make_unique<Bucket>(CutOff, *this));

auto ret = umaMemoryProviderGetMinPageSize(hProvider, nullptr,
&ProviderMinPageSize);
if (ret != UMA_RESULT_SUCCESS) {
ProviderMinPageSize = 0;
}
}

void *allocate(size_t Size, size_t Alignment, bool &FromPool);
Expand Down Expand Up @@ -742,9 +758,15 @@ void *DisjointPool::AllocImpl::allocate(size_t Size, size_t Alignment,
return allocate(Size, FromPool);
}

// TODO: we potentially waste some space here, calulate it based on minBucketSize and Slab alignemnt
// (using umaMemoryProviderGetMinPageSize)?
size_t AlignedSize = (Size > 1) ? (Size + Alignment - 1) : Alignment;
size_t AlignedSize;
if (Alignment <= ProviderMinPageSize) {
// This allocation will be served from a Bucket which size is multiple
// of Alignment and Slab address is aligned to ProviderMinPageSize
// so the bucket will be properly aligned.
AlignedSize = (Size > 1) ? AlignUp(Size, Alignment) : Alignment;
} else {
AlignedSize = Size + Alignment - 1;
}

// Check if requested allocation size is within pooling limit.
// If not, just request aligned pointer from the system.
Expand Down

0 comments on commit 9c07701

Please sign in to comment.