Use Symbols to store JS object references when serialising and delete ObjectIDCache
When serialising JS objects, we keep track of any encountered object,
and serialize it only once. Any further serialisation instead stores an
ID referring to the original object (essentially an opaque pointer).
The trouble of course is to have a unique, persistent identifier for
such an object.
svn uses an ObjectIDCache, essentially a "JS Object -> ID" map (which
internally is essentially a "JS heap pointer -> ID" map).
JS, since ES15, includes a "Symbol" primitive type, which is a unique,
immutable identifier. They are also not iterable by for..in or
GetOwnPropertyName or related.
This means they can be used to store the tag directly on the object
(since it's impossible overwrite a user property).
Thanks to this, we can forgo ObjectIDCache in the serializers, and since
following D2897 it becomes unused, we can delete it, along with the
Finalization code it used.
Part of SM52 migration, stage: SM45-compatible changes.
Patch by: Itms
Tested By: Freagarach
Refs #4893
Differential Revision: https://code.wildfiregames.com/D3085
This was SVN commit r24167.
2020-11-12 07:40:19 +01:00
|
|
|
/* Copyright (C) 2020 Wildfire Games.
|
2014-11-13 12:19:28 +01:00
|
|
|
* This file is part of 0 A.D.
|
|
|
|
*
|
|
|
|
* 0 A.D. is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 0 A.D. is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
#include "ScriptContext.h"
|
2014-11-13 12:19:28 +01:00
|
|
|
|
|
|
|
#include "ps/GameSetup/Config.h"
|
|
|
|
#include "ps/Profile.h"
|
2016-08-06 17:41:59 +02:00
|
|
|
#include "scriptinterface/ScriptEngine.h"
|
2020-11-14 09:46:32 +01:00
|
|
|
#include "scriptinterface/ScriptInterface.h"
|
2014-11-13 12:19:28 +01:00
|
|
|
|
|
|
|
|
|
|
|
void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const JS::GCDescription& UNUSED(desc))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* During non-incremental GC, the GC is bracketed by JSGC_CYCLE_BEGIN/END
|
|
|
|
* callbacks. During an incremental GC, the sequence of callbacks is as
|
|
|
|
* follows:
|
|
|
|
* JSGC_CYCLE_BEGIN, JSGC_SLICE_END (first slice)
|
|
|
|
* JSGC_SLICE_BEGIN, JSGC_SLICE_END (second slice)
|
|
|
|
* ...
|
|
|
|
* JSGC_SLICE_BEGIN, JSGC_CYCLE_END (last slice)
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
if (progress == JS::GC_SLICE_BEGIN)
|
|
|
|
{
|
|
|
|
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
|
|
|
|
g_Profiler.Start("GCSlice");
|
|
|
|
g_Profiler2.RecordRegionEnter("GCSlice");
|
|
|
|
}
|
|
|
|
else if (progress == JS::GC_SLICE_END)
|
|
|
|
{
|
|
|
|
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
|
|
|
|
g_Profiler.Stop();
|
2016-06-22 15:38:05 +02:00
|
|
|
g_Profiler2.RecordRegionLeave();
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
else if (progress == JS::GC_CYCLE_BEGIN)
|
|
|
|
{
|
|
|
|
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
|
|
|
|
g_Profiler.Start("GCSlice");
|
|
|
|
g_Profiler2.RecordRegionEnter("GCSlice");
|
|
|
|
}
|
|
|
|
else if (progress == JS::GC_CYCLE_END)
|
|
|
|
{
|
|
|
|
if (CProfileManager::IsInitialised() && ThreadUtil::IsMainThread())
|
|
|
|
g_Profiler.Stop();
|
2016-06-22 15:38:05 +02:00
|
|
|
g_Profiler2.RecordRegionLeave();
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// The following code can be used to print some information aobut garbage collection
|
|
|
|
// Search for "Nonincremental reason" if there are problems running GC incrementally.
|
|
|
|
#if 0
|
|
|
|
if (progress == JS::GCProgress::GC_CYCLE_BEGIN)
|
|
|
|
printf("starting cycle ===========================================\n");
|
|
|
|
|
2015-12-19 02:29:55 +01:00
|
|
|
const char16_t* str = desc.formatMessage(rt);
|
2014-11-13 12:19:28 +01:00
|
|
|
int len = 0;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
for(int i = 0; i < 10000; i++)
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
if(!str[i])
|
|
|
|
break;
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
wchar_t outstring[len];
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
for(int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
outstring[i] = (wchar_t)str[i];
|
|
|
|
}
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
printf("---------------------------------------\n: %ls \n---------------------------------------\n", outstring);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
shared_ptr<ScriptContext> ScriptContext::CreateContext(int contextSize, int heapGrowthBytesGCTrigger)
|
2020-11-12 10:34:40 +01:00
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
return shared_ptr<ScriptContext>(new ScriptContext(contextSize, heapGrowthBytesGCTrigger));
|
2020-11-12 10:34:40 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
ScriptContext::ScriptContext(int contextSize, int heapGrowthBytesGCTrigger):
|
2014-11-13 12:19:28 +01:00
|
|
|
m_LastGCBytes(0),
|
|
|
|
m_LastGCCheck(0.0f),
|
|
|
|
m_HeapGrowthBytesGCTrigger(heapGrowthBytesGCTrigger),
|
2020-11-14 11:57:50 +01:00
|
|
|
m_ContextSize(contextSize)
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be initialized before constructing any ScriptContexts!");
|
2014-11-13 12:19:28 +01:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
m_rt = JS_NewRuntime(contextSize, JS::DefaultNurseryBytes, nullptr);
|
2014-11-13 12:19:28 +01:00
|
|
|
ENSURE(m_rt); // TODO: error handling
|
|
|
|
|
|
|
|
JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook);
|
2016-08-02 18:58:30 +02:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_ContextSize);
|
|
|
|
JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_ContextSize);
|
2014-11-13 12:19:28 +01:00
|
|
|
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL);
|
2016-08-02 18:58:30 +02:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
// The whole heap-growth mechanism seems to work only for non-incremental GCs.
|
|
|
|
// We disable it to make it more clear if full GCs happen triggered by this JSAPI internal mechanism.
|
|
|
|
JS_SetGCParameter(m_rt, JSGC_DYNAMIC_HEAP_GROWTH, false);
|
2016-08-06 17:41:59 +02:00
|
|
|
|
Improve JS Exception handling.
- Check for pending exceptions after function calls and script
executions.
- Call LOGERROR instead of JS_ReportError when there is a conversion
error in FromJSVal, since that can only be called from C++ (where JS
errors don't really make sense). Instead, C++ callers of FromJSVal
should handle the failure and, themselves, either report an error or
simply do something else.
- Wrap JS_ReportError since that makes updating it later easier.
This isn't a systematical fix since ToJSVal also ought return a boolean
for failures, and we probably should trigger errors instead of warnings
on 'implicit' conversions, rather a preparation diff.
Part of the SM52 migration, stage: SM45 compatible (actually SM52
incompatible, too).
Based on a patch by: Itms
Comments by: Vladislavbelov, Stan`
Refs #742, #4893
Differential Revision: https://code.wildfiregames.com/D3093
This was SVN commit r24187.
2020-11-15 19:29:17 +01:00
|
|
|
JS_SetErrorReporter(m_rt, ScriptException::ErrorReporter);
|
2020-11-14 09:46:32 +01:00
|
|
|
|
|
|
|
m_cx = JS_NewContext(m_rt, STACK_CHUNK_SIZE);
|
|
|
|
ENSURE(m_cx); // TODO: error handling
|
|
|
|
|
|
|
|
JS_SetOffthreadIonCompilationEnabled(m_rt, true);
|
|
|
|
|
|
|
|
// For GC debugging:
|
|
|
|
// JS_SetGCZeal(m_cx, 2, JS_DEFAULT_ZEAL_FREQ);
|
|
|
|
|
|
|
|
JS_SetContextPrivate(m_cx, nullptr);
|
|
|
|
|
|
|
|
JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_ION_ENABLE, 1);
|
|
|
|
JS_SetGlobalJitCompilerOption(m_rt, JSJITCOMPILER_BASELINE_ENABLE, 1);
|
|
|
|
|
|
|
|
JS::RuntimeOptionsRef(m_cx)
|
|
|
|
.setExtraWarnings(true)
|
|
|
|
.setWerror(false)
|
|
|
|
.setStrictMode(true);
|
2020-11-14 11:57:50 +01:00
|
|
|
|
|
|
|
ScriptEngine::GetSingleton().RegisterContext(m_cx);
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
ScriptContext::~ScriptContext()
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptContext!");
|
|
|
|
|
2020-11-14 09:46:32 +01:00
|
|
|
JS_DestroyContext(m_cx);
|
2014-11-13 12:19:28 +01:00
|
|
|
JS_DestroyRuntime(m_rt);
|
2016-08-06 17:41:59 +02:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
ScriptEngine::GetSingleton().UnRegisterContext(m_cx);
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void ScriptContext::RegisterCompartment(JSCompartment* cmpt)
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
2020-11-14 09:46:32 +01:00
|
|
|
ENSURE(cmpt);
|
|
|
|
m_Compartments.push_back(cmpt);
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void ScriptContext::UnRegisterCompartment(JSCompartment* cmpt)
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
2020-11-14 09:46:32 +01:00
|
|
|
m_Compartments.remove(cmpt);
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#define GC_DEBUG_PRINT 0
|
2020-11-14 11:57:50 +01:00
|
|
|
void ScriptContext::MaybeIncrementalGC(double delay)
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
|
|
|
PROFILE2("MaybeIncrementalGC");
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
if (JS::IsIncrementalGCEnabled(m_rt))
|
|
|
|
{
|
|
|
|
// The idea is to get the heap size after a completed GC and trigger the next GC when the heap size has
|
2016-11-23 14:02:58 +01:00
|
|
|
// reached m_LastGCBytes + X.
|
2014-11-13 12:19:28 +01:00
|
|
|
// In practice it doesn't quite work like that. When the incremental marking is completed, the sweeping kicks in.
|
|
|
|
// The sweeping actually frees memory and it does this in a background thread (if JS_USE_HELPER_THREADS is set).
|
|
|
|
// While the sweeping is happening we already run scripts again and produce new garbage.
|
|
|
|
|
|
|
|
const int GCSliceTimeBudget = 30; // Milliseconds an incremental slice is allowed to run
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Have a minimum time in seconds to wait between GC slices and before starting a new GC to distribute the GC
|
|
|
|
// load and to hopefully make it unnoticeable for the player. This value should be high enough to distribute
|
|
|
|
// the load well enough and low enough to make sure we don't run out of memory before we can start with the
|
2014-11-13 12:19:28 +01:00
|
|
|
// sweeping.
|
|
|
|
if (timer_Time() - m_LastGCCheck < delay)
|
|
|
|
return;
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
m_LastGCCheck = timer_Time();
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
int gcBytes = JS_GetGCParameter(m_rt, JSGC_BYTES);
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
#if GC_DEBUG_PRINT
|
|
|
|
std::cout << "gcBytes: " << gcBytes / 1024 << " KB" << std::endl;
|
|
|
|
#endif
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2014-11-13 12:19:28 +01:00
|
|
|
if (m_LastGCBytes > gcBytes || m_LastGCBytes == 0)
|
|
|
|
{
|
|
|
|
#if GC_DEBUG_PRINT
|
|
|
|
printf("Setting m_LastGCBytes: %d KB \n", gcBytes / 1024);
|
|
|
|
#endif
|
|
|
|
m_LastGCBytes = gcBytes;
|
|
|
|
}
|
|
|
|
|
2016-11-23 14:02:58 +01:00
|
|
|
// Run an additional incremental GC slice if the currently running incremental GC isn't over yet
|
2014-11-13 12:19:28 +01:00
|
|
|
// ... or
|
|
|
|
// start a new incremental GC if the JS heap size has grown enough for a GC to make sense
|
|
|
|
if (JS::IsIncrementalGCInProgress(m_rt) || (gcBytes - m_LastGCBytes > m_HeapGrowthBytesGCTrigger))
|
2016-11-23 15:09:58 +01:00
|
|
|
{
|
2014-11-13 12:19:28 +01:00
|
|
|
#if GC_DEBUG_PRINT
|
|
|
|
if (JS::IsIncrementalGCInProgress(m_rt))
|
|
|
|
printf("An incremental GC cycle is in progress. \n");
|
|
|
|
else
|
|
|
|
printf("GC needed because JSGC_BYTES - m_LastGCBytes > m_HeapGrowthBytesGCTrigger \n"
|
2016-11-23 14:02:58 +01:00
|
|
|
" JSGC_BYTES: %d KB \n m_LastGCBytes: %d KB \n m_HeapGrowthBytesGCTrigger: %d KB \n",
|
|
|
|
gcBytes / 1024,
|
|
|
|
m_LastGCBytes / 1024,
|
2014-11-13 12:19:28 +01:00
|
|
|
m_HeapGrowthBytesGCTrigger / 1024);
|
|
|
|
#endif
|
2016-11-23 15:09:58 +01:00
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
// A hack to make sure we never exceed the context size because we can't collect the memory
|
2014-11-13 12:19:28 +01:00
|
|
|
// fast enough.
|
2020-11-14 11:57:50 +01:00
|
|
|
if (gcBytes > m_ContextSize / 2)
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
|
|
|
if (JS::IsIncrementalGCInProgress(m_rt))
|
|
|
|
{
|
|
|
|
#if GC_DEBUG_PRINT
|
2020-11-14 11:57:50 +01:00
|
|
|
printf("Finishing incremental GC because gcBytes > m_ContextSize / 2. \n");
|
2014-11-13 12:19:28 +01:00
|
|
|
#endif
|
2020-11-14 09:46:32 +01:00
|
|
|
PrepareCompartmentsForIncrementalGC();
|
2014-11-13 12:19:28 +01:00
|
|
|
JS::FinishIncrementalGC(m_rt, JS::gcreason::REFRESH_FRAME);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-14 11:57:50 +01:00
|
|
|
if (gcBytes > m_ContextSize * 0.75)
|
2015-01-24 15:46:52 +01:00
|
|
|
{
|
|
|
|
ShrinkingGC();
|
2014-11-13 12:19:28 +01:00
|
|
|
#if GC_DEBUG_PRINT
|
2020-11-14 11:57:50 +01:00
|
|
|
printf("Running shrinking GC because gcBytes > m_ContextSize * 0.75. \n");
|
2014-11-13 12:19:28 +01:00
|
|
|
#endif
|
2015-01-24 15:46:52 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if GC_DEBUG_PRINT
|
2020-11-14 11:57:50 +01:00
|
|
|
printf("Running full GC because gcBytes > m_ContextSize / 2. \n");
|
2015-01-24 15:46:52 +01:00
|
|
|
#endif
|
|
|
|
JS_GC(m_rt);
|
|
|
|
}
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
#if GC_DEBUG_PRINT
|
|
|
|
if (!JS::IsIncrementalGCInProgress(m_rt))
|
|
|
|
printf("Starting incremental GC \n");
|
|
|
|
else
|
|
|
|
printf("Running incremental GC slice \n");
|
|
|
|
#endif
|
2020-11-14 09:46:32 +01:00
|
|
|
PrepareCompartmentsForIncrementalGC();
|
2016-09-02 18:46:39 +02:00
|
|
|
if (!JS::IsIncrementalGCInProgress(m_rt))
|
|
|
|
JS::StartIncrementalGC(m_rt, GC_NORMAL, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget);
|
|
|
|
else
|
|
|
|
JS::IncrementalGCSlice(m_rt, JS::gcreason::REFRESH_FRAME, GCSliceTimeBudget);
|
2014-11-13 12:19:28 +01:00
|
|
|
}
|
|
|
|
m_LastGCBytes = gcBytes;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void ScriptContext::ShrinkingGC()
|
2015-01-24 15:46:52 +01:00
|
|
|
{
|
|
|
|
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_COMPARTMENT);
|
|
|
|
JS::PrepareForFullGC(m_rt);
|
2016-09-02 18:46:39 +02:00
|
|
|
JS::GCForReason(m_rt, GC_SHRINK, JS::gcreason::REFRESH_FRAME);
|
2015-01-24 15:46:52 +01:00
|
|
|
JS_SetGCParameter(m_rt, JSGC_MODE, JSGC_MODE_INCREMENTAL);
|
|
|
|
}
|
|
|
|
|
2020-11-14 11:57:50 +01:00
|
|
|
void ScriptContext::PrepareCompartmentsForIncrementalGC() const
|
2014-11-13 12:19:28 +01:00
|
|
|
{
|
2020-11-14 09:46:32 +01:00
|
|
|
for (JSCompartment* const& cmpt : m_Compartments)
|
|
|
|
JS::PrepareZoneForGC(js::GetCompartmentZone(cmpt));
|
2015-01-24 15:46:52 +01:00
|
|
|
}
|