1
0
forked from mirrors/0ad

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.
This commit is contained in:
wraitii
2020-11-12 06:40:19 +00:00
parent d5bbf900bc
commit dad2857538
12 changed files with 52 additions and 278 deletions
+1 -20
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2016 Wildfire Games.
/* Copyright (C) 2020 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -89,22 +89,6 @@ void GCSliceCallbackHook(JSRuntime* UNUSED(rt), JS::GCProgress progress, const J
#endif
}
void ScriptRuntime::GCCallback(JSRuntime* UNUSED(rt), JSGCStatus status, void *data)
{
if (status == JSGC_END)
reinterpret_cast<ScriptRuntime*>(data)->GCCallbackMember();
}
void ScriptRuntime::GCCallbackMember()
{
m_FinalizationListObjectIdCache.clear();
}
void ScriptRuntime::AddDeferredFinalizationObject(const std::shared_ptr<void>& obj)
{
m_FinalizationListObjectIdCache.push_back(obj);
}
ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtimeSize, int heapGrowthBytesGCTrigger):
m_LastGCBytes(0),
m_LastGCCheck(0.0f),
@@ -118,7 +102,6 @@ ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtim
ENSURE(m_rt); // TODO: error handling
JS::SetGCSliceCallback(m_rt, GCSliceCallbackHook);
JS_SetGCCallback(m_rt, ScriptRuntime::GCCallback, this);
JS_SetGCParameter(m_rt, JSGC_MAX_MALLOC_BYTES, m_RuntimeSize);
JS_SetGCParameter(m_rt, JSGC_MAX_BYTES, m_RuntimeSize);
@@ -133,9 +116,7 @@ ScriptRuntime::ScriptRuntime(shared_ptr<ScriptRuntime> parentRuntime, int runtim
ScriptRuntime::~ScriptRuntime()
{
JS_SetGCCallback(m_rt, nullptr, nullptr);
JS_DestroyRuntime(m_rt);
ENSURE(m_FinalizationListObjectIdCache.empty() && "Leak: Removing callback while some objects still aren't finalized!");
ENSURE(ScriptEngine::IsInitialised() && "The ScriptEngine must be active (initialized and not yet shut down) when destroying a ScriptRuntime!");
ScriptEngine::GetSingleton().UnRegisterRuntime(m_rt);