Refactor and improve collections code

#3043
This commit is contained in:
Wojtek Figat
2025-01-23 14:44:11 +01:00
parent af416fe0c8
commit f5280eab74
13 changed files with 277 additions and 268 deletions

View File

@@ -608,7 +608,7 @@ int32 Editor::LoadProduct()
// Validate project min supported version (older engine may try to load newer project)
// Special check if project specifies only build number, then major/minor fields are set to 0
const auto engineVersion = FLAXENGINE_VERSION;
for (auto e : projects)
for (const auto& e : projects)
{
const auto project = e.Item;
if (project->MinEngineVersion > engineVersion ||

View File

@@ -582,7 +582,7 @@ bool ScriptsBuilderService::Init()
auto project = Editor::Project;
HashSet<ProjectInfo*> projects;
project->GetAllProjects(projects);
for (auto e : projects)
for (const auto& e : projects)
{
ProjectInfo* project = e.Item;
if (project->Name == TEXT("Flax"))

View File

@@ -6,6 +6,7 @@
#include "Engine/Platform/Platform.h"
#include "Engine/Core/Memory/Memory.h"
#include "Engine/Core/Memory/Allocation.h"
#include "Engine/Core/Memory/AllocationUtils.h"
/// <summary>
/// Template for dynamic array with variable capacity.
@@ -25,22 +26,9 @@ private:
int32 _capacity;
AllocationData _allocation;
FORCE_INLINE static void MoveToEmpty(AllocationData& to, AllocationData& from, const int32 fromCount, const int32 fromCapacity)
{
if IF_CONSTEXPR (AllocationType::HasSwap)
to.Swap(from);
else
{
to.Allocate(fromCapacity);
Memory::MoveItems(to.Get(), from.Get(), fromCount);
Memory::DestructItems(from.Get(), fromCount);
from.Free();
}
}
public:
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes an empty <see cref="Array"/> without reserving any space.
/// </summary>
FORCE_INLINE Array()
: _count(0)
@@ -49,10 +37,10 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by reserving space.
/// </summary>
/// <param name="capacity">The initial capacity.</param>
explicit Array(const int32 capacity)
/// <param name="capacity">The number of elements that can be added without a need to allocate more memory.</param>
FORCE_INLINE explicit Array(const int32 capacity)
: _count(0)
, _capacity(capacity)
{
@@ -61,21 +49,7 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// </summary>
/// <param name="initList">The initial values defined in the array.</param>
Array(std::initializer_list<T> initList)
{
_count = _capacity = static_cast<int32>(initList.size());
if (_count > 0)
{
_allocation.Allocate(_count);
Memory::ConstructItems(_allocation.Get(), initList.begin(), _count);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by copying elements.
/// </summary>
/// <param name="data">The initial data.</param>
/// <param name="length">The amount of items.</param>
@@ -91,38 +65,25 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by copying listed elements.
/// </summary>
/// <param name="other">The other collection to copy.</param>
Array(const Array& other)
/// <param name="initList">The initial values defined in the array.</param>
FORCE_INLINE Array(std::initializer_list<T> initList)
: Array(initList.begin(), (int32)initList.size())
{
_count = _capacity = other._count;
if (_capacity > 0)
{
_allocation.Allocate(_capacity);
Memory::ConstructItems(_allocation.Get(), other.Get(), other._count);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by copying the elements from the other collection.
/// </summary>
/// <param name="other">The other collection to copy.</param>
/// <param name="extraSize">The additionally amount of items to add to the add.</param>
Array(const Array& other, int32 extraSize)
FORCE_INLINE Array(const Array& other)
: Array(other.Get(), other.Count())
{
ASSERT(extraSize >= 0);
_count = _capacity = other._count + extraSize;
if (_capacity > 0)
{
_allocation.Allocate(_capacity);
Memory::ConstructItems(_allocation.Get(), other.Get(), other._count);
Memory::ConstructItems(_allocation.Get() + other._count, extraSize);
}
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by copying the elements from the other collection.
/// </summary>
/// <param name="other">The other collection to copy.</param>
template<typename Other = T, typename OtherAllocationType = AllocationType>
@@ -138,7 +99,7 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="Array"/> class.
/// Initializes <see cref="Array"/> by moving the content of the other collection.
/// </summary>
/// <param name="other">The other collection to move.</param>
Array(Array&& other) noexcept
@@ -147,7 +108,7 @@ public:
_capacity = other._capacity;
other._count = 0;
other._capacity = 0;
MoveToEmpty(_allocation, other._allocation, _count, _capacity);
AllocationUtils::MoveToEmpty<T, AllocationType>(_allocation, other._allocation, _count, _capacity);
}
/// <summary>
@@ -204,7 +165,7 @@ public:
_capacity = other._capacity;
other._count = 0;
other._capacity = 0;
MoveToEmpty(_allocation, other._allocation, _count, _capacity);
AllocationUtils::MoveToEmpty<T, AllocationType>(_allocation, other._allocation, _count, _capacity);
}
return *this;
}
@@ -377,10 +338,13 @@ public:
/// <summary>
/// Clear the collection without changing its capacity.
/// </summary>
FORCE_INLINE void Clear()
void Clear()
{
Memory::DestructItems(_allocation.Get(), _count);
_count = 0;
if (_count != 0)
{
Memory::DestructItems(_allocation.Get(), _count);
_count = 0;
}
}
/// <summary>

View File

@@ -34,7 +34,7 @@ private:
public:
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// Initializes an empty <see cref="BitArray"/> without reserving any space.
/// </summary>
FORCE_INLINE BitArray()
: _count(0)
@@ -43,9 +43,9 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// Initializes <see cref="BitArray"/> by reserving space.
/// </summary>
/// <param name="capacity">The initial capacity.</param>
/// <param name="capacity">The number of elements that can be added without a need to allocate more memory.</param>
explicit BitArray(const int32 capacity)
: _count(0)
, _capacity(capacity)
@@ -55,7 +55,7 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="BitArray"/> class.
/// Initializes <see cref="BitArray"/> by copying the elements from the other collection.
/// </summary>
/// <param name="other">The other collection to copy.</param>
BitArray(const BitArray& other) noexcept

View File

@@ -4,6 +4,7 @@
#include "Engine/Core/Memory/Memory.h"
#include "Engine/Core/Memory/Allocation.h"
#include "Engine/Core/Memory/AllocationUtils.h"
#include "Engine/Core/Collections/BucketState.h"
#include "Engine/Core/Collections/HashFunctions.h"
#include "Engine/Core/Collections/Config.h"
@@ -25,6 +26,7 @@ public:
struct Bucket
{
friend Dictionary;
friend Memory;
/// <summary>The key.</summary>
KeyType Key;
@@ -34,6 +36,57 @@ public:
private:
BucketState _state;
Bucket()
: _state(BucketState::Empty)
{
}
Bucket(Bucket&& other) noexcept
{
_state = other._state;
if (other._state == BucketState::Occupied)
{
Memory::MoveItems(&Key, &other.Key, 1);
Memory::MoveItems(&Value, &other.Value, 1);
other._state = BucketState::Empty;
}
}
Bucket& operator=(Bucket&& other) noexcept
{
if (this != &other)
{
if (_state == BucketState::Occupied)
{
Memory::DestructItem(&Key);
Memory::DestructItem(&Value);
}
_state = other._state;
if (other._state == BucketState::Occupied)
{
Memory::MoveItems(&Key, &other.Key, 1);
Memory::MoveItems(&Value, &other.Value, 1);
other._state = BucketState::Empty;
}
}
return *this;
}
/// <summary>Copying a bucket is useless, because a key must be unique in the dictionary.</summary>
Bucket(const Bucket&) = delete;
/// <summary>Copying a bucket is useless, because a key must be unique in the dictionary.</summary>
Bucket& operator=(const Bucket&) = delete;
~Bucket()
{
if (_state == BucketState::Occupied)
{
Memory::DestructItem(&Key);
Memory::DestructItem(&Value);
}
}
FORCE_INLINE void Free()
{
if (_state == BucketState::Occupied)
@@ -104,46 +157,19 @@ private:
int32 _size = 0;
AllocationData _allocation;
FORCE_INLINE static void MoveToEmpty(AllocationData& to, AllocationData& from, const int32 fromSize)
{
if IF_CONSTEXPR (AllocationType::HasSwap)
to.Swap(from);
else
{
to.Allocate(fromSize);
Bucket* toData = to.Get();
Bucket* fromData = from.Get();
for (int32 i = 0; i < fromSize; i++)
{
Bucket& fromBucket = fromData[i];
if (fromBucket.IsOccupied())
{
Bucket& toBucket = toData[i];
Memory::MoveItems(&toBucket.Key, &fromBucket.Key, 1);
Memory::MoveItems(&toBucket.Value, &fromBucket.Value, 1);
toBucket._state = BucketState::Occupied;
Memory::DestructItem(&fromBucket.Key);
Memory::DestructItem(&fromBucket.Value);
fromBucket._state = BucketState::Empty;
}
}
from.Free();
}
}
public:
/// <summary>
/// Initializes a new instance of the <see cref="Dictionary"/> class.
/// Initializes an empty <see cref="Dictionary"/> without reserving any space.
/// </summary>
Dictionary()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="Dictionary"/> class.
/// Initializes <see cref="Dictionary"/> by reserving space.
/// </summary>
/// <param name="capacity">The initial capacity.</param>
explicit Dictionary(const int32 capacity)
/// <param name="capacity">The number of elements that can be added without a need to allocate more memory.</param>
FORCE_INLINE explicit Dictionary(const int32 capacity)
{
SetCapacity(capacity);
}
@@ -160,11 +186,11 @@ public:
other._elementsCount = 0;
other._deletedCount = 0;
other._size = 0;
MoveToEmpty(_allocation, other._allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(_allocation, other._allocation, _size, _size);
}
/// <summary>
/// Initializes a new instance of the <see cref="Dictionary"/> class.
/// Initializes <see cref="Dictionary"/> by copying the elements from the other collection.
/// </summary>
/// <param name="other">Other collection to copy</param>
Dictionary(const Dictionary& other)
@@ -201,7 +227,7 @@ public:
other._elementsCount = 0;
other._deletedCount = 0;
other._size = 0;
MoveToEmpty(_allocation, other._allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(_allocation, other._allocation, _size, _size);
}
return *this;
}
@@ -536,25 +562,16 @@ public:
/// <param name="preserveContents">Enables preserving collection contents during resizing.</param>
void SetCapacity(int32 capacity, const bool preserveContents = true)
{
if (capacity == Capacity())
if (capacity == _size)
return;
ASSERT(capacity >= 0);
AllocationData oldAllocation;
MoveToEmpty(oldAllocation, _allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(oldAllocation, _allocation, _size, _size);
const int32 oldSize = _size;
const int32 oldElementsCount = _elementsCount;
_deletedCount = _elementsCount = 0;
if (capacity != 0 && (capacity & (capacity - 1)) != 0)
{
// Align capacity value to the next power of two (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2)
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
capacity++;
}
capacity = AllocationUtils::AlignToPowerOf2(capacity);
if (capacity)
{
_allocation.Allocate(capacity);
@@ -574,11 +591,9 @@ public:
{
FindPosition(oldBucket.Key, pos);
ASSERT(pos.FreeSlotIndex != -1);
Bucket* bucket = &_allocation.Get()[pos.FreeSlotIndex];
Memory::MoveItems(&bucket->Key, &oldBucket.Key, 1);
Memory::MoveItems(&bucket->Value, &oldBucket.Value, 1);
bucket->_state = BucketState::Occupied;
++_elementsCount;
Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex];
bucket = MoveTemp(oldBucket);
_elementsCount++;
}
}
}
@@ -968,7 +983,7 @@ private:
{
// Rebuild entire table completely
AllocationData oldAllocation;
MoveToEmpty(oldAllocation, _allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(oldAllocation, _allocation, _size, _size);
_allocation.Allocate(_size);
Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; i++)
@@ -982,10 +997,8 @@ private:
{
FindPosition(oldBucket.Key, pos);
ASSERT(pos.FreeSlotIndex != -1);
Bucket* bucket = &_allocation.Get()[pos.FreeSlotIndex];
Memory::MoveItems(&bucket->Key, &oldBucket.Key, 1);
Memory::MoveItems(&bucket->Value, &oldBucket.Value, 1);
bucket->_state = BucketState::Occupied;
Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex];
bucket = MoveTemp(oldBucket);
}
}
for (int32 i = 0; i < _size; i++)

View File

@@ -4,6 +4,7 @@
#include "Engine/Core/Memory/Memory.h"
#include "Engine/Core/Memory/Allocation.h"
#include "Engine/Core/Memory/AllocationUtils.h"
#include "Engine/Core/Collections/BucketState.h"
#include "Engine/Core/Collections/HashFunctions.h"
#include "Engine/Core/Collections/Config.h"
@@ -24,6 +25,7 @@ public:
struct Bucket
{
friend HashSet;
friend Memory;
/// <summary>The item.</summary>
T Item;
@@ -31,6 +33,51 @@ public:
private:
BucketState _state;
Bucket()
: _state(BucketState::Empty)
{
}
Bucket(Bucket&& other) noexcept
{
_state = other._state;
if (other._state == BucketState::Occupied)
{
Memory::MoveItems(&Item, &other.Item, 1);
other._state = BucketState::Empty;
}
}
Bucket& operator=(Bucket&& other) noexcept
{
if (this != &other)
{
if (_state == BucketState::Occupied)
{
Memory::DestructItem(&Item);
}
_state = other._state;
if (other._state == BucketState::Occupied)
{
Memory::MoveItems(&Item, &other.Item, 1);
other._state = BucketState::Empty;
}
}
return *this;
}
/// <summary>Copying a bucket is useless, because a key must be unique in the dictionary.</summary>
Bucket(const Bucket&) = delete;
/// <summary>Copying a bucket is useless, because a key must be unique in the dictionary.</summary>
Bucket& operator=(const Bucket&) = delete;
~Bucket()
{
if (_state == BucketState::Occupied)
Memory::DestructItem(&Item);
}
FORCE_INLINE void Free()
{
if (_state == BucketState::Occupied)
@@ -87,44 +134,19 @@ private:
int32 _size = 0;
AllocationData _allocation;
FORCE_INLINE static void MoveToEmpty(AllocationData& to, AllocationData& from, const int32 fromSize)
{
if IF_CONSTEXPR (AllocationType::HasSwap)
to.Swap(from);
else
{
to.Allocate(fromSize);
Bucket* toData = to.Get();
Bucket* fromData = from.Get();
for (int32 i = 0; i < fromSize; ++i)
{
Bucket& fromBucket = fromData[i];
if (fromBucket.IsOccupied())
{
Bucket& toBucket = toData[i];
Memory::MoveItems(&toBucket.Item, &fromBucket.Item, 1);
toBucket._state = BucketState::Occupied;
Memory::DestructItem(&fromBucket.Item);
fromBucket._state = BucketState::Empty;
}
}
from.Free();
}
}
public:
/// <summary>
/// Initializes a new instance of the <see cref="HashSet"/> class.
/// Initializes an empty <see cref="HashSet"/> without reserving any space.
/// </summary>
HashSet()
{
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet"/> class.
/// Initializes <see cref="HashSet"/> by reserving space.
/// </summary>
/// <param name="capacity">The initial capacity.</param>
explicit HashSet(const int32 capacity)
/// <param name="capacity">The number of elements that can be added without a need to allocate more memory.</param>
FORCE_INLINE explicit HashSet(const int32 capacity)
{
SetCapacity(capacity);
}
@@ -141,11 +163,11 @@ public:
other._elementsCount = 0;
other._deletedCount = 0;
other._size = 0;
MoveToEmpty(_allocation, other._allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(_allocation, other._allocation, _size, _size);
}
/// <summary>
/// Initializes a new instance of the <see cref="HashSet"/> class.
/// Initializes <see cref="HashSet"/> by copying the elements from the other collection.
/// </summary>
/// <param name="other">Other collection to copy</param>
HashSet(const HashSet& other)
@@ -182,7 +204,7 @@ public:
other._elementsCount = 0;
other._deletedCount = 0;
other._size = 0;
MoveToEmpty(_allocation, other._allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(_allocation, other._allocation, _size, _size);
}
return *this;
}
@@ -413,25 +435,16 @@ public:
/// <param name="preserveContents">Enable/disable preserving collection contents during resizing</param>
void SetCapacity(int32 capacity, const bool preserveContents = true)
{
if (capacity == Capacity())
if (capacity == _size)
return;
ASSERT(capacity >= 0);
AllocationData oldAllocation;
MoveToEmpty(oldAllocation, _allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(oldAllocation, _allocation, _size, _size);
const int32 oldSize = _size;
const int32 oldElementsCount = _elementsCount;
_deletedCount = _elementsCount = 0;
if (capacity != 0 && (capacity & (capacity - 1)) != 0)
{
// Align capacity value to the next power of two (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2)
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
capacity++;
}
capacity = AllocationUtils::AlignToPowerOf2(capacity);
if (capacity)
{
_allocation.Allocate(capacity);
@@ -451,9 +464,8 @@ public:
{
FindPosition(oldBucket.Item, pos);
ASSERT(pos.FreeSlotIndex != -1);
Bucket* bucket = &_allocation.Get()[pos.FreeSlotIndex];
Memory::MoveItems(&bucket->Item, &oldBucket.Item, 1);
bucket->_state = BucketState::Occupied;
Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex];
bucket = MoveTemp(oldBucket);
_elementsCount++;
}
}
@@ -764,7 +776,7 @@ private:
{
// Rebuild entire table completely
AllocationData oldAllocation;
MoveToEmpty(oldAllocation, _allocation, _size);
AllocationUtils::MoveToEmpty<Bucket, AllocationType>(oldAllocation, _allocation, _size, _size);
_allocation.Allocate(_size);
Bucket* data = _allocation.Get();
for (int32 i = 0; i < _size; ++i)
@@ -778,9 +790,8 @@ private:
{
FindPosition(oldBucket.Item, pos);
ASSERT(pos.FreeSlotIndex != -1);
Bucket* bucket = &_allocation.Get()[pos.FreeSlotIndex];
Memory::MoveItems(&bucket->Item, &oldBucket.Item, 1);
bucket->_state = BucketState::Occupied;
Bucket& bucket = _allocation.Get()[pos.FreeSlotIndex];
bucket = MoveTemp(oldBucket);
}
}
for (int32 i = 0; i < _size; ++i)

View File

@@ -5,6 +5,39 @@
#include "Memory.h"
#include "Engine/Core/Core.h"
namespace AllocationUtils
{
// Rounds up the input value to the next power of 2 to be used as bigger memory allocation block. Handles overflow.
inline int32 RoundUpToPowerOf2(int32 capacity)
{
// Reference: http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
uint64 capacity64 = (uint64)(capacity + 1) * 2;
if (capacity64 > MAX_int32)
capacity64 = MAX_int32;
return (int32)capacity64;
}
// Aligns the input value to the next power of 2 to be used as bigger memory allocation block.
inline int32 AlignToPowerOf2(int32 capacity)
{
// Reference: http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
capacity++;
return capacity;
}
}
/// <summary>
/// The memory allocation policy that uses inlined memory of the fixed size (no resize support, does not use heap allocations at all).
/// </summary>
@@ -47,16 +80,12 @@ public:
FORCE_INLINE void Allocate(const int32 capacity)
{
#if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(capacity <= Capacity);
#endif
ASSERT_LOW_LAYER(capacity <= Capacity);
}
FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount)
{
#if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(capacity <= Capacity);
#endif
ASSERT_LOW_LAYER(capacity <= Capacity);
}
FORCE_INLINE void Free()
@@ -71,7 +100,7 @@ public:
};
/// <summary>
/// The memory allocation policy that uses default heap allocator.
/// The memory allocation policy that uses default heap allocation.
/// </summary>
class HeapAllocation
{
@@ -109,33 +138,17 @@ public:
if (capacity < minCapacity)
capacity = minCapacity;
if (capacity < 8)
{
capacity = 8;
}
else
{
// Round up to the next power of 2 and multiply by 2 (http://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2)
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
uint64 capacity64 = (uint64)(capacity + 1) * 2;
if (capacity64 > MAX_int32)
capacity64 = MAX_int32;
capacity = (int32)capacity64;
}
capacity = AllocationUtils::RoundUpToPowerOf2(capacity);
return capacity;
}
FORCE_INLINE void Allocate(const int32 capacity)
{
#if ENABLE_ASSERTION_LOW_LAYERS
ASSERT(!_data);
#endif
ASSERT_LOW_LAYER(!_data);
_data = static_cast<T*>(Allocator::Allocate(capacity * sizeof(T)));
#if !BUILD_RELEASE
#if ENABLE_ASSERTION
if (!_data)
OUT_OF_MEMORY;
#endif
@@ -144,7 +157,7 @@ public:
FORCE_INLINE void Relocate(const int32 capacity, int32 oldCount, int32 newCount)
{
T* newData = capacity != 0 ? static_cast<T*>(Allocator::Allocate(capacity * sizeof(T))) : nullptr;
#if !BUILD_RELEASE
#if ENABLE_ASSERTION
if (!newData && capacity != 0)
OUT_OF_MEMORY;
#endif
@@ -176,7 +189,7 @@ public:
/// <summary>
/// The memory allocation policy that uses inlined memory of the fixed size and supports using additional allocation to increase its capacity (eg. via heap allocation).
/// </summary>
template<int Capacity, typename OtherAllocator = HeapAllocation>
template<int Capacity, typename FallbackAllocation = HeapAllocation>
class InlinedAllocation
{
public:
@@ -186,11 +199,11 @@ public:
class alignas(sizeof(void*)) Data
{
private:
typedef typename OtherAllocator::template Data<T> OtherData;
typedef typename FallbackAllocation::template Data<T> FallbackData;
bool _useOther = false;
bool _useFallback = false;
byte _data[Capacity * sizeof(T)];
OtherData _other;
FallbackData _fallback;
public:
FORCE_INLINE Data()
@@ -203,25 +216,25 @@ public:
FORCE_INLINE T* Get()
{
return _useOther ? _other.Get() : reinterpret_cast<T*>(_data);
return _useFallback ? _fallback.Get() : reinterpret_cast<T*>(_data);
}
FORCE_INLINE const T* Get() const
{
return _useOther ? _other.Get() : reinterpret_cast<const T*>(_data);
return _useFallback ? _fallback.Get() : reinterpret_cast<const T*>(_data);
}
FORCE_INLINE int32 CalculateCapacityGrow(int32 capacity, int32 minCapacity) const
{
return minCapacity <= Capacity ? Capacity : _other.CalculateCapacityGrow(capacity, minCapacity);
return minCapacity <= Capacity ? Capacity : _fallback.CalculateCapacityGrow(capacity, minCapacity);
}
FORCE_INLINE void Allocate(int32 capacity)
{
if (capacity > Capacity)
{
_useOther = true;
_other.Allocate(capacity);
_useFallback = true;
_fallback.Allocate(capacity);
}
}
@@ -232,32 +245,32 @@ public:
// Check if the new allocation will fit into inlined storage
if (capacity <= Capacity)
{
if (_useOther)
if (_useFallback)
{
// Move the items from other allocation to the inlined storage
Memory::MoveItems(data, _other.Get(), newCount);
Memory::MoveItems(data, _fallback.Get(), newCount);
// Free the other allocation
Memory::DestructItems(_other.Get(), oldCount);
_other.Free();
_useOther = false;
Memory::DestructItems(_fallback.Get(), oldCount);
_fallback.Free();
_useFallback = false;
}
}
else
{
if (_useOther)
if (_useFallback)
{
// Resize other allocation
_other.Relocate(capacity, oldCount, newCount);
_fallback.Relocate(capacity, oldCount, newCount);
}
else
{
// Allocate other allocation
_other.Allocate(capacity);
_useOther = true;
_fallback.Allocate(capacity);
_useFallback = true;
// Move the items from the inlined storage to the other allocation
Memory::MoveItems(_other.Get(), data, newCount);
Memory::MoveItems(_fallback.Get(), data, newCount);
Memory::DestructItems(data, oldCount);
}
}
@@ -265,10 +278,10 @@ public:
FORCE_INLINE void Free()
{
if (_useOther)
if (_useFallback)
{
_useOther = false;
_other.Free();
_useFallback = false;
_fallback.Free();
}
}

View File

@@ -0,0 +1,24 @@
// Copyright (c) 2012-2024 Wojciech Figat. All rights reserved.
#pragma once
#include "Allocation.h"
#include "Engine/Core/Templates.h"
namespace AllocationUtils
{
// Moves the data from the source allocation to the destination allocation.
template<typename T, typename AllocationType>
inline void MoveToEmpty(typename AllocationType::template Data<T>& to, typename AllocationType::template Data<T>& from, const int32 fromCount, const int32 fromCapacity)
{
if IF_CONSTEXPR (AllocationType::HasSwap)
to.Swap(from);
else
{
to.Allocate(fromCapacity);
Memory::MoveItems(to.Get(), from.Get(), fromCount);
Memory::DestructItems(from.Get(), fromCount);
from.Free();
}
}
}

View File

@@ -91,15 +91,16 @@ namespace AllocatorExt
}
}
namespace Memory
class Memory
{
public:
/// <summary>
/// Constructs the item in the memory.
/// </summary>
/// <remarks>The optimized version is noop.</remarks>
/// <param name="dst">The address of the memory location to construct.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<!TIsTriviallyConstructible<T>::Value>::Type ConstructItem(T* dst)
FORCE_INLINE static typename TEnableIf<!TIsTriviallyConstructible<T>::Value>::Type ConstructItem(T* dst)
{
new(dst) T();
}
@@ -110,7 +111,7 @@ namespace Memory
/// <remarks>The optimized version is noop.</remarks>
/// <param name="dst">The address of the memory location to construct.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<TIsTriviallyConstructible<T>::Value>::Type ConstructItem(T* dst)
FORCE_INLINE static typename TEnableIf<TIsTriviallyConstructible<T>::Value>::Type ConstructItem(T* dst)
{
// More undefined behavior! No more clean memory! More performance! Yay!
//Platform::MemoryClear(dst, sizeof(T));
@@ -123,7 +124,7 @@ namespace Memory
/// <param name="dst">The address of the first memory location to construct.</param>
/// <param name="count">The number of element to construct. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<!TIsTriviallyConstructible<T>::Value>::Type ConstructItems(T* dst, int32 count)
FORCE_INLINE static typename TEnableIf<!TIsTriviallyConstructible<T>::Value>::Type ConstructItems(T* dst, int32 count)
{
while (count--)
{
@@ -139,7 +140,7 @@ namespace Memory
/// <param name="dst">The address of the first memory location to construct.</param>
/// <param name="count">The number of element to construct. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<TIsTriviallyConstructible<T>::Value>::Type ConstructItems(T* dst, int32 count)
FORCE_INLINE static typename TEnableIf<TIsTriviallyConstructible<T>::Value>::Type ConstructItems(T* dst, int32 count)
{
// More undefined behavior! No more clean memory! More performance! Yay!
//Platform::MemoryClear(dst, count * sizeof(T));
@@ -153,7 +154,7 @@ namespace Memory
/// <param name="src">The address of the first memory location to pass to the constructor.</param>
/// <param name="count">The number of element to construct. Can be equal 0.</param>
template<typename T, typename U>
FORCE_INLINE typename TEnableIf<!TIsBitwiseConstructible<T, U>::Value>::Type ConstructItems(T* dst, const U* src, int32 count)
FORCE_INLINE static typename TEnableIf<!TIsBitwiseConstructible<T, U>::Value>::Type ConstructItems(T* dst, const U* src, int32 count)
{
while (count--)
{
@@ -171,7 +172,7 @@ namespace Memory
/// <param name="src">The address of the first memory location to pass to the constructor.</param>
/// <param name="count">The number of element to construct. Can be equal 0.</param>
template<typename T, typename U>
FORCE_INLINE typename TEnableIf<TIsBitwiseConstructible<T, U>::Value>::Type ConstructItems(T* dst, const U* src, int32 count)
FORCE_INLINE static typename TEnableIf<TIsBitwiseConstructible<T, U>::Value>::Type ConstructItems(T* dst, const U* src, int32 count)
{
Platform::MemoryCopy(dst, src, count * sizeof(U));
}
@@ -182,7 +183,7 @@ namespace Memory
/// <remarks>The optimized version is noop.</remarks>
/// <param name="dst">The address of the memory location to destruct.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<!TIsTriviallyDestructible<T>::Value>::Type DestructItem(T* dst)
FORCE_INLINE static typename TEnableIf<!TIsTriviallyDestructible<T>::Value>::Type DestructItem(T* dst)
{
dst->~T();
}
@@ -193,7 +194,7 @@ namespace Memory
/// <remarks>The optimized version is noop.</remarks>
/// <param name="dst">The address of the memory location to destruct.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<TIsTriviallyDestructible<T>::Value>::Type DestructItem(T* dst)
FORCE_INLINE static typename TEnableIf<TIsTriviallyDestructible<T>::Value>::Type DestructItem(T* dst)
{
}
@@ -204,7 +205,7 @@ namespace Memory
/// <param name="dst">The address of the first memory location to destruct.</param>
/// <param name="count">The number of element to destruct. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<!TIsTriviallyDestructible<T>::Value>::Type DestructItems(T* dst, int32 count)
FORCE_INLINE static typename TEnableIf<!TIsTriviallyDestructible<T>::Value>::Type DestructItems(T* dst, int32 count)
{
while (count--)
{
@@ -220,7 +221,7 @@ namespace Memory
/// <param name="dst">The address of the first memory location to destruct.</param>
/// <param name="count">The number of element to destruct. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<TIsTriviallyDestructible<T>::Value>::Type DestructItems(T* dst, int32 count)
FORCE_INLINE static typename TEnableIf<TIsTriviallyDestructible<T>::Value>::Type DestructItems(T* dst, int32 count)
{
}
@@ -232,7 +233,7 @@ namespace Memory
/// <param name="src">The address of the first memory location to assign from.</param>
/// <param name="count">The number of element to assign. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<!TIsTriviallyCopyAssignable<T>::Value>::Type CopyItems(T* dst, const T* src, int32 count)
FORCE_INLINE static typename TEnableIf<!TIsTriviallyCopyAssignable<T>::Value>::Type CopyItems(T* dst, const T* src, int32 count)
{
while (count--)
{
@@ -250,7 +251,7 @@ namespace Memory
/// <param name="src">The address of the first memory location to assign from.</param>
/// <param name="count">The number of element to assign. Can be equal 0.</param>
template<typename T>
FORCE_INLINE typename TEnableIf<TIsTriviallyCopyAssignable<T>::Value>::Type CopyItems(T* dst, const T* src, int32 count)
FORCE_INLINE static typename TEnableIf<TIsTriviallyCopyAssignable<T>::Value>::Type CopyItems(T* dst, const T* src, int32 count)
{
Platform::MemoryCopy(dst, src, count * sizeof(T));
}
@@ -263,11 +264,11 @@ namespace Memory
/// <param name="src">The address of the first memory location to pass to the move constructor.</param>
/// <param name="count">The number of element to move. Can be equal 0.</param>
template<typename T, typename U>
FORCE_INLINE typename TEnableIf<!TIsBitwiseConstructible<T, U>::Value>::Type MoveItems(T* dst, const U* src, int32 count)
FORCE_INLINE static typename TEnableIf<!TIsBitwiseConstructible<T, U>::Value>::Type MoveItems(T* dst, U* src, int32 count)
{
while (count--)
{
new(dst) T((T&&)*src);
new(dst) T((U&&)*src);
++(T*&)dst;
++src;
}
@@ -281,11 +282,11 @@ namespace Memory
/// <param name="src">The address of the first memory location to pass to the move constructor.</param>
/// <param name="count">The number of element to move. Can be equal 0.</param>
template<typename T, typename U>
FORCE_INLINE typename TEnableIf<TIsBitwiseConstructible<T, U>::Value>::Type MoveItems(T* dst, const U* src, int32 count)
FORCE_INLINE static typename TEnableIf<TIsBitwiseConstructible<T, U>::Value>::Type MoveItems(T* dst, U* src, int32 count)
{
Platform::MemoryCopy(dst, src, count * sizeof(U));
}
}
};
/// <summary>
/// Creates a new object of the given type.

View File

@@ -234,6 +234,12 @@ struct TIsCopyConstructible
enum { Value = __is_constructible(T, typename TAddLValueReference<typename TAddConst<T>::Type>::Type) };
};
template<typename T>
struct TIsMoveConstructible
{
enum { Value = __is_constructible(T, typename TAddRValueReference<T>::Type) };
};
////////////////////////////////////////////////////////////////////////////////////
// Checks if a type has a trivial copy constructor.

View File

@@ -198,7 +198,7 @@ namespace
const MClass* attribute = ((NativeBinaryModule*)GetBinaryModuleFlaxEngine())->Assembly->GetClass("FlaxEngine.DebugCommand");
ASSERT_LOW_LAYER(attribute);
const auto& classes = managedModule->Assembly->GetClasses();
for (auto e : classes)
for (const auto& e : classes)
{
MClass* mclass = e.Value;
if (mclass->IsGeneric() ||

View File

@@ -30,7 +30,7 @@ private:
public:
/// <summary>
/// Initializes a new instance of the <see cref="RenderListBuffer"/> class.
/// Initializes an empty <see cref="RenderListBuffer"/> without reserving any space.
/// </summary>
FORCE_INLINE RenderListBuffer()
: _count(0)
@@ -39,19 +39,7 @@ public:
}
/// <summary>
/// Initializes a new instance of the <see cref="RenderListBuffer"/> class.
/// </summary>
/// <param name="capacity">The initial capacity.</param>
explicit RenderListBuffer(const int32 capacity)
: _count(0)
, _capacity(capacity)
{
if (capacity > 0)
_allocation.Allocate(capacity);
}
/// <summary>
/// Initializes a new instance of the <see cref="RenderListBuffer"/> class.
/// Initializes <see cref="Array"/> by copying elements.
/// </summary>
/// <param name="data">The initial data.</param>
/// <param name="length">The amount of items.</param>
@@ -369,17 +357,6 @@ private:
{
// Ensure there is a slack for others threads to reduce resize counts in highly multi-threaded environment
constexpr int32 slack = PLATFORM_THREADS_LIMIT * 8;
int32 capacity = count + slack;
{
// Round up to the next power of 2 and multiply by 2
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
capacity = (capacity + 1) * 2;
}
return capacity;
return AllocationUtils::RoundUpToPowerOf2(count + slack);
}
};

View File

@@ -335,17 +335,17 @@ void MCore::UnloadEngine()
void MCore::ReloadScriptingAssemblyLoadContext()
{
// Clear any cached class attributes (see https://github.com/FlaxEngine/FlaxEngine/issues/1108)
for (auto e : CachedClassHandles)
for (const auto& e : CachedClassHandles)
{
e.Value->_hasCachedAttributes = false;
e.Value->_attributes.Clear();
}
for (auto e : CachedAssemblyHandles)
for (const auto& e : CachedAssemblyHandles)
{
MAssembly* a = e.Value;
if (!a->IsLoaded() || !a->_hasCachedClasses)
continue;
for (auto q : a->GetClasses())
for (const auto& q : a->GetClasses())
{
MClass* c = q.Value;
c->_hasCachedAttributes = false;