mirror of
https://github.com/beefytech/Beef.git
synced 2025-07-04 15:26:00 +02:00
Optimizations, switching CanImplicitlyCast method, new CPU rate checker
This commit is contained in:
parent
39fd8d2624
commit
098ad1ce55
25 changed files with 759 additions and 301 deletions
|
@ -909,8 +909,25 @@ static void __cdecl AbortHandler(int)
|
|||
BfpSystem_FatalError("Abort handler", NULL);
|
||||
}
|
||||
|
||||
static int64 gCPUFreq = -1;
|
||||
static int64 gStartCPUTick = -1;
|
||||
static int64 gStartQPF = -1;
|
||||
|
||||
static void InitCPUFreq()
|
||||
{
|
||||
if (gStartCPUTick == -1)
|
||||
{
|
||||
gStartCPUTick = __rdtsc();
|
||||
LARGE_INTEGER largeVal = { 0 };
|
||||
QueryPerformanceCounter(&largeVal);
|
||||
gStartQPF = largeVal.QuadPart;
|
||||
}
|
||||
}
|
||||
|
||||
BFP_EXPORT void BFP_CALLTYPE BfpSystem_Init(int version, BfpSystemInitFlags flags)
|
||||
{
|
||||
InitCPUFreq();
|
||||
|
||||
::_set_error_mode(_OUT_TO_STDERR);
|
||||
|
||||
#ifdef _DEBUG
|
||||
|
@ -1123,10 +1140,28 @@ BFP_EXPORT int64 BFP_CALLTYPE BfpSystem_GetCPUTick()
|
|||
}
|
||||
|
||||
BFP_EXPORT int64 BFP_CALLTYPE BfpSystem_GetCPUTickFreq()
|
||||
{
|
||||
LARGE_INTEGER freq = { 0 };
|
||||
QueryPerformanceFrequency(&freq);
|
||||
return freq.QuadPart;
|
||||
{
|
||||
LARGE_INTEGER largeVal = { 0 };
|
||||
QueryPerformanceFrequency(&largeVal);
|
||||
int64 qpfFreq = largeVal.QuadPart;
|
||||
|
||||
if (gStartCPUTick == -1)
|
||||
{
|
||||
InitCPUFreq();
|
||||
Sleep(10);
|
||||
}
|
||||
|
||||
int64 cpuTick1 = __rdtsc();
|
||||
QueryPerformanceCounter(&largeVal);
|
||||
int64 slowTick1 = largeVal.QuadPart;
|
||||
|
||||
int64 cpuElapsed = cpuTick1 - gStartCPUTick;
|
||||
int64 slowElapsed = slowTick1 - gStartQPF;
|
||||
double elapsedSeconds = slowElapsed / (double)qpfFreq;
|
||||
int64 freq = (int64)(cpuElapsed / elapsedSeconds);
|
||||
gCPUFreq = freq;
|
||||
|
||||
return gCPUFreq;
|
||||
}
|
||||
|
||||
BFP_EXPORT void BFP_CALLTYPE BfpSystem_CreateGUID(BfpGUID* outGuid)
|
||||
|
|
|
@ -639,7 +639,7 @@ public:
|
|||
this->mSize++;
|
||||
}
|
||||
|
||||
void Insert(intptr idx, T* vals, intptr size)
|
||||
void Insert(intptr idx, const T* vals, intptr size)
|
||||
{
|
||||
BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
|
||||
if (this->mSize + size > this->mAllocSize)
|
||||
|
@ -934,7 +934,7 @@ public:
|
|||
this->mSize++;
|
||||
}
|
||||
|
||||
void Insert(intptr idx, T* vals, intptr size)
|
||||
void Insert(intptr idx, const T* vals, intptr size)
|
||||
{
|
||||
BF_ASSERT((uintptr)idx <= (uintptr)this->mSize);
|
||||
if (this->mSize + size > this->mAllocSize)
|
||||
|
|
|
@ -355,7 +355,7 @@ static int64 GetTimestamp()
|
|||
#ifdef BF_PLATFORM_WINDOWS
|
||||
return __rdtsc() / 100;
|
||||
#else
|
||||
return BfpSystem_GetCPUTick();
|
||||
return BfpSystem_GetCPUTick() / 100;
|
||||
#endif
|
||||
}
|
||||
|
||||
|
|
|
@ -186,7 +186,7 @@ public:
|
|||
return mPrevSizes + (int)((uint8*)ptr - mCurAlloc);
|
||||
}
|
||||
|
||||
uint8* AllocBytes(int wantSize, int alignSize, const char* dbgName = "AllocBytes")
|
||||
uint8* AllocBytes(intptr wantSize, int alignSize, const char* dbgName = "AllocBytes")
|
||||
{
|
||||
mCurPtr = (uint8*)(((intptr)mCurPtr + alignSize - 1) & ~(alignSize - 1));
|
||||
|
||||
|
@ -194,7 +194,7 @@ public:
|
|||
return retVal;
|
||||
}
|
||||
|
||||
uint8* AllocBytes(int wantSize, const char* dbgName = "AllocBytes")
|
||||
uint8* AllocBytes(intptr wantSize, const char* dbgName = "AllocBytes")
|
||||
{
|
||||
#ifdef BUMPALLOC_TRACKALLOCS
|
||||
BumpAllocTrackedEntry* allocSizePtr;
|
||||
|
@ -237,6 +237,11 @@ class AllocatorBump
|
|||
public:
|
||||
BumpAllocator* mAlloc;
|
||||
|
||||
AllocatorBump()
|
||||
{
|
||||
mAlloc = NULL;
|
||||
}
|
||||
|
||||
T* allocate(intptr count)
|
||||
{
|
||||
return (T*)mAlloc->AllocBytes((int)(sizeof(T) * count), alignof(T));
|
||||
|
@ -245,6 +250,16 @@ public:
|
|||
void deallocate(T* ptr)
|
||||
{
|
||||
}
|
||||
|
||||
void* rawAllocate(intptr size)
|
||||
{
|
||||
return mAlloc->AllocBytes(size, 16);
|
||||
}
|
||||
|
||||
void rawDeallocate(void* ptr)
|
||||
{
|
||||
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -107,6 +107,42 @@ void ChunkedDataBuffer::Write(uint8 byte)
|
|||
mSize++;
|
||||
}
|
||||
|
||||
void ChunkedDataBuffer::Write_2(uint16 val)
|
||||
{
|
||||
while (mWriteCurPtr + 2 > mWriteCurAlloc + ALLOC_SIZE)
|
||||
{
|
||||
Write((uint8*)&val, 2);
|
||||
return;
|
||||
}
|
||||
*(uint16*)mWriteCurPtr = val;
|
||||
mWriteCurPtr += 2;
|
||||
mSize += 2;
|
||||
}
|
||||
|
||||
void ChunkedDataBuffer::Write_3(uint32 val)
|
||||
{
|
||||
while (mWriteCurPtr + 3 > mWriteCurAlloc + ALLOC_SIZE)
|
||||
{
|
||||
Write((uint8*)&val, 3);
|
||||
return;
|
||||
}
|
||||
*(uint32*)mWriteCurPtr = val;
|
||||
mWriteCurPtr += 3;
|
||||
mSize += 3;
|
||||
}
|
||||
|
||||
void ChunkedDataBuffer::Write_4(uint32 val)
|
||||
{
|
||||
while (mWriteCurPtr + 4 > mWriteCurAlloc + ALLOC_SIZE)
|
||||
{
|
||||
Write((uint8*)&val, 4);
|
||||
return;
|
||||
}
|
||||
*(uint32*)mWriteCurPtr = val;
|
||||
mWriteCurPtr += 4;
|
||||
mSize += 4;
|
||||
}
|
||||
|
||||
int ChunkedDataBuffer::GetReadPos()
|
||||
{
|
||||
return mReadPoolIdx * ALLOC_SIZE + (int)(mReadCurPtr - mReadCurAlloc);
|
||||
|
|
|
@ -32,6 +32,9 @@ public:
|
|||
void GrowPool();
|
||||
void Write(const void* data, int size);
|
||||
void Write(uint8 byte);
|
||||
void Write_2(uint16 val);
|
||||
void Write_3(uint32 val);
|
||||
void Write_4(uint32 val);
|
||||
int GetReadPos();
|
||||
void SetReadPos(int pos);
|
||||
void NextReadPool();
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include "../Common.h"
|
||||
#include "Array.h"
|
||||
#include <unordered_map>
|
||||
|
||||
NS_BF_BEGIN;
|
||||
|
||||
template <typename TKey>
|
||||
class HashSet
|
||||
template <typename TKey, typename TAlloc = AllocatorCLib<TKey> >
|
||||
class HashSet : public TAlloc
|
||||
{
|
||||
public:
|
||||
typedef int int_cosize;
|
||||
|
@ -254,14 +254,14 @@ private:
|
|||
Resize(ExpandSize(mCount), false);
|
||||
}
|
||||
|
||||
void Resize(int newSize, bool forceNewHashCodes)
|
||||
void Resize(intptr newSize, bool forceNewHashCodes)
|
||||
{
|
||||
BF_ASSERT(newSize >= mAllocSize);
|
||||
int_cosize* newBuckets = new int_cosize[newSize];
|
||||
int_cosize* newBuckets = (int_cosize*)rawAllocate(sizeof(int_cosize) * newSize);
|
||||
for (int_cosize i = 0; i < newSize; i++)
|
||||
newBuckets[i] = -1;
|
||||
Entry* newEntries = new Entry[newSize];
|
||||
//mEntries.CopyTo(newEntries, 0, 0, mCount);
|
||||
Entry* newEntries = (Entry*)rawAllocate(sizeof(Entry)*newSize);
|
||||
|
||||
for (int i = 0; i < mCount; i++)
|
||||
{
|
||||
auto& newEntry = newEntries[i];
|
||||
|
@ -294,12 +294,12 @@ private:
|
|||
}
|
||||
}
|
||||
|
||||
delete[] mBuckets;
|
||||
delete[] mEntries;
|
||||
rawDeallocate(mBuckets);
|
||||
rawDeallocate(mEntries);
|
||||
|
||||
mBuckets = newBuckets;
|
||||
mEntries = newEntries;
|
||||
mAllocSize = newSize;
|
||||
mAllocSize = (int_cosize)newSize;
|
||||
}
|
||||
|
||||
int FindEntry(const TKey& key)
|
||||
|
@ -332,10 +332,10 @@ private:
|
|||
void Initialize(intptr capacity)
|
||||
{
|
||||
int_cosize size = GetPrimeish((int_cosize)capacity);
|
||||
mBuckets = new int_cosize[size];
|
||||
mBuckets = (int_cosize*)rawAllocate(sizeof(int_cosize) * size);
|
||||
mAllocSize = size;
|
||||
for (int_cosize i = 0; i < (int_cosize)mAllocSize; i++) mBuckets[i] = -1;
|
||||
mEntries = new Entry[size];
|
||||
mEntries = (Entry*)rawAllocate(sizeof(Entry) * size);
|
||||
mFreeList = -1;
|
||||
}
|
||||
|
||||
|
@ -431,8 +431,8 @@ public:
|
|||
}
|
||||
else
|
||||
{
|
||||
mBuckets = new int_cosize[mAllocSize];
|
||||
mEntries = new Entry[mAllocSize];
|
||||
mBuckets = (int_cosize*)rawAllocate(sizeof(int_cosize) * mAllocSize);
|
||||
mEntries = (Entry*)rawAllocate(sizeof(Entry) * mAllocSize);
|
||||
|
||||
for (int_cosize i = 0; i < mAllocSize; i++)
|
||||
mBuckets[i] = val.mBuckets[i];
|
||||
|
@ -476,8 +476,8 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
delete[] mBuckets;
|
||||
delete[] mEntries;
|
||||
rawDeallocate(mBuckets);
|
||||
rawDeallocate(mEntries);
|
||||
}
|
||||
|
||||
HashSet& operator=(const HashSet& rhs)
|
||||
|
|
|
@ -217,7 +217,7 @@ public:
|
|||
BF_ASSERT((uintptr)idx < (uintptr)this->mSize);
|
||||
return this->mVals[idx];
|
||||
}
|
||||
|
||||
|
||||
bool operator==(const SizedArrayBase& arrB) const
|
||||
{
|
||||
if (this->mSize != arrB.mSize)
|
||||
|
@ -288,6 +288,11 @@ public:
|
|||
this->mSize = 0;
|
||||
}
|
||||
|
||||
void Clear()
|
||||
{
|
||||
this->mSize = 0;
|
||||
}
|
||||
|
||||
/*void Free()
|
||||
{
|
||||
if (this->mVals != NULL)
|
||||
|
@ -1060,17 +1065,27 @@ public:
|
|||
}
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
static bool operator==(const ArrayBase<T>& arrA, const SizedArrayBase<T>& arrB)
|
||||
{
|
||||
if (arrA.mSize != arrB.mSize)
|
||||
return false;
|
||||
for (intptr i = 0; i < arrA.mSize; i++)
|
||||
if (arrA.mVals[i] != arrB.mVals[i])
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
NS_BF_END;
|
||||
|
||||
/*namespace std
|
||||
namespace std
|
||||
{
|
||||
template<typename T>
|
||||
struct hash<Beefy::Array<T> >
|
||||
struct hash<Beefy::SizedArrayImpl<T> >
|
||||
{
|
||||
size_t operator()(const Beefy::Array<T>& val) const
|
||||
size_t operator()(const Beefy::SizedArrayImpl<T>& val) const
|
||||
{
|
||||
return _Hash_seq((const uint8*)val.mVals, sizeof(T) * val.mSize);
|
||||
return HashBytes((const uint8*)val.mVals, sizeof(T) * val.mSize);
|
||||
}
|
||||
};
|
||||
}*/
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue