mirror of
https://github.com/Cockatrice/Cockatrice.git
synced 2026-07-29 12:20:24 -07:00
[Card Database] Improve loading times through binary cache (#7051)
* [Card Database] Improve loading times through binary cache Took 10 minutes Took 9 minutes Took 16 seconds * [Card Database] Remove lib qt include Took 18 minutes Took 14 seconds * Downgrade to 6.3 datastream Took 5 minutes * go up to 6.4 datastream Took 1 minute * Address comments * Small bug fixes Took 20 minutes Took 10 seconds * More fixes. Took 4 minutes Took 4 seconds * Even more fixes. Took 11 minutes Took 4 seconds * More fixes. Took 6 minutes Took 26 seconds Took 8 minutes * Namespace instead of class Took 6 minutes --------- Co-authored-by: Lukas Brübach <Bruebach.Lukas@bdosecurity.de>
This commit is contained in:
parent
4bb8831531
commit
749223c2dc
31 changed files with 1604 additions and 106 deletions
|
|
@ -1,5 +1,7 @@
|
|||
#include "card_database_settings.h"
|
||||
|
||||
#include <QMutexLocker>
|
||||
|
||||
CardDatabaseSettings::CardDatabaseSettings(const QString &settingPath, QObject *parent)
|
||||
: SettingsManager(settingPath + "cardDatabase.ini", QString(), QString(), parent)
|
||||
{
|
||||
|
|
@ -7,32 +9,73 @@ CardDatabaseSettings::CardDatabaseSettings(const QString &settingPath, QObject *
|
|||
|
||||
void CardDatabaseSettings::setSortKey(QString shortName, unsigned int sortKey)
|
||||
{
|
||||
setValue(sortKey, "sortkey", "sets", std::move(shortName));
|
||||
setValue(sortKey, "sortkey", "sets", shortName);
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
setOptionsCache[shortName].sortKey = sortKey;
|
||||
}
|
||||
|
||||
void CardDatabaseSettings::setEnabled(QString shortName, bool enabled)
|
||||
{
|
||||
setValue(enabled, "enabled", "sets", std::move(shortName));
|
||||
setValue(enabled, "enabled", "sets", shortName);
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
setOptionsCache[shortName].enabled = enabled;
|
||||
}
|
||||
|
||||
void CardDatabaseSettings::setIsKnown(QString shortName, bool isknown)
|
||||
{
|
||||
setValue(isknown, "isknown", "sets", std::move(shortName));
|
||||
setValue(isknown, "isknown", "sets", shortName);
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
setOptionsCache[shortName].isKnown = isknown;
|
||||
}
|
||||
|
||||
void CardDatabaseSettings::ensureSetOptionsLoaded() const
|
||||
{
|
||||
if (setOptionsLoaded) {
|
||||
return;
|
||||
}
|
||||
QSettings settings(getSettings());
|
||||
settings.beginGroup("sets");
|
||||
const QStringList groups = settings.childGroups();
|
||||
for (const QString &group : groups) {
|
||||
settings.beginGroup(group);
|
||||
SetOptions &o = setOptionsCache[group];
|
||||
o.sortKey = settings.value("sortkey", 0).toUInt();
|
||||
o.enabled = settings.value("enabled", true).toBool();
|
||||
o.isKnown = settings.value("isknown", true).toBool();
|
||||
settings.endGroup();
|
||||
}
|
||||
setOptionsLoaded = true;
|
||||
}
|
||||
|
||||
unsigned int CardDatabaseSettings::getSortKey(QString shortName) const
|
||||
{
|
||||
return getValue("sortkey", "sets", std::move(shortName)).toUInt();
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
return setOptionsCache.value(shortName).sortKey;
|
||||
}
|
||||
|
||||
bool CardDatabaseSettings::isEnabled(QString shortName) const
|
||||
{
|
||||
return getValue("enabled", "sets", std::move(shortName)).toBool();
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
return setOptionsCache.value(shortName).enabled;
|
||||
}
|
||||
|
||||
bool CardDatabaseSettings::isKnown(QString shortName) const
|
||||
{
|
||||
return getValue("isknown", "sets", std::move(shortName)).toBool();
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
return setOptionsCache.value(shortName).isKnown;
|
||||
}
|
||||
|
||||
ICardSetPriorityController::SetOptions CardDatabaseSettings::getSetOptions(QString shortName) const
|
||||
{
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
return setOptionsCache.value(shortName);
|
||||
}
|
||||
|
||||
void CardDatabaseSettings::saveSets(const QVector<ICardSetPriorityController::SetSaveData> &data)
|
||||
|
|
@ -47,4 +90,12 @@ void CardDatabaseSettings::saveSets(const QVector<ICardSetPriorityController::Se
|
|||
}
|
||||
s.endGroup();
|
||||
});
|
||||
|
||||
QMutexLocker lock(&setOptionsMutex);
|
||||
ensureSetOptionsLoaded();
|
||||
for (const auto &entry : data) {
|
||||
SetOptions &o = setOptionsCache[entry.shortName];
|
||||
o.sortKey = entry.sortKey;
|
||||
o.enabled = entry.enabled;
|
||||
}
|
||||
}
|
||||
|
|
@ -10,6 +10,8 @@
|
|||
|
||||
#include "settings_manager.h"
|
||||
|
||||
#include <QHash>
|
||||
#include <QMutex>
|
||||
#include <libcockatrice/interfaces/interface_card_set_priority_controller.h>
|
||||
|
||||
class CardDatabaseSettings : public SettingsManager, public ICardSetPriorityController
|
||||
|
|
@ -26,10 +28,23 @@ public:
|
|||
bool isEnabled(QString shortName) const override;
|
||||
bool isKnown(QString shortName) const override;
|
||||
|
||||
SetOptions getSetOptions(QString shortName) const override;
|
||||
|
||||
void saveSets(const QVector<SetSaveData> &data) override;
|
||||
|
||||
private:
|
||||
explicit CardDatabaseSettings(const QString &settingPath, QObject *parent = nullptr);
|
||||
|
||||
// In-memory cache of set priority options. QSettings re-opens and re-parses
|
||||
// the whole .ini on every access, which is catastrophic when CardSet
|
||||
// construction calls getSortKey/isEnabled/isKnown once per set during the
|
||||
// database load (thousands of file parses). The cache is populated from a
|
||||
// single QSettings session and kept consistent with the writes below.
|
||||
mutable QMutex setOptionsMutex;
|
||||
mutable QHash<QString, SetOptions> setOptionsCache;
|
||||
mutable bool setOptionsLoaded = false;
|
||||
|
||||
void ensureSetOptionsLoaded() const;
|
||||
};
|
||||
|
||||
#endif // CARDDATABASESETTINGS_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue