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

@@ -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);
}
};