Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tweak(citizen-scripting-v8): increase heap size per isolate to match physical memory size #2377

Merged
merged 1 commit into from
Jun 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading