1
0
Fork 0
mirror of https://github.com/beefytech/Beef.git synced 2025-06-14 14:24:10 +02:00

Single allocation for dictionary data instead of two

This commit is contained in:
Brian Fiete 2020-02-01 11:42:57 -08:00
parent 3da6535ef5
commit 0c7597fe60

View file

@ -179,11 +179,12 @@ private:
void Resize(intptr newSize, bool forceNewHashCodes) void Resize(intptr newSize, bool forceNewHashCodes)
{ {
BF_ASSERT(newSize >= mAllocSize); BF_ASSERT(newSize >= mAllocSize);
int_cosize* newBuckets = new int_cosize[newSize]; Entry* newEntries;
int_cosize* newBuckets;
AllocData(newSize, newEntries, newBuckets);
for (int_cosize i = 0; i < newSize; i++) for (int_cosize i = 0; i < newSize; i++)
newBuckets[i] = -1; newBuckets[i] = -1;
Entry* newEntries = new Entry[newSize];
//mEntries.CopyTo(newEntries, 0, 0, mCount);
for (int i = 0; i < mCount; i++) for (int i = 0; i < mCount; i++)
{ {
auto& newEntry = newEntries[i]; auto& newEntry = newEntries[i];
@ -217,8 +218,7 @@ private:
} }
} }
delete [] mBuckets; DeleteData();
delete [] mEntries;
mBuckets = newBuckets; mBuckets = newBuckets;
mEntries = newEntries; mEntries = newEntries;
@ -255,10 +255,9 @@ private:
void Initialize(intptr capacity) void Initialize(intptr capacity)
{ {
int_cosize size = GetPrimeish((int_cosize)capacity); int_cosize size = GetPrimeish((int_cosize)capacity);
mBuckets = new int_cosize[size]; AllocData(size, mEntries, mBuckets);
mAllocSize = size; mAllocSize = size;
for (int_cosize i = 0; i < (int_cosize)mAllocSize; i++) mBuckets[i] = -1; for (int_cosize i = 0; i < (int_cosize)mAllocSize; i++) mBuckets[i] = -1;
mEntries = new Entry[size];
mFreeList = -1; mFreeList = -1;
} }
@ -358,9 +357,8 @@ public:
mEntries = NULL; mEntries = NULL;
} }
else else
{ {
mBuckets = new int_cosize[mAllocSize]; AllocData(mAllocSize, mEntries, mBuckets);
mEntries = new Entry[mAllocSize];
for (int_cosize i = 0; i < mAllocSize; i++) for (int_cosize i = 0; i < mAllocSize; i++)
mBuckets[i] = val.mBuckets[i]; mBuckets[i] = val.mBuckets[i];
@ -414,8 +412,19 @@ public:
} }
} }
delete [] mBuckets; DeleteData();
delete [] mEntries; }
void AllocData(intptr size, Entry*& outEntries, int_cosize*& outBuckets)
{
uint8* data = new uint8[size * (sizeof(Entry) + sizeof(int_cosize))];
outEntries = (Entry*)data;
outBuckets = (int_cosize*)(data + size * sizeof(Entry));
}
void DeleteData()
{
delete mEntries;
} }
Dictionary& operator=(const Dictionary& rhs) Dictionary& operator=(const Dictionary& rhs)