Refactoring/optimization: Decreased size of VmaAllocation object by one pointer (from 72 to 64 bytes in 64-bit configurations) by removing VmaAllocation_T::BlockAllocation::m_hPool.

This commit is contained in:
Adam Sawicki 2019-01-24 17:21:27 +01:00
parent 70556b6754
commit 5df580a997

View file

@ -4943,7 +4943,6 @@ public:
} }
void InitBlockAllocation( void InitBlockAllocation(
VmaPool hPool,
VmaDeviceMemoryBlock* block, VmaDeviceMemoryBlock* block,
VkDeviceSize offset, VkDeviceSize offset,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -4959,7 +4958,6 @@ public:
m_Size = size; m_Size = size;
m_MapCount = mapped ? MAP_COUNT_FLAG_PERSISTENT_MAP : 0; m_MapCount = mapped ? MAP_COUNT_FLAG_PERSISTENT_MAP : 0;
m_SuballocationType = (uint8_t)suballocationType; m_SuballocationType = (uint8_t)suballocationType;
m_BlockAllocation.m_hPool = hPool;
m_BlockAllocation.m_Block = block; m_BlockAllocation.m_Block = block;
m_BlockAllocation.m_Offset = offset; m_BlockAllocation.m_Offset = offset;
m_BlockAllocation.m_CanBecomeLost = canBecomeLost; m_BlockAllocation.m_CanBecomeLost = canBecomeLost;
@ -4970,7 +4968,6 @@ public:
VMA_ASSERT(m_Type == ALLOCATION_TYPE_NONE); VMA_ASSERT(m_Type == ALLOCATION_TYPE_NONE);
VMA_ASSERT(m_LastUseFrameIndex.load() == VMA_FRAME_INDEX_LOST); VMA_ASSERT(m_LastUseFrameIndex.load() == VMA_FRAME_INDEX_LOST);
m_Type = (uint8_t)ALLOCATION_TYPE_BLOCK; m_Type = (uint8_t)ALLOCATION_TYPE_BLOCK;
m_BlockAllocation.m_hPool = VK_NULL_HANDLE;
m_BlockAllocation.m_Block = VMA_NULL; m_BlockAllocation.m_Block = VMA_NULL;
m_BlockAllocation.m_Offset = 0; m_BlockAllocation.m_Offset = 0;
m_BlockAllocation.m_CanBecomeLost = true; m_BlockAllocation.m_CanBecomeLost = true;
@ -5023,7 +5020,6 @@ public:
bool IsPersistentMap() const { return (m_MapCount & MAP_COUNT_FLAG_PERSISTENT_MAP) != 0; } bool IsPersistentMap() const { return (m_MapCount & MAP_COUNT_FLAG_PERSISTENT_MAP) != 0; }
void* GetMappedData() const; void* GetMappedData() const;
bool CanBecomeLost() const; bool CanBecomeLost() const;
VmaPool GetPool() const;
uint32_t GetLastUseFrameIndex() const uint32_t GetLastUseFrameIndex() const
{ {
@ -5089,7 +5085,6 @@ private:
// Allocation out of VmaDeviceMemoryBlock. // Allocation out of VmaDeviceMemoryBlock.
struct BlockAllocation struct BlockAllocation
{ {
VmaPool m_hPool; // Null if belongs to general memory.
VmaDeviceMemoryBlock* m_Block; VmaDeviceMemoryBlock* m_Block;
VkDeviceSize m_Offset; VkDeviceSize m_Offset;
bool m_CanBecomeLost; bool m_CanBecomeLost;
@ -5753,6 +5748,7 @@ public:
// Always call after construction. // Always call after construction.
void Init( void Init(
VmaAllocator hAllocator, VmaAllocator hAllocator,
VmaPool hParentPool,
uint32_t newMemoryTypeIndex, uint32_t newMemoryTypeIndex,
VkDeviceMemory newMemory, VkDeviceMemory newMemory,
VkDeviceSize newSize, VkDeviceSize newSize,
@ -5761,6 +5757,7 @@ public:
// Always call before destruction. // Always call before destruction.
void Destroy(VmaAllocator allocator); void Destroy(VmaAllocator allocator);
VmaPool GetParentPool() const { return m_hParentPool; }
VkDeviceMemory GetDeviceMemory() const { return m_hMemory; } VkDeviceMemory GetDeviceMemory() const { return m_hMemory; }
uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; } uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }
uint32_t GetId() const { return m_Id; } uint32_t GetId() const { return m_Id; }
@ -5788,6 +5785,7 @@ public:
VkImage hImage); VkImage hImage);
private: private:
VmaPool m_hParentPool; // VK_NULL_HANDLE if not belongs to custom pool.
uint32_t m_MemoryTypeIndex; uint32_t m_MemoryTypeIndex;
uint32_t m_Id; uint32_t m_Id;
VkDeviceMemory m_hMemory; VkDeviceMemory m_hMemory;
@ -5833,6 +5831,7 @@ struct VmaBlockVector
public: public:
VmaBlockVector( VmaBlockVector(
VmaAllocator hAllocator, VmaAllocator hAllocator,
VmaPool hParentPool,
uint32_t memoryTypeIndex, uint32_t memoryTypeIndex,
VkDeviceSize preferredBlockSize, VkDeviceSize preferredBlockSize,
size_t minBlockCount, size_t minBlockCount,
@ -5846,6 +5845,7 @@ public:
VkResult CreateMinBlocks(); VkResult CreateMinBlocks();
VmaPool GetParentPool() const { return m_hParentPool; }
uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; } uint32_t GetMemoryTypeIndex() const { return m_MemoryTypeIndex; }
VkDeviceSize GetPreferredBlockSize() const { return m_PreferredBlockSize; } VkDeviceSize GetPreferredBlockSize() const { return m_PreferredBlockSize; }
VkDeviceSize GetBufferImageGranularity() const { return m_BufferImageGranularity; } VkDeviceSize GetBufferImageGranularity() const { return m_BufferImageGranularity; }
@ -5858,7 +5858,6 @@ public:
bool IsCorruptionDetectionEnabled() const; bool IsCorruptionDetectionEnabled() const;
VkResult Allocate( VkResult Allocate(
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -5905,6 +5904,7 @@ private:
friend class VmaDefragmentationAlgorithm_Generic; friend class VmaDefragmentationAlgorithm_Generic;
const VmaAllocator m_hAllocator; const VmaAllocator m_hAllocator;
const VmaPool m_hParentPool;
const uint32_t m_MemoryTypeIndex; const uint32_t m_MemoryTypeIndex;
const VkDeviceSize m_PreferredBlockSize; const VkDeviceSize m_PreferredBlockSize;
const size_t m_MinBlockCount; const size_t m_MinBlockCount;
@ -5933,7 +5933,6 @@ private:
void IncrementallySortBlocks(); void IncrementallySortBlocks();
VkResult AllocatePage( VkResult AllocatePage(
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -5944,7 +5943,6 @@ private:
// To be used only without CAN_MAKE_OTHER_LOST flag. // To be used only without CAN_MAKE_OTHER_LOST flag.
VkResult AllocateFromBlock( VkResult AllocateFromBlock(
VmaDeviceMemoryBlock* pBlock, VmaDeviceMemoryBlock* pBlock,
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -7313,12 +7311,6 @@ bool VmaAllocation_T::CanBecomeLost() const
} }
} }
VmaPool VmaAllocation_T::GetPool() const
{
VMA_ASSERT(m_Type == ALLOCATION_TYPE_BLOCK);
return m_BlockAllocation.m_hPool;
}
bool VmaAllocation_T::MakeLost(uint32_t currentFrameIndex, uint32_t frameInUseCount) bool VmaAllocation_T::MakeLost(uint32_t currentFrameIndex, uint32_t frameInUseCount)
{ {
VMA_ASSERT(CanBecomeLost()); VMA_ASSERT(CanBecomeLost());
@ -11086,6 +11078,7 @@ VmaDeviceMemoryBlock::VmaDeviceMemoryBlock(VmaAllocator hAllocator) :
void VmaDeviceMemoryBlock::Init( void VmaDeviceMemoryBlock::Init(
VmaAllocator hAllocator, VmaAllocator hAllocator,
VmaPool hParentPool,
uint32_t newMemoryTypeIndex, uint32_t newMemoryTypeIndex,
VkDeviceMemory newMemory, VkDeviceMemory newMemory,
VkDeviceSize newSize, VkDeviceSize newSize,
@ -11094,6 +11087,7 @@ void VmaDeviceMemoryBlock::Init(
{ {
VMA_ASSERT(m_hMemory == VK_NULL_HANDLE); VMA_ASSERT(m_hMemory == VK_NULL_HANDLE);
m_hParentPool = hParentPool;
m_MemoryTypeIndex = newMemoryTypeIndex; m_MemoryTypeIndex = newMemoryTypeIndex;
m_Id = id; m_Id = id;
m_hMemory = newMemory; m_hMemory = newMemory;
@ -11328,6 +11322,7 @@ VmaPool_T::VmaPool_T(
VkDeviceSize preferredBlockSize) : VkDeviceSize preferredBlockSize) :
m_BlockVector( m_BlockVector(
hAllocator, hAllocator,
this, // hParentPool
createInfo.memoryTypeIndex, createInfo.memoryTypeIndex,
createInfo.blockSize != 0 ? createInfo.blockSize : preferredBlockSize, createInfo.blockSize != 0 ? createInfo.blockSize : preferredBlockSize,
createInfo.minBlockCount, createInfo.minBlockCount,
@ -11351,6 +11346,7 @@ VmaPool_T::~VmaPool_T()
VmaBlockVector::VmaBlockVector( VmaBlockVector::VmaBlockVector(
VmaAllocator hAllocator, VmaAllocator hAllocator,
VmaPool hParentPool,
uint32_t memoryTypeIndex, uint32_t memoryTypeIndex,
VkDeviceSize preferredBlockSize, VkDeviceSize preferredBlockSize,
size_t minBlockCount, size_t minBlockCount,
@ -11361,6 +11357,7 @@ VmaBlockVector::VmaBlockVector(
bool explicitBlockSize, bool explicitBlockSize,
uint32_t algorithm) : uint32_t algorithm) :
m_hAllocator(hAllocator), m_hAllocator(hAllocator),
m_hParentPool(hParentPool),
m_MemoryTypeIndex(memoryTypeIndex), m_MemoryTypeIndex(memoryTypeIndex),
m_PreferredBlockSize(preferredBlockSize), m_PreferredBlockSize(preferredBlockSize),
m_MinBlockCount(minBlockCount), m_MinBlockCount(minBlockCount),
@ -11432,7 +11429,6 @@ bool VmaBlockVector::IsCorruptionDetectionEnabled() const
static const uint32_t VMA_ALLOCATION_TRY_COUNT = 32; static const uint32_t VMA_ALLOCATION_TRY_COUNT = 32;
VkResult VmaBlockVector::Allocate( VkResult VmaBlockVector::Allocate(
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -11455,7 +11451,6 @@ VkResult VmaBlockVector::Allocate(
for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex) for(allocIndex = 0; allocIndex < allocationCount; ++allocIndex)
{ {
res = AllocatePage( res = AllocatePage(
hCurrentPool,
currentFrameIndex, currentFrameIndex,
size, size,
alignment, alignment,
@ -11483,7 +11478,6 @@ VkResult VmaBlockVector::Allocate(
} }
VkResult VmaBlockVector::AllocatePage( VkResult VmaBlockVector::AllocatePage(
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -11554,7 +11548,6 @@ VkResult VmaBlockVector::AllocatePage(
VMA_ASSERT(pCurrBlock); VMA_ASSERT(pCurrBlock);
VkResult res = AllocateFromBlock( VkResult res = AllocateFromBlock(
pCurrBlock, pCurrBlock,
hCurrentPool,
currentFrameIndex, currentFrameIndex,
size, size,
alignment, alignment,
@ -11581,7 +11574,6 @@ VkResult VmaBlockVector::AllocatePage(
VMA_ASSERT(pCurrBlock); VMA_ASSERT(pCurrBlock);
VkResult res = AllocateFromBlock( VkResult res = AllocateFromBlock(
pCurrBlock, pCurrBlock,
hCurrentPool,
currentFrameIndex, currentFrameIndex,
size, size,
alignment, alignment,
@ -11606,7 +11598,6 @@ VkResult VmaBlockVector::AllocatePage(
VMA_ASSERT(pCurrBlock); VMA_ASSERT(pCurrBlock);
VkResult res = AllocateFromBlock( VkResult res = AllocateFromBlock(
pCurrBlock, pCurrBlock,
hCurrentPool,
currentFrameIndex, currentFrameIndex,
size, size,
alignment, alignment,
@ -11679,7 +11670,6 @@ VkResult VmaBlockVector::AllocatePage(
res = AllocateFromBlock( res = AllocateFromBlock(
pBlock, pBlock,
hCurrentPool,
currentFrameIndex, currentFrameIndex,
size, size,
alignment, alignment,
@ -11813,7 +11803,6 @@ VkResult VmaBlockVector::AllocatePage(
*pAllocation = vma_new(m_hAllocator, VmaAllocation_T)(currentFrameIndex, isUserDataString); *pAllocation = vma_new(m_hAllocator, VmaAllocation_T)(currentFrameIndex, isUserDataString);
pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation); pBestRequestBlock->m_pMetadata->Alloc(bestRequest, suballocType, size, *pAllocation);
(*pAllocation)->InitBlockAllocation( (*pAllocation)->InitBlockAllocation(
hCurrentPool,
pBestRequestBlock, pBestRequestBlock,
bestRequest.offset, bestRequest.offset,
alignment, alignment,
@ -11968,7 +11957,6 @@ void VmaBlockVector::IncrementallySortBlocks()
VkResult VmaBlockVector::AllocateFromBlock( VkResult VmaBlockVector::AllocateFromBlock(
VmaDeviceMemoryBlock* pBlock, VmaDeviceMemoryBlock* pBlock,
VmaPool hCurrentPool,
uint32_t currentFrameIndex, uint32_t currentFrameIndex,
VkDeviceSize size, VkDeviceSize size,
VkDeviceSize alignment, VkDeviceSize alignment,
@ -12017,7 +12005,6 @@ VkResult VmaBlockVector::AllocateFromBlock(
*pAllocation = vma_new(m_hAllocator, VmaAllocation_T)(currentFrameIndex, isUserDataString); *pAllocation = vma_new(m_hAllocator, VmaAllocation_T)(currentFrameIndex, isUserDataString);
pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation); pBlock->m_pMetadata->Alloc(currRequest, suballocType, size, *pAllocation);
(*pAllocation)->InitBlockAllocation( (*pAllocation)->InitBlockAllocation(
hCurrentPool,
pBlock, pBlock,
currRequest.offset, currRequest.offset,
alignment, alignment,
@ -12059,6 +12046,7 @@ VkResult VmaBlockVector::CreateBlock(VkDeviceSize blockSize, size_t* pNewBlockIn
VmaDeviceMemoryBlock* const pBlock = vma_new(m_hAllocator, VmaDeviceMemoryBlock)(m_hAllocator); VmaDeviceMemoryBlock* const pBlock = vma_new(m_hAllocator, VmaDeviceMemoryBlock)(m_hAllocator);
pBlock->Init( pBlock->Init(
m_hAllocator, m_hAllocator,
m_hParentPool,
m_MemoryTypeIndex, m_MemoryTypeIndex,
mem, mem,
allocInfo.allocationSize, allocInfo.allocationSize,
@ -13393,7 +13381,7 @@ void VmaDefragmentationContext_T::AddAllocations(
{ {
VmaBlockVectorDefragmentationContext* pBlockVectorDefragCtx = VMA_NULL; VmaBlockVectorDefragmentationContext* pBlockVectorDefragCtx = VMA_NULL;
const VmaPool hAllocPool = hAlloc->GetPool(); const VmaPool hAllocPool = hAlloc->GetBlock()->GetParentPool();
// This allocation belongs to custom pool. // This allocation belongs to custom pool.
if(hAllocPool != VK_NULL_HANDLE) if(hAllocPool != VK_NULL_HANDLE)
{ {
@ -14171,6 +14159,7 @@ VmaAllocator_T::VmaAllocator_T(const VmaAllocatorCreateInfo* pCreateInfo) :
m_pBlockVectors[memTypeIndex] = vma_new(this, VmaBlockVector)( m_pBlockVectors[memTypeIndex] = vma_new(this, VmaBlockVector)(
this, this,
VK_NULL_HANDLE, // hParentPool
memTypeIndex, memTypeIndex,
preferredBlockSize, preferredBlockSize,
0, 0,
@ -14396,7 +14385,6 @@ VkResult VmaAllocator_T::AllocateMemoryOfType(
else else
{ {
VkResult res = blockVector->Allocate( VkResult res = blockVector->Allocate(
VK_NULL_HANDLE, // hCurrentPool
m_CurrentFrameIndex.load(), m_CurrentFrameIndex.load(),
size, size,
alignment, alignment,
@ -14711,7 +14699,6 @@ VkResult VmaAllocator_T::AllocateMemory(
vkMemReq.alignment, vkMemReq.alignment,
GetMemoryTypeMinAlignment(createInfo.pool->m_BlockVector.GetMemoryTypeIndex())); GetMemoryTypeMinAlignment(createInfo.pool->m_BlockVector.GetMemoryTypeIndex()));
return createInfo.pool->m_BlockVector.Allocate( return createInfo.pool->m_BlockVector.Allocate(
createInfo.pool,
m_CurrentFrameIndex.load(), m_CurrentFrameIndex.load(),
vkMemReq.size, vkMemReq.size,
alignmentForPool, alignmentForPool,
@ -14820,7 +14807,7 @@ void VmaAllocator_T::FreeMemory(
case VmaAllocation_T::ALLOCATION_TYPE_BLOCK: case VmaAllocation_T::ALLOCATION_TYPE_BLOCK:
{ {
VmaBlockVector* pBlockVector = VMA_NULL; VmaBlockVector* pBlockVector = VMA_NULL;
VmaPool hPool = allocation->GetPool(); VmaPool hPool = allocation->GetBlock()->GetParentPool();
if(hPool != VK_NULL_HANDLE) if(hPool != VK_NULL_HANDLE)
{ {
pBlockVector = &hPool->m_BlockVector; pBlockVector = &hPool->m_BlockVector;