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
CriticalSection AssetsLocker;
Dictionary<Guid, Asset*> Assets(2048);
Array<Guid> LoadCallAssets(PLATFORM_THREADS_LIMIT);
Dictionary<Guid, Asset*> Assets;
CriticalSection LoadedAssetsToInvokeLocker;
Array<Asset*> LoadedAssetsToInvoke(64);
Array<Asset*> LoadedAssetsToInvoke;
Array<Asset*> ToUnload;
// Assets Registry Stuff
@@ -85,6 +84,7 @@ namespace
ConcurrentTaskQueue<ContentLoadTask> LoadTasks;
ConditionVariable LoadTasksSignal;
CriticalSection LoadTasksMutex;
Array<Guid> LoadCallAssets;
#else
Array<ContentLoadTask*> LoadTasks;
#endif
@@ -125,6 +125,13 @@ bool ContentService::Init()
{
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
Cache.Init();
@@ -294,6 +301,7 @@ void ContentService::Dispose()
for (auto* e : LoadTasks)
e->Cancel();
LoadTasks.Clear();
LoadTasks.SetCapacity(0);
#endif
}
@@ -1381,6 +1389,7 @@ Asset* Content::LoadAsync(const Guid& id, const ScriptingTypeHandle& type)
return result;
}
#if PLATFORM_THREADS_LIMIT > 1
// Check if that asset is during loading
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
LoadCallAssets.Add(id);
AssetsLocker.Unlock();
#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)
AssetInfo assetInfo;
@@ -1461,7 +1473,9 @@ Asset* Content::LoadAsync(const Guid& id, const ScriptingTypeHandle& type)
result->startLoading();
// Remove from the loading queue and release lock
#if PLATFORM_THREADS_LIMIT > 1
LoadCallAssets.Remove(id);
#endif
AssetsLocker.Unlock();
#undef LOAD_FAILED
+5 -3
View File
@@ -164,9 +164,6 @@ public:
NavigationService()
: EngineService(TEXT("Navigation"), 60)
{
#if COMPILE_WITH_NAV_MESH_BUILDER
NavMeshBuilder::Init();
#endif
}
bool Init() override;
@@ -305,6 +302,11 @@ void NavigationSettings::Deserialize(DeserializeStream& stream, ISerializeModifi
bool NavigationService::Init()
{
PROFILE_MEM(Navigation);
#if COMPILE_WITH_NAV_MESH_BUILDER
NavMeshBuilder::Init();
#endif
// Link memory allocation calls to use engine default allocator
dtAllocSetCustom(dtAllocDefault, Allocator::Free);
rcAllocSetCustom(rcAllocDefault, Allocator::Free);
@@ -24,6 +24,7 @@
#include <unistd.h>
#include <emscripten/emscripten.h>
#include <emscripten/threading.h>
#include <emscripten/emmalloc.h>
#include <emscripten/version.h>
#include <emscripten/heap.h>
@@ -78,6 +79,38 @@ void WebFileSystem::GetSpecialFolderPath(const SpecialFolder type, String& resul
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()
{
return TEXT("Browser");
+4
View File
@@ -92,6 +92,10 @@ public:
*dst = value;
#endif
}
#if 0
static void* Allocate(uint64 size, uint64 alignment);
static void Free(void* ptr);
#endif
FORCE_INLINE static uint64 GetCurrentThreadID()
{
#ifdef __EMSCRIPTEN_PTHREADS__
+4 -2
View File
@@ -227,8 +227,8 @@ namespace
CachedPSO PsoDepth;
CachedPSO PsoNoDepth;
CachedPSO* CurrentPso = nullptr;
DynamicVertexBuffer VB(RENDER2D_INITIAL_VB_CAPACITY, (uint32)sizeof(Render2DVertex), TEXT("Render2D.VB"));
DynamicIndexBuffer IB(RENDER2D_INITIAL_IB_CAPACITY, sizeof(uint32), TEXT("Render2D.IB"));
DynamicVertexBuffer VB(0, (uint32)sizeof(Render2DVertex), TEXT("Render2D.VB"));
DynamicIndexBuffer IB(0, sizeof(uint32), TEXT("Render2D.IB"));
uint32 VBIndex = 0;
uint32 IBIndex = 0;
}
@@ -633,6 +633,8 @@ bool Render2DService::Init()
}));
DrawCalls.EnsureCapacity(RENDER2D_INITIAL_DRAW_CALL_CAPACITY);
VB.Data.EnsureCapacity(RENDER2D_INITIAL_VB_CAPACITY);
IB.Data.EnsureCapacity(RENDER2D_INITIAL_IB_CAPACITY);
return false;
}
+2 -1
View File
@@ -50,7 +50,7 @@ bool IsBakingLightmaps = false;
bool EnableLightmapsUsage = true;
#endif
Array<RendererPassBase*> PassList(64);
Array<RendererPassBase*> PassList;
class RendererService : public EngineService
{
@@ -73,6 +73,7 @@ bool RendererService::Init()
PROFILE_MEM(Graphics);
// Register passes
PassList.EnsureCapacity(64);
PassList.Add(GBufferPass::Instance());
PassList.Add(ShadowsPass::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
Dictionary<Guid, ScriptingObject*> _objectsDictionary(1024 * 16);
Dictionary<Guid, ScriptingObject*> _objectsDictionary;
#endif
bool _isEngineAssemblyLoaded = false;
bool _hasGameModulesLoaded = false;
@@ -179,6 +179,8 @@ bool ScriptingService::Init()
PROFILE_MEM(Scripting);
Stopwatch stopwatch;
_objectsDictionary.EnsureCapacity(16 * 1024);
// Initialize managed runtime
if (MCore::LoadEngine())
{