1
0
forked from mirrors/0ad

Only freeze once the cached JS values of templates

CParamNodes can be quite large, thus we usually cache the JS::Value when
converting them. The AIInterface makes heavy use of it as detected in #7404.

However, the cached values are re-frozen everytime, which is a
significant waste of time on a large number of templates.
This commit is contained in:
Itms
2025-01-07 17:14:30 +01:00
parent f97a1f1c54
commit e48b9ea106
3 changed files with 11 additions and 8 deletions
@@ -1,4 +1,4 @@
/* Copyright (C) 2024 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -69,11 +69,6 @@ template<> void Script::ToJSVal<IComponent*>(const ScriptRequest& rq, JS::Mutab
template<> void Script::ToJSVal<CParamNode>(const ScriptRequest& rq, JS::MutableHandleValue ret, CParamNode const& val)
{
val.ToJSVal(rq, true, ret);
// Prevent modifications to the object, so that it's safe to share between
// components and to reconstruct on deserialization
if (ret.isObject())
Script::DeepFreezeObject(rq, ret);
}
template<> void Script::ToJSVal<const CParamNode*>(const ScriptRequest& rq, JS::MutableHandleValue ret, const CParamNode* const& val)
+7 -1
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2024 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -25,6 +25,7 @@
#include "ps/CStrIntern.h"
#include "ps/Filesystem.h"
#include "ps/XML/Xeromyces.h"
#include "scriptinterface/Object.h"
#include "scriptinterface/ScriptRequest.h"
#include <boost/algorithm/string/classification.hpp>
@@ -382,7 +383,12 @@ void CParamNode::ToJSVal(const ScriptRequest& rq, bool cacheValue, JS::MutableHa
ConstructJSVal(rq, ret);
if (cacheValue)
{
if (ret.isObject())
Script::DeepFreezeObject(rq, ret);
m_ScriptVal.reset(new JS::PersistentRootedValue(rq.cx, ret));
}
}
void CParamNode::ConstructJSVal(const ScriptRequest& rq, JS::MutableHandleValue ret) const
+3 -1
View File
@@ -1,4 +1,4 @@
/* Copyright (C) 2022 Wildfire Games.
/* Copyright (C) 2025 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@@ -252,6 +252,8 @@ public:
* If @p cacheValue is true, then the same JS::Value will be returned each time
* this is called (regardless of whether you passed the same @p cx - be careful
* to only use the cache in one context).
* Cached object values are frozen, using DeepFreezeObject, so that it's safe to
* share between components and to reconstruct on deserialization.
* When caching, the lifetime of @p cx must be longer than the lifetime of this node.
* The cache will be reset if *this* node is modified (e.g. by LoadXML),
* but *not* if any child nodes are modified (so don't do that).