Optimize various initial pre-allocs to happen within memory tracing

This commit is contained in:
2026-03-23 18:25:05 +01:00
parent 90a0cc0e03
commit 3baec506e2
7 changed files with 71 additions and 13 deletions
+19 -5
View File
@@ -68,10 +68,9 @@ namespace
{ {
// Assets // Assets
CriticalSection AssetsLocker; CriticalSection AssetsLocker;
Dictionary<Guid, Asset*> Assets(2048); Dictionary<Guid, Asset*> Assets;
Array<Guid> LoadCallAssets(PLATFORM_THREADS_LIMIT);
CriticalSection LoadedAssetsToInvokeLocker; CriticalSection LoadedAssetsToInvokeLocker;
Array<Asset*> LoadedAssetsToInvoke(64); Array<Asset*> LoadedAssetsToInvoke;
Array<Asset*> ToUnload; Array<Asset*> ToUnload;
// Assets Registry Stuff // Assets Registry Stuff
@@ -85,6 +84,7 @@ namespace
ConcurrentTaskQueue<ContentLoadTask> LoadTasks; ConcurrentTaskQueue<ContentLoadTask> LoadTasks;
ConditionVariable LoadTasksSignal; ConditionVariable LoadTasksSignal;
CriticalSection LoadTasksMutex; CriticalSection LoadTasksMutex;
Array<Guid> LoadCallAssets;
#else #else
Array<ContentLoadTask*> LoadTasks; Array<ContentLoadTask*> LoadTasks;
#endif #endif
@@ -125,6 +125,13 @@ bool ContentService::Init()
{ {
PROFILE_MEM(Content); PROFILE_MEM(Content);
// Init memory containers
Assets.EnsureCapacity(2048);
LoadedAssetsToInvoke.EnsureCapacity(64);
#if PLATFORM_THREADS_LIMIT > 1
LoadCallAssets.EnsureCapacity(PLATFORM_THREADS_LIMIT);
#endif
// Load assets registry // Load assets registry
Cache.Init(); Cache.Init();
@@ -294,6 +301,7 @@ void ContentService::Dispose()
for (auto* e : LoadTasks) for (auto* e : LoadTasks)
e->Cancel(); e->Cancel();
LoadTasks.Clear(); LoadTasks.Clear();
LoadTasks.SetCapacity(0);
#endif #endif
} }
@@ -1381,6 +1389,7 @@ Asset* Content::LoadAsync(const Guid& id, const ScriptingTypeHandle& type)
return result; return result;
} }
#if PLATFORM_THREADS_LIMIT > 1
// Check if that asset is during loading // Check if that asset is during loading
if (LoadCallAssets.Contains(id)) if (LoadCallAssets.Contains(id))
{ {
@@ -1401,9 +1410,12 @@ Asset* Content::LoadAsync(const Guid& id, const ScriptingTypeHandle& type)
// Mark asset as loading and release lock so other threads can load other assets // Mark asset as loading and release lock so other threads can load other assets
LoadCallAssets.Add(id); LoadCallAssets.Add(id);
AssetsLocker.Unlock();
#define LOAD_FAILED() AssetsLocker.Lock(); LoadCallAssets.Remove(id); AssetsLocker.Unlock(); return nullptr #define LOAD_FAILED() AssetsLocker.Lock(); LoadCallAssets.Remove(id); AssetsLocker.Unlock(); return nullptr
#else
#define LOAD_FAILED() return nullptr
#endif
AssetsLocker.Unlock();
// Get cached asset info (from registry) // Get cached asset info (from registry)
AssetInfo assetInfo; AssetInfo assetInfo;
@@ -1461,7 +1473,9 @@ Asset* Content::LoadAsync(const Guid& id, const ScriptingTypeHandle& type)
result->startLoading(); result->startLoading();
// Remove from the loading queue and release lock // Remove from the loading queue and release lock
#if PLATFORM_THREADS_LIMIT > 1
LoadCallAssets.Remove(id); LoadCallAssets.Remove(id);
#endif
AssetsLocker.Unlock(); AssetsLocker.Unlock();
#undef LOAD_FAILED #undef LOAD_FAILED
+5 -3
View File
@@ -164,9 +164,6 @@ public:
NavigationService() NavigationService()
: EngineService(TEXT("Navigation"), 60) : EngineService(TEXT("Navigation"), 60)
{ {
#if COMPILE_WITH_NAV_MESH_BUILDER
NavMeshBuilder::Init();
#endif
} }
bool Init() override; bool Init() override;
@@ -305,6 +302,11 @@ void NavigationSettings::Deserialize(DeserializeStream& stream, ISerializeModifi
bool NavigationService::Init() bool NavigationService::Init()
{ {
PROFILE_MEM(Navigation);
#if COMPILE_WITH_NAV_MESH_BUILDER
NavMeshBuilder::Init();
#endif
// Link memory allocation calls to use engine default allocator // Link memory allocation calls to use engine default allocator
dtAllocSetCustom(dtAllocDefault, Allocator::Free); dtAllocSetCustom(dtAllocDefault, Allocator::Free);
rcAllocSetCustom(rcAllocDefault, Allocator::Free); rcAllocSetCustom(rcAllocDefault, Allocator::Free);
@@ -24,6 +24,7 @@
#include <unistd.h> #include <unistd.h>
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#include <emscripten/threading.h> #include <emscripten/threading.h>
#include <emscripten/emmalloc.h>
#include <emscripten/version.h> #include <emscripten/version.h>
#include <emscripten/heap.h> #include <emscripten/heap.h>
@@ -78,6 +79,38 @@ void WebFileSystem::GetSpecialFolderPath(const SpecialFolder type, String& resul
result = TEXT("/"); result = TEXT("/");
} }
#if 0
void* WebPlatform::Allocate(uint64 size, uint64 alignment)
{
void* ptr = nullptr;
if (alignment && size)
{
// Alignment always has to be power of two
ASSERT_LOW_LAYER((alignment & (alignment - 1)) == 0);
ptr = emscripten_builtin_memalign(alignment, size);
if (!ptr)
OutOfMemory();
#if COMPILE_WITH_PROFILER
OnMemoryAlloc(ptr, size);
#endif
}
return ptr;
}
void WebPlatform::Free(void* ptr)
{
if (ptr)
{
#if COMPILE_WITH_PROFILER
OnMemoryFree(ptr);
#endif
emscripten_builtin_free(ptr);
}
}
#endif
String WebPlatform::GetSystemName() String WebPlatform::GetSystemName()
{ {
return TEXT("Browser"); return TEXT("Browser");
+4
View File
@@ -92,6 +92,10 @@ public:
*dst = value; *dst = value;
#endif #endif
} }
#if 0
static void* Allocate(uint64 size, uint64 alignment);
static void Free(void* ptr);
#endif
FORCE_INLINE static uint64 GetCurrentThreadID() FORCE_INLINE static uint64 GetCurrentThreadID()
{ {
#ifdef __EMSCRIPTEN_PTHREADS__ #ifdef __EMSCRIPTEN_PTHREADS__
+4 -2
View File
@@ -227,8 +227,8 @@ namespace
CachedPSO PsoDepth; CachedPSO PsoDepth;
CachedPSO PsoNoDepth; CachedPSO PsoNoDepth;
CachedPSO* CurrentPso = nullptr; CachedPSO* CurrentPso = nullptr;
DynamicVertexBuffer VB(RENDER2D_INITIAL_VB_CAPACITY, (uint32)sizeof(Render2DVertex), TEXT("Render2D.VB")); DynamicVertexBuffer VB(0, (uint32)sizeof(Render2DVertex), TEXT("Render2D.VB"));
DynamicIndexBuffer IB(RENDER2D_INITIAL_IB_CAPACITY, sizeof(uint32), TEXT("Render2D.IB")); DynamicIndexBuffer IB(0, sizeof(uint32), TEXT("Render2D.IB"));
uint32 VBIndex = 0; uint32 VBIndex = 0;
uint32 IBIndex = 0; uint32 IBIndex = 0;
} }
@@ -633,6 +633,8 @@ bool Render2DService::Init()
})); }));
DrawCalls.EnsureCapacity(RENDER2D_INITIAL_DRAW_CALL_CAPACITY); DrawCalls.EnsureCapacity(RENDER2D_INITIAL_DRAW_CALL_CAPACITY);
VB.Data.EnsureCapacity(RENDER2D_INITIAL_VB_CAPACITY);
IB.Data.EnsureCapacity(RENDER2D_INITIAL_IB_CAPACITY);
return false; return false;
} }
+2 -1
View File
@@ -50,7 +50,7 @@ bool IsBakingLightmaps = false;
bool EnableLightmapsUsage = true; bool EnableLightmapsUsage = true;
#endif #endif
Array<RendererPassBase*> PassList(64); Array<RendererPassBase*> PassList;
class RendererService : public EngineService class RendererService : public EngineService
{ {
@@ -73,6 +73,7 @@ bool RendererService::Init()
PROFILE_MEM(Graphics); PROFILE_MEM(Graphics);
// Register passes // Register passes
PassList.EnsureCapacity(64);
PassList.Add(GBufferPass::Instance()); PassList.Add(GBufferPass::Instance());
PassList.Add(ShadowsPass::Instance()); PassList.Add(ShadowsPass::Instance());
PassList.Add(LightPass::Instance()); PassList.Add(LightPass::Instance());
+4 -2
View File
@@ -96,9 +96,9 @@ namespace
} }
}; };
Dictionary<Guid, ScriptingObjectData> _objectsDictionary(1024 * 16); Dictionary<Guid, ScriptingObjectData> _objectsDictionary;
#else #else
Dictionary<Guid, ScriptingObject*> _objectsDictionary(1024 * 16); Dictionary<Guid, ScriptingObject*> _objectsDictionary;
#endif #endif
bool _isEngineAssemblyLoaded = false; bool _isEngineAssemblyLoaded = false;
bool _hasGameModulesLoaded = false; bool _hasGameModulesLoaded = false;
@@ -179,6 +179,8 @@ bool ScriptingService::Init()
PROFILE_MEM(Scripting); PROFILE_MEM(Scripting);
Stopwatch stopwatch; Stopwatch stopwatch;
_objectsDictionary.EnsureCapacity(16 * 1024);
// Initialize managed runtime // Initialize managed runtime
if (MCore::LoadEngine()) if (MCore::LoadEngine())
{ {