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;
mutable QMutex _customTypeProtect;
mutable QReadWriteLock _customTypeProtect { QReadWriteLock::Recursive };
CustomMarshalMap _customTypes;
CustomPrototypeMap _customPrototypes;
ScriptValue _nullValue;

View file

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