Fix regression in Dictionary capacity and use similar improvement in HashSet

This commit is contained in:
Wojtek Figat
2025-01-21 15:38:14 +01:00
parent 236b8d5667
commit b247070840
2 changed files with 5 additions and 4 deletions

View File

@@ -407,7 +407,7 @@ public:
Compact();
// Ensure to have enough memory for the next item (in case of new element insertion)
EnsureCapacity(_elementsCount + 1 + _deletedCount);
EnsureCapacity(((_elementsCount + 1) * DICTIONARY_DEFAULT_SLACK_SCALE + _deletedCount) / DICTIONARY_DEFAULT_SLACK_SCALE);
// Find location of the item or place to insert it
FindPositionResult pos;
@@ -940,7 +940,7 @@ private:
Compact();
// Ensure to have enough memory for the next item (in case of new element insertion)
EnsureCapacity(_elementsCount + 1 + _deletedCount);
EnsureCapacity(((_elementsCount + 1) * DICTIONARY_DEFAULT_SLACK_SCALE + _deletedCount) / DICTIONARY_DEFAULT_SLACK_SCALE);
// Find location of the item or place to insert it
FindPositionResult pos;

View File

@@ -470,8 +470,9 @@ public:
/// </summary>
/// <param name="minCapacity">The minimum required capacity.</param>
/// <param name="preserveContents">True if preserve collection data when changing its size, otherwise collection after resize will be empty.</param>
void EnsureCapacity(const int32 minCapacity, const bool preserveContents = true)
void EnsureCapacity(int32 minCapacity, const bool preserveContents = true)
{
minCapacity *= DICTIONARY_DEFAULT_SLACK_SCALE;
if (_size >= minCapacity)
return;
int32 capacity = _allocation.CalculateCapacityGrow(_size, minCapacity);
@@ -734,7 +735,7 @@ private:
Compact();
// Ensure to have enough memory for the next item (in case of new element insertion)
EnsureCapacity((_elementsCount + 1) * DICTIONARY_DEFAULT_SLACK_SCALE + _deletedCount);
EnsureCapacity(((_elementsCount + 1) * DICTIONARY_DEFAULT_SLACK_SCALE + _deletedCount) / DICTIONARY_DEFAULT_SLACK_SCALE);
// Find location of the item or place to insert it
FindPositionResult pos;