Avoid double lookups

This commit is contained in:
Chocobo1 2019-09-08 14:00:08 +08:00
parent d2c21ce507
commit daf52a2610
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
2 changed files with 11 additions and 7 deletions

View file

@ -202,17 +202,19 @@ bool SettingsStorage::save()
QVariant SettingsStorage::loadValue(const QString &key, const QVariant &defaultValue) const QVariant SettingsStorage::loadValue(const QString &key, const QVariant &defaultValue) const
{ {
QReadLocker locker(&m_lock); const QReadLocker locker(&m_lock);
return m_data.value(mapKey(key), defaultValue); return m_data.value(mapKey(key), defaultValue);
} }
void SettingsStorage::storeValue(const QString &key, const QVariant &value) void SettingsStorage::storeValue(const QString &key, const QVariant &value)
{ {
const QString realKey = mapKey(key); const QString realKey = mapKey(key);
QWriteLocker locker(&m_lock); const QWriteLocker locker(&m_lock);
if (m_data.value(realKey) != value) {
QVariant &currentValue = m_data[realKey];
if (currentValue != value) {
m_dirty = true; m_dirty = true;
m_data.insert(realKey, value); currentValue = value;
m_timer.start(); m_timer.start();
} }
} }
@ -220,10 +222,9 @@ void SettingsStorage::storeValue(const QString &key, const QVariant &value)
void SettingsStorage::removeValue(const QString &key) void SettingsStorage::removeValue(const QString &key)
{ {
const QString realKey = mapKey(key); const QString realKey = mapKey(key);
QWriteLocker locker(&m_lock); const QWriteLocker locker(&m_lock);
if (m_data.contains(realKey)) { if (m_data.remove(realKey) > 0) {
m_dirty = true; m_dirty = true;
m_data.remove(realKey);
m_timer.start(); m_timer.start();
} }
} }

View file

@ -68,6 +68,9 @@ public:
CachedSettingValue<T> &operator=(const T &newValue) CachedSettingValue<T> &operator=(const T &newValue)
{ {
if (m_value == newValue)
return *this;
m_value = newValue; m_value = newValue;
storeValue(m_value); storeValue(m_value);
return *this; return *this;