Merge pull request #690 from overte-org/fix/entity_server_mem_leak

Fix entity server memory leak and improve performance
This commit is contained in:
ksuprynowicz 2023-10-31 19:04:34 +01:00 committed by GitHub
commit 894f8cec2b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 13 additions and 10 deletions

View file

@ -2553,9 +2553,8 @@ bool EntityTree::writeToMap(QVariantMap& entityDescription, OctreeElementPointer
}
entityDescription["DataVersion"] = _persistDataVersion;
entityDescription["Id"] = _persistID;
// V8TODO: Creating new script engine each time is very inefficient
ScriptEnginePointer engine = newScriptEngine();
RecurseOctreeToMapOperator theOperator(entityDescription, element, engine.get(), skipDefaultValues,
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
RecurseOctreeToMapOperator theOperator(entityDescription, element, scriptEngine.get(), skipDefaultValues,
skipThoseWithBadParents, _myAvatar);
withReadLock([&] {
recurseTreeWithOperator(&theOperator);
@ -2704,8 +2703,6 @@ bool EntityTree::readFromMap(QVariantMap& map, const bool isImport) {
// to a ScriptValue, and then to EntityItemProperties. These properties are used
// to add the new entity to the EntityTree.
QVariantList entitiesQList = map["Entities"].toList();
// V8TODO: Creating new script engine each time is very inefficient
ScriptEnginePointer scriptEngine = newScriptEngine();
if (entitiesQList.length() == 0) {
qCDebug(entities) << "EntityTree::readFromMap: entitiesQList.length() == 0, Empty map or invalidly formed file";
@ -2730,9 +2727,12 @@ bool EntityTree::readFromMap(QVariantMap& map, const bool isImport) {
" mapped it to parentJointIndex " << entityMap["parentJointIndex"].toInt();
}
ScriptValue entityScriptValue = variantMapToScriptValue(entityMap, *scriptEngine);
EntityItemProperties properties;
EntityItemPropertiesFromScriptValueIgnoreReadOnly(entityScriptValue, properties);
{
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
ScriptValue entityScriptValue = variantMapToScriptValue(entityMap, *scriptEngine);
EntityItemPropertiesFromScriptValueIgnoreReadOnly(entityScriptValue, properties);
}
EntityItemID entityItemID;
if (entityMap.contains("id")) {
@ -2881,9 +2881,8 @@ bool EntityTree::readFromMap(QVariantMap& map, const bool isImport) {
}
bool EntityTree::writeToJSON(QString& jsonString, const OctreeElementPointer& element) {
// V8TODO: Creating new script engine each time is very inefficient
ScriptEnginePointer engine = newScriptEngine();
RecurseOctreeToJSONOperator theOperator(element, engine.get(), jsonString);
const std::lock_guard<std::mutex> scriptLock(scriptEngineMutex);
RecurseOctreeToJSONOperator theOperator(element, scriptEngine.get(), jsonString);
withReadLock([&] {
recurseTreeWithOperator(&theOperator);
});

View file

@ -385,6 +385,10 @@ private:
// Return an AACube containing object and all its entity descendants
AACube updateEntityQueryAACubeWorker(SpatiallyNestablePointer object, EntityEditPacketSender* packetSender,
MovingEntitiesOperator& moveOperator, bool force, bool tellServer);
// Script engine for writing entity tree data to and from JSON
std::mutex scriptEngineMutex;
ScriptEnginePointer scriptEngine{ newScriptEngine() };
};
void convertGrabUserDataToProperties(EntityItemProperties& properties);