From ef9ea478ece947e5791c4cdd985b2c34d78a4a93 Mon Sep 17 00:00:00 2001 From: Dunedan Date: Sat, 5 Apr 2025 09:33:47 +0200 Subject: [PATCH] Add workaround to turn off nursery size heuristic SpiderMonkey 98 introduced a size heuristic for the nursery GC region (https://phabricator.services.mozilla.com/D136637). As this heuristic uses a wall-clock time duration, it results in a severe performance regression on slower systems for our use case. This commit adds a workaround to turn off that heuristic, by telling SpiderMonkey that a "page load" (something which doesn't have a meaning in the context of pyrogenesis) is in progress, as that heuristic is disabled for page loads. Co-Authored by: Ralph Sennhauser Fixes #7714 (cherry picked from commit 11dd480b67fc787fe4a874d2ec423d1492aa1d57) Signed-off-by: Itms --- source/scriptinterface/ScriptContext.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/source/scriptinterface/ScriptContext.cpp b/source/scriptinterface/ScriptContext.cpp index c0c814e03d..5f285781a4 100644 --- a/source/scriptinterface/ScriptContext.cpp +++ b/source/scriptinterface/ScriptContext.cpp @@ -27,6 +27,8 @@ #include "scriptinterface/ScriptEngine.h" #include "scriptinterface/ScriptInterface.h" +#include "js/friend/PerformanceHint.h" + void GCSliceCallbackHook(JSContext* UNUSED(cx), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc)) { /** @@ -129,6 +131,10 @@ ScriptContext::ScriptContext(int contextSize, int heapGrowthBytesGCTrigger): JS::ContextOptionsRef(m_cx).setStrictMode(true); + // Workaround to turn off nursery size heuristic. + // See https://gitea.wildfiregames.com/0ad/0ad/issues/7714 for details. + js::gc::SetPerformanceHint(m_cx, js::gc::PerformanceHint::InPageLoad); + ScriptEngine::GetSingleton().RegisterContext(m_cx); JS::SetJobQueue(m_cx, m_JobQueue.get()); @@ -139,6 +145,9 @@ ScriptContext::~ScriptContext() { ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptContext!"); + // Switch back to normal performance mode to avoid assertion in debug mode. + js::gc::SetPerformanceHint(m_cx, js::gc::PerformanceHint::Normal); + JS_DestroyContext(m_cx); ScriptEngine::GetSingleton().UnRegisterContext(m_cx); }