Skip to content

Commit

Permalink
Merge Increase heap size per isolate to match system memory (pr-2377)
Browse files Browse the repository at this point in the history
- f51ba83 tweak(citizen-scripting-v8): increase heap size per isolate to match system memory
  • Loading branch information
prikolium-cfx committed Jun 13, 2024
2 parents b14168f + f51ba83 commit 58fcdeb
Showing 1 changed file with 32 additions and 1 deletion.
33 changes: 32 additions & 1 deletion code/components/citizen-scripting-v8/src/V8ScriptRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,30 @@ inline static std::chrono::milliseconds msec()
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::high_resolution_clock::now().time_since_epoch());
}

// This copies behavior from mono
#ifdef WIN32
#include <windows.h>
// Gets scaled (max * 0.9) max memory in bytes.
size_t GetScaledPhysicalMemorySize()
{
MEMORYSTATUSEX status;
status.dwLength = sizeof(status);
GlobalMemoryStatusEx(&status);
return status.ullTotalPhys * 0.9;
}
#else
#include <unistd.h>
// Gets scaled (max * 0.9) max memory in bytes.
size_t GetScaledPhysicalMemorySize()
{
long pages = sysconf(_SC_PHYS_PAGES);
long pageSize = sysconf(_SC_PAGE_SIZE);

return (pages * pageSize) * 0.9;
}
#endif


inline bool UseNode()
{
#ifndef IS_FXSERVER
Expand Down Expand Up @@ -2889,7 +2913,6 @@ void V8ScriptGlobals::Initialize()

V8::SetFlagsFromCommandLine(&argc, (char**)argv, false);
#endif

const char* flags = "--turbo-inline-js-wasm-calls --expose_gc --harmony-top-level-await";
V8::SetFlagsFromString(flags, strlen(flags));

Expand Down Expand Up @@ -2920,6 +2943,14 @@ void V8ScriptGlobals::Initialize()
Isolate::CreateParams params;
params.array_buffer_allocator = m_arrayBufferAllocator.get();

const auto kScaledMemory = GetScaledPhysicalMemorySize();

auto constraints = ResourceConstraints{};

constraints.ConfigureDefaultsFromHeapSize(0, kScaledMemory);

params.constraints = constraints;

m_isolate = Isolate::Allocate();

m_isolate->AddGCPrologueCallback([](Isolate* isolate, GCType type,
Expand Down

0 comments on commit 58fcdeb

Please sign in to comment.