Replaced regular mutex with a read write one to improve performance

This commit is contained in:
ksuprynowicz 2022-08-18 17:12:37 +02:00
parent ad57a5e6fd
commit 5a9b0ccfb0
2 changed files with 11 additions and 6 deletions

View file

@ -181,7 +181,7 @@ protected:
QPointer<ScriptManager> _scriptManager; QPointer<ScriptManager> _scriptManager;
mutable QMutex _customTypeProtect; mutable QReadWriteLock _customTypeProtect { QReadWriteLock::Recursive };
CustomMarshalMap _customTypes; CustomMarshalMap _customTypes;
CustomPrototypeMap _customPrototypes; CustomPrototypeMap _customPrototypes;
ScriptValue _nullValue; ScriptValue _nullValue;

View file

@ -27,8 +27,9 @@ void ScriptEngineQtScript::setDefaultPrototype(int metaTypeId, const ScriptValue
ScriptValueQtWrapper* unwrappedPrototype = ScriptValueQtWrapper::unwrap(prototype); ScriptValueQtWrapper* unwrappedPrototype = ScriptValueQtWrapper::unwrap(prototype);
if (unwrappedPrototype) { if (unwrappedPrototype) {
const QScriptValue& scriptPrototype = unwrappedPrototype->toQtValue(); const QScriptValue& scriptPrototype = unwrappedPrototype->toQtValue();
QMutexLocker guard(&_customTypeProtect); _customTypeProtect.lockForWrite();
_customPrototypes.insert(metaTypeId, scriptPrototype); _customPrototypes.insert(metaTypeId, scriptPrototype);
_customTypeProtect.unlock();
} }
} }
@ -36,12 +37,13 @@ void ScriptEngineQtScript::registerCustomType(int type,
ScriptEngine::MarshalFunction marshalFunc, ScriptEngine::MarshalFunction marshalFunc,
ScriptEngine::DemarshalFunction demarshalFunc) ScriptEngine::DemarshalFunction demarshalFunc)
{ {
QMutexLocker guard(&_customTypeProtect); _customTypeProtect.lockForWrite();
// storing it in a map for our own benefit // storing it in a map for our own benefit
CustomMarshal& customType = _customTypes.insert(type, CustomMarshal()).value(); CustomMarshal& customType = _customTypes.insert(type, CustomMarshal()).value();
customType.demarshalFunc = demarshalFunc; customType.demarshalFunc = demarshalFunc;
customType.marshalFunc = marshalFunc; customType.marshalFunc = marshalFunc;
_customTypeProtect.unlock();
} }
Q_DECLARE_METATYPE(ScriptValue); Q_DECLARE_METATYPE(ScriptValue);
@ -225,11 +227,12 @@ bool ScriptEngineQtScript::castValueToVariant(const QScriptValue& val, QVariant&
// do we have a registered handler for this type? // do we have a registered handler for this type?
ScriptEngine::DemarshalFunction demarshalFunc = nullptr; ScriptEngine::DemarshalFunction demarshalFunc = nullptr;
{ {
QMutexLocker guard(&_customTypeProtect); _customTypeProtect.lockForRead();
CustomMarshalMap::const_iterator lookup = _customTypes.find(destTypeId); CustomMarshalMap::const_iterator lookup = _customTypes.find(destTypeId);
if (lookup != _customTypes.cend()) { if (lookup != _customTypes.cend()) {
demarshalFunc = lookup.value().demarshalFunc; demarshalFunc = lookup.value().demarshalFunc;
} }
_customTypeProtect.unlock();
} }
if (demarshalFunc) { if (demarshalFunc) {
dest = QVariant(destTypeId, static_cast<void*>(NULL)); dest = QVariant(destTypeId, static_cast<void*>(NULL));
@ -400,11 +403,12 @@ QScriptValue ScriptEngineQtScript::castVariantToValue(const QVariant& val) {
// do we have a registered handler for this type? // do we have a registered handler for this type?
ScriptEngine::MarshalFunction marshalFunc = nullptr; ScriptEngine::MarshalFunction marshalFunc = nullptr;
{ {
QMutexLocker guard(&_customTypeProtect); _customTypeProtect.lockForRead();
CustomMarshalMap::const_iterator lookup = _customTypes.find(valTypeId); CustomMarshalMap::const_iterator lookup = _customTypes.find(valTypeId);
if (lookup != _customTypes.cend()) { if (lookup != _customTypes.cend()) {
marshalFunc = lookup.value().marshalFunc; marshalFunc = lookup.value().marshalFunc;
} }
_customTypeProtect.unlock();
} }
if (marshalFunc) { if (marshalFunc) {
ScriptValue wrappedVal = marshalFunc(this, val.constData()); ScriptValue wrappedVal = marshalFunc(this, val.constData());
@ -455,11 +459,12 @@ QScriptValue ScriptEngineQtScript::castVariantToValue(const QVariant& val) {
} }
// have we set a prototype'd variant? // have we set a prototype'd variant?
{ {
QMutexLocker guard(&_customTypeProtect); _customTypeProtect.lockForRead();
CustomPrototypeMap::const_iterator lookup = _customPrototypes.find(valTypeId); CustomPrototypeMap::const_iterator lookup = _customPrototypes.find(valTypeId);
if (lookup != _customPrototypes.cend()) { if (lookup != _customPrototypes.cend()) {
return ScriptVariantQtProxy::newVariant(this, val, lookup.value()); return ScriptVariantQtProxy::newVariant(this, val, lookup.value());
} }
_customTypeProtect.unlock();
} }
// just do a generic variant // just do a generic variant
return QScriptEngine::newVariant(val); return QScriptEngine::newVariant(val);