Skip to content

Commit c317c7b

Browse files
Internal improvement: moved setting of incremental sort in block vector to separate variable.
Code by @medranSolus
1 parent 31910c8 commit c317c7b

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

include/vk_mem_alloc.h

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10925,7 +10925,7 @@ class VmaBlockVector
1092510925
size_t allocationCount,
1092610926
VmaAllocation* pAllocations);
1092710927

10928-
void Free(const VmaAllocation hAllocation, bool incrementalSort = true);
10928+
void Free(const VmaAllocation hAllocation);
1092910929

1093010930
#if VMA_STATS_STRING_ENABLED
1093110931
void PrintDetailedMap(class VmaJsonWriter& json);
@@ -10951,6 +10951,9 @@ class VmaBlockVector
1095110951
// Incrementally sorted by sumFreeSize, ascending.
1095210952
VmaVector<VmaDeviceMemoryBlock*, VmaStlAllocator<VmaDeviceMemoryBlock*>> m_Blocks;
1095310953
uint32_t m_NextBlockId;
10954+
bool m_IncrementalSort = true;
10955+
10956+
void SetIncrementalSort(bool val) { m_IncrementalSort = val; }
1095410957

1095510958
VkDeviceSize CalcMaxBlockSize() const;
1095610959
// Finds and removes given block from vector.
@@ -12622,9 +12625,7 @@ VkResult VmaBlockVector::AllocatePage(
1262212625
return VK_ERROR_OUT_OF_DEVICE_MEMORY;
1262312626
}
1262412627

12625-
void VmaBlockVector::Free(
12626-
const VmaAllocation hAllocation,
12627-
bool incrementalSort)
12628+
void VmaBlockVector::Free(const VmaAllocation hAllocation)
1262812629
{
1262912630
VmaDeviceMemoryBlock* pBlockToDelete = VMA_NULL;
1263012631

@@ -12684,8 +12685,7 @@ void VmaBlockVector::Free(
1268412685
}
1268512686
}
1268612687

12687-
if (incrementalSort)
12688-
IncrementallySortBlocks();
12688+
IncrementallySortBlocks();
1268912689
}
1269012690

1269112691
// Destruction of a free block. Deferred until this point, outside of mutex
@@ -12730,6 +12730,8 @@ void VmaBlockVector::Remove(VmaDeviceMemoryBlock* pBlock)
1273012730

1273112731
void VmaBlockVector::IncrementallySortBlocks()
1273212732
{
12733+
if (!m_IncrementalSort)
12734+
return;
1273312735
if (m_Algorithm != VMA_POOL_CREATE_LINEAR_ALGORITHM_BIT)
1273412736
{
1273512737
// Bubble sort only until first swap.
@@ -13013,6 +13015,7 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T(
1301313015
m_BlockVectorCount = 1;
1301413016
m_PoolBlockVector = &info.pool->m_BlockVector;
1301513017
m_pBlockVectors = &m_PoolBlockVector;
13018+
m_PoolBlockVector->SetIncrementalSort(false);
1301613019
m_PoolBlockVector->SortByFreeSize();
1301713020
}
1301813021
else
@@ -13024,7 +13027,10 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T(
1302413027
{
1302513028
VmaBlockVector* vector = m_pBlockVectors[i];
1302613029
if (vector != VMA_NULL)
13030+
{
13031+
vector->SetIncrementalSort(false);
1302713032
vector->SortByFreeSize();
13033+
}
1302813034
}
1302913035
}
1303013036

@@ -13050,6 +13056,20 @@ VmaDefragmentationContext_T::VmaDefragmentationContext_T(
1305013056

1305113057
VmaDefragmentationContext_T::~VmaDefragmentationContext_T()
1305213058
{
13059+
if (m_PoolBlockVector != VMA_NULL)
13060+
{
13061+
m_PoolBlockVector->SetIncrementalSort(true);
13062+
}
13063+
else
13064+
{
13065+
for (uint32_t i = 0; i < m_BlockVectorCount; ++i)
13066+
{
13067+
VmaBlockVector* vector = m_pBlockVectors[i];
13068+
if (vector != VMA_NULL)
13069+
vector->SetIncrementalSort(true);
13070+
}
13071+
}
13072+
1305313073
if (m_AlgorithmState)
1305413074
{
1305513075
switch (m_Algorithm)
@@ -13169,7 +13189,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
1316913189
prevCount = vector->GetBlockCount();
1317013190
freedBlockSize = move.dstTmpAllocation->GetBlock()->m_pMetadata->GetSize();
1317113191
}
13172-
vector->Free(move.dstTmpAllocation, false);
13192+
vector->Free(move.dstTmpAllocation);
1317313193
{
1317413194
VmaMutexLockRead lock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex);
1317513195
currentCount = vector->GetBlockCount();
@@ -13182,7 +13202,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
1318213202
{
1318313203
m_PassStats.bytesMoved -= move.srcAllocation->GetSize();
1318413204
--m_PassStats.allocationsMoved;
13185-
vector->Free(move.dstTmpAllocation, false);
13205+
vector->Free(move.dstTmpAllocation);
1318613206

1318713207
VmaDeviceMemoryBlock* newBlock = move.srcAllocation->GetBlock();
1318813208
bool notPresent = true;
@@ -13208,7 +13228,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
1320813228
prevCount = vector->GetBlockCount();
1320913229
freedBlockSize = move.srcAllocation->GetBlock()->m_pMetadata->GetSize();
1321013230
}
13211-
vector->Free(move.srcAllocation, false);
13231+
vector->Free(move.srcAllocation);
1321213232
{
1321313233
VmaMutexLockRead lock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex);
1321413234
currentCount = vector->GetBlockCount();
@@ -13220,7 +13240,7 @@ VkResult VmaDefragmentationContext_T::DefragmentPassEnd(VmaDefragmentationPassMo
1322013240
VmaMutexLockRead lock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex);
1322113241
dstBlockSize = move.dstTmpAllocation->GetBlock()->m_pMetadata->GetSize();
1322213242
}
13223-
vector->Free(move.dstTmpAllocation, false);
13243+
vector->Free(move.dstTmpAllocation);
1322413244
{
1322513245
VmaMutexLockRead lock(vector->GetMutex(), vector->GetAllocator()->m_UseMutex);
1322613246
freedBlockSize += dstBlockSize * (currentCount - vector->GetBlockCount());

0 commit comments

Comments
 (0)