From f118b840c29e2efa60b6c1e732c0a4fd7649e7f9 Mon Sep 17 00:00:00 2001 From: acetone Date: Mon, 21 Nov 2022 21:00:35 +0300 Subject: [PATCH] optimization --- src/webpage.cpp | 59 ++++++++++++++++++++++++++++++------------------- 1 file changed, 36 insertions(+), 23 deletions(-) diff --git a/src/webpage.cpp b/src/webpage.cpp index a5618a9..1f3dda3 100644 --- a/src/webpage.cpp +++ b/src/webpage.cpp @@ -181,37 +181,50 @@ void WebPage::topDestinations(QString &document) void WebPage::blockedDestinations(QString &document) { - QMap> uniqueRecords; - - QString blockedDestinationsBlock {m_blockedDestinationsBlock}; - QString blockedDestinationsList; - for (const auto& inst: g::instanses) + static QString blockedDestinationsBlock; + static bool inited = false; + if (not inited) { - QMapIterator mIter(inst.second->blackList()); + inited = true; + + QMap> uniqueRecords; + + QString blockedDestinationsList; + + for (const auto& inst: g::instanses) + { + QMapIterator mIter(inst.second->blackList()); + while (mIter.hasNext()) + { + auto record = mIter.next(); + for (const auto& addr: record.value()) + { + uniqueRecords[record.key()].insert(addr); + } + + if (not uniqueRecords.contains(record.key())) + { + uniqueRecords.insert(record.key(), QSet()); + } + } + } + + QMapIterator> mIter(uniqueRecords); while (mIter.hasNext()) { auto record = mIter.next(); - for (const auto& addr: record.value()) - { - uniqueRecords[record.key()].insert(addr); - } + blockedDestinationsList += blockedDestinationItem(record.key(), record.value().values()); + } - if (not uniqueRecords.contains(record.key())) - { - uniqueRecords.insert(record.key(), QSet()); - } + if (not blockedDestinationsList.isEmpty()) + { + blockedDestinationsBlock = m_blockedDestinationsBlock; + blockedDestinationsBlock.replace("{{VALUE}}", blockedDestinationsList); + blockedDestinationsBlock = '\n'+blockedDestinationsBlock; } } - QMapIterator> mIter(uniqueRecords); - while (mIter.hasNext()) - { - auto record = mIter.next(); - blockedDestinationsList += blockedDestinationItem(record.key(), record.value().values()); - } - - blockedDestinationsBlock.replace("{{VALUE}}", blockedDestinationsList); - document.replace(g::c::HA_BLOCKED_DESTINTIONS, blockedDestinationsList.isEmpty() ? "" : "\n"+blockedDestinationsBlock); + document.replace(g::c::HA_BLOCKED_DESTINTIONS, blockedDestinationsBlock); } void WebPage::informationBlock(QString &document)