Fix collections capacity growing to use the closest power of two

Capacity was incorrectly 2x larger than needed.
Added unit test to ensure it stays correct.
This commit is contained in:
Wojtek Figat
2025-12-04 23:29:15 +01:00
parent a1999183f2
commit 3a798a70fa
2 changed files with 14 additions and 3 deletions

View File

@@ -18,9 +18,7 @@ namespace AllocationUtils
capacity |= capacity >> 8;
capacity |= capacity >> 16;
uint64 capacity64 = (uint64)(capacity + 1) * 2;
if (capacity64 > MAX_int32)
capacity64 = MAX_int32;
return (int32)capacity64;
return capacity64 >= MAX_int32 ? MAX_int32 : (int32)capacity64 / 2;
}
// Aligns the input value to the next power of 2 to be used as bigger memory allocation block.