From 3dd89032d05b089e2012267020d816a39cdfdcbd Mon Sep 17 00:00:00 2001 From: RJ Date: Mon, 10 Jun 2024 02:25:32 -0400 Subject: [PATCH 1/3] Move loadOrderMechanism out of loop --- src/pluginlist.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/pluginlist.cpp b/src/pluginlist.cpp index 748ffbada..52b5d69d3 100644 --- a/src/pluginlist.cpp +++ b/src/pluginlist.cpp @@ -182,6 +182,8 @@ void PluginList::refresh(const QString& profileName, gamePlugins ? gamePlugins->lightPluginsAreSupported() : false; const bool overridePluginsAreSupported = gamePlugins ? gamePlugins->overridePluginsAreSupported() : false; + const bool loadOrderMechanismNone = + m_GamePlugin->loadOrderMechanism() == IPluginGame::LoadOrderMechanism::None; m_CurrentProfile = profileName; @@ -206,10 +208,8 @@ void PluginList::refresh(const QString& profileName, bool forceLoaded = Settings::instance().game().forceEnableCoreFiles() && primaryPlugins.contains(filename, Qt::CaseInsensitive); - bool forceEnabled = enabledPlugins.contains(filename, Qt::CaseInsensitive); - bool forceDisabled = - m_GamePlugin->loadOrderMechanism() == IPluginGame::LoadOrderMechanism::None && - !forceLoaded && !forceEnabled; + bool forceEnabled = enabledPlugins.contains(filename, Qt::CaseInsensitive); + bool forceDisabled = loadOrderMechanismNone && !forceLoaded && !forceEnabled; if (!lightPluginsAreSupported && filename.endsWith(".esl")) { forceDisabled = true; } From a178829512db4c1fcea20a86078ae51f577fac1a Mon Sep 17 00:00:00 2001 From: RJ Date: Mon, 10 Jun 2024 04:21:41 -0400 Subject: [PATCH 2/3] PluginList::refresh reduce number of loops --- src/pluginlist.cpp | 105 +++++++++++++++++----------------- src/shared/directoryentry.cpp | 1 + 2 files changed, 55 insertions(+), 51 deletions(-) diff --git a/src/pluginlist.cpp b/src/pluginlist.cpp index 52b5d69d3..540cdfc4f 100644 --- a/src/pluginlist.cpp +++ b/src/pluginlist.cpp @@ -187,77 +187,80 @@ void PluginList::refresh(const QString& profileName, m_CurrentProfile = profileName; - QStringList availablePlugins; + std::vector> availablePlugins; + QStringList archiveCandidates; - std::vector files = baseDirectory.getFiles(); - for (FileEntryPtr current : files) { + for (FileEntryPtr current : baseDirectory.getFiles()) { if (current.get() == nullptr) { continue; } - QString filename = ToQString(current->getName()); + const QString& filename = ToQString(current->getName()); if (filename.endsWith(".esp", Qt::CaseInsensitive) || filename.endsWith(".esm", Qt::CaseInsensitive) || filename.endsWith(".esl", Qt::CaseInsensitive)) { + availablePlugins.emplace_back(filename, current); + } else if (filename.endsWith(".bsa", Qt::CaseInsensitive) || + filename.endsWith("ba2", Qt::CaseInsensitive)) { + archiveCandidates.append(filename); + } + } - availablePlugins.append(filename); + for (const auto& [filename, current] : availablePlugins) { + if (m_ESPsByName.contains(filename)) { + continue; + } - if (m_ESPsByName.find(filename) != m_ESPsByName.end()) { - continue; - } + bool forceLoaded = Settings::instance().game().forceEnableCoreFiles() && + primaryPlugins.contains(filename, Qt::CaseInsensitive); + bool forceEnabled = enabledPlugins.contains(filename, Qt::CaseInsensitive); + bool forceDisabled = loadOrderMechanismNone && !forceLoaded && !forceEnabled; + if (!lightPluginsAreSupported && filename.endsWith(".esl")) { + forceDisabled = true; + } - bool forceLoaded = Settings::instance().game().forceEnableCoreFiles() && - primaryPlugins.contains(filename, Qt::CaseInsensitive); - bool forceEnabled = enabledPlugins.contains(filename, Qt::CaseInsensitive); - bool forceDisabled = loadOrderMechanismNone && !forceLoaded && !forceEnabled; - if (!lightPluginsAreSupported && filename.endsWith(".esl")) { - forceDisabled = true; - } + bool archive = false; + try { + FilesOrigin& origin = baseDirectory.getOriginByID(current->getOrigin(archive)); - bool archive = false; - try { - FilesOrigin& origin = baseDirectory.getOriginByID(current->getOrigin(archive)); - - // name without extension - QString baseName = QFileInfo(filename).completeBaseName(); - - QString iniPath = baseName + ".ini"; - bool hasIni = baseDirectory.findFile(ToWString(iniPath)).get() != nullptr; - std::set loadedArchives; - QString candidateName; - for (FileEntryPtr archiveCandidate : files) { - candidateName = ToQString(archiveCandidate->getName()); - if (candidateName.startsWith(baseName, Qt::CaseInsensitive) && - (candidateName.endsWith(".bsa", Qt::CaseInsensitive) || - candidateName.endsWith(".ba2", Qt::CaseInsensitive))) { - loadedArchives.insert(candidateName); - } - } + // name without extension + QString baseName = QFileInfo(filename).completeBaseName(); - QString originName = ToQString(origin.getName()); - unsigned int modIndex = ModInfo::getIndex(originName); - if (modIndex != UINT_MAX) { - ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); - originName = modInfo->name(); + QString iniPath = baseName + ".ini"; + bool hasIni = baseDirectory.findFile(ToWString(iniPath)).get() != nullptr; + std::set loadedArchives; + for (const auto& archiveName : archiveCandidates) { + if (archiveName.startsWith(baseName, Qt::CaseInsensitive)) { + loadedArchives.insert(archiveName); } + } - m_ESPs.push_back(ESPInfo(filename, forceLoaded, forceEnabled, forceDisabled, - originName, ToQString(current->getFullPath()), hasIni, - loadedArchives, lightPluginsAreSupported, - overridePluginsAreSupported)); - m_ESPs.rbegin()->priority = -1; - } catch (const std::exception& e) { - reportError( - tr("failed to update esp info for file %1 (source id: %2), error: %3") - .arg(filename) - .arg(current->getOrigin(archive)) - .arg(e.what())); + QString originName = ToQString(origin.getName()); + unsigned int modIndex = ModInfo::getIndex(originName); + if (modIndex != UINT_MAX) { + ModInfo::Ptr modInfo = ModInfo::getByIndex(modIndex); + originName = modInfo->name(); } + + m_ESPs.emplace_back(ESPInfo(filename, forceLoaded, forceEnabled, forceDisabled, + originName, ToQString(current->getFullPath()), hasIni, + loadedArchives, lightPluginsAreSupported, + overridePluginsAreSupported)); + m_ESPs.rbegin()->priority = -1; + } catch (const std::exception& e) { + reportError(tr("failed to update esp info for file %1 (source id: %2), error: %3") + .arg(filename) + .arg(current->getOrigin(archive)) + .arg(e.what())); } } for (const auto& espName : m_ESPsByName) { - if (!availablePlugins.contains(espName.first, Qt::CaseInsensitive)) { + auto it = std::ranges::find_if( + availablePlugins, [&espName](const std::pair& ele) { + return ele.first.compare(espName.first, Qt::CaseInsensitive) == 0; + }); + if (it == availablePlugins.end()) { m_ESPs[espName.second].name = ""; } } diff --git a/src/shared/directoryentry.cpp b/src/shared/directoryentry.cpp index 5468beea4..01c325ef1 100644 --- a/src/shared/directoryentry.cpp +++ b/src/shared/directoryentry.cpp @@ -299,6 +299,7 @@ int DirectoryEntry::anyOrigin() const std::vector DirectoryEntry::getFiles() const { std::vector result; + result.reserve(m_Files.size()); for (auto iter = m_Files.begin(); iter != m_Files.end(); ++iter) { result.push_back(m_FileRegister->getFile(iter->second)); From f0dac4e86a8733aee62fd0a384abad4556fca9df Mon Sep 17 00:00:00 2001 From: RJ Date: Mon, 10 Jun 2024 06:00:35 -0400 Subject: [PATCH 3/3] Use unordered map --- src/pluginlist.cpp | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/src/pluginlist.cpp b/src/pluginlist.cpp index 540cdfc4f..5b056f83c 100644 --- a/src/pluginlist.cpp +++ b/src/pluginlist.cpp @@ -187,7 +187,7 @@ void PluginList::refresh(const QString& profileName, m_CurrentProfile = profileName; - std::vector> availablePlugins; + std::unordered_map availablePlugins; QStringList archiveCandidates; for (FileEntryPtr current : baseDirectory.getFiles()) { @@ -199,7 +199,7 @@ void PluginList::refresh(const QString& profileName, if (filename.endsWith(".esp", Qt::CaseInsensitive) || filename.endsWith(".esm", Qt::CaseInsensitive) || filename.endsWith(".esl", Qt::CaseInsensitive)) { - availablePlugins.emplace_back(filename, current); + availablePlugins.insert(std::make_pair(filename, current)); } else if (filename.endsWith(".bsa", Qt::CaseInsensitive) || filename.endsWith("ba2", Qt::CaseInsensitive)) { archiveCandidates.append(filename); @@ -242,10 +242,10 @@ void PluginList::refresh(const QString& profileName, originName = modInfo->name(); } - m_ESPs.emplace_back(ESPInfo(filename, forceLoaded, forceEnabled, forceDisabled, - originName, ToQString(current->getFullPath()), hasIni, - loadedArchives, lightPluginsAreSupported, - overridePluginsAreSupported)); + m_ESPs.emplace_back(filename, forceLoaded, forceEnabled, forceDisabled, + originName, ToQString(current->getFullPath()), hasIni, + loadedArchives, lightPluginsAreSupported, + overridePluginsAreSupported); m_ESPs.rbegin()->priority = -1; } catch (const std::exception& e) { reportError(tr("failed to update esp info for file %1 (source id: %2), error: %3") @@ -256,11 +256,7 @@ void PluginList::refresh(const QString& profileName, } for (const auto& espName : m_ESPsByName) { - auto it = std::ranges::find_if( - availablePlugins, [&espName](const std::pair& ele) { - return ele.first.compare(espName.first, Qt::CaseInsensitive) == 0; - }); - if (it == availablePlugins.end()) { + if (!availablePlugins.contains(espName.first)) { m_ESPs[espName.second].name = ""; } }