From 74edcf8faf023fc4e18161f5eca80b201bb21871 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 20:22:00 +0200 Subject: [PATCH 01/10] refactor: move implementations to translation unit --- src/util/DebugCount.cpp | 76 +++++++++++++++++++++++++++++++- src/util/DebugCount.hpp | 98 +++-------------------------------------- 2 files changed, 81 insertions(+), 93 deletions(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index 331aa605a2a..90d2fa612e8 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -1,7 +1,79 @@ -#include "DebugCount.hpp" +#include "util/DebugCount.hpp" + +#include "common/UniqueAccess.hpp" + +#include + +namespace { + +using namespace chatterino; + +struct Count { + int64_t value; +}; + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +UniqueAccess> COUNTS; + +} // namespace namespace chatterino { -UniqueAccess> DebugCount::counts_; +void DebugCount::set(const QString &name, const int64_t &amount) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->insert(name, {amount}); + } + else + { + it.value().value = amount; + } +} + +void DebugCount::increase(const QString &name, const int64_t &amount) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->insert(name, {amount}); + } + else + { + it.value().value += amount; + } +} + +void DebugCount::decrease(const QString &name, const int64_t &amount) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->insert(name, {-amount}); + } + else + { + it.value().value -= amount; + } +} + +QString DebugCount::getDebugText() +{ + auto counts = COUNTS.access(); + + QString text; + for (auto it = counts->begin(); it != counts->end(); it++) + { + text += it.key() + ": " + QString::number(it.value().value) + "\n"; + } + return text; +} } // namespace chatterino diff --git a/src/util/DebugCount.hpp b/src/util/DebugCount.hpp index 35f9fc62175..3b4e56e72ff 100644 --- a/src/util/DebugCount.hpp +++ b/src/util/DebugCount.hpp @@ -1,111 +1,27 @@ #pragma once -#include "common/UniqueAccess.hpp" - -#include #include -#include -#include - namespace chatterino { class DebugCount { public: - static void increase(const QString &name) - { - auto counts = counts_.access(); - - auto it = counts->find(name); - if (it == counts->end()) - { - counts->insert(name, 1); - } - else - { - reinterpret_cast(it.value())++; - } - } + static void set(const QString &name, const int64_t &amount); - static void set(const QString &name, const int64_t &amount) - { - auto counts = counts_.access(); - - auto it = counts->find(name); - if (it == counts->end()) - { - counts->insert(name, amount); - } - else - { - reinterpret_cast(it.value()) = amount; - } - } - - static void increase(const QString &name, const int64_t &amount) + static void increase(const QString &name, const int64_t &amount); + static void increase(const QString &name) { - auto counts = counts_.access(); - - auto it = counts->find(name); - if (it == counts->end()) - { - counts->insert(name, amount); - } - else - { - reinterpret_cast(it.value()) += amount; - } + DebugCount::increase(name, 1); } + static void decrease(const QString &name, const int64_t &amount); static void decrease(const QString &name) { - auto counts = counts_.access(); - - auto it = counts->find(name); - if (it == counts->end()) - { - counts->insert(name, -1); - } - else - { - reinterpret_cast(it.value())--; - } - } - static void decrease(const QString &name, const int64_t &amount) - { - auto counts = counts_.access(); - - auto it = counts->find(name); - if (it == counts->end()) - { - counts->insert(name, -amount); - } - else - { - reinterpret_cast(it.value()) -= amount; - } - } - - static QString getDebugText() - { - auto counts = counts_.access(); - - QString text; - for (auto it = counts->begin(); it != counts->end(); it++) - { - text += it.key() + ": " + QString::number(it.value()) + "\n"; - } - return text; - } - - QString toString() - { - return ""; + DebugCount::decrease(name, 1); } -private: - static UniqueAccess> counts_; + static QString getDebugText(); }; } // namespace chatterino From e3352711a8c7fd30cca0413c93b4a6485570638b Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 20:28:26 +0200 Subject: [PATCH 02/10] feat: add DebugCount::configure --- src/util/DebugCount.cpp | 18 +++++++++++++++++- src/util/DebugCount.hpp | 9 +++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index 90d2fa612e8..e5691215d5c 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -9,7 +9,8 @@ namespace { using namespace chatterino; struct Count { - int64_t value; + int64_t value = 0; + DebugCount::Flags flags = DebugCount::Flag::None; }; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) @@ -19,6 +20,21 @@ UniqueAccess> COUNTS; namespace chatterino { +void DebugCount::configure(const QString &name, Flags flags) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->insert(name, {.flags = flags}); + } + else + { + it.value().flags = flags; + } +} + void DebugCount::set(const QString &name, const int64_t &amount) { auto counts = COUNTS.access(); diff --git a/src/util/DebugCount.hpp b/src/util/DebugCount.hpp index 3b4e56e72ff..8ca1a3eb00f 100644 --- a/src/util/DebugCount.hpp +++ b/src/util/DebugCount.hpp @@ -1,5 +1,7 @@ #pragma once +#include "common/FlagsEnum.hpp" + #include namespace chatterino { @@ -7,6 +9,13 @@ namespace chatterino { class DebugCount { public: + enum class Flag : uint16_t { + None = 0, + }; + using Flags = FlagsEnum; + + static void configure(const QString &name, Flags flags); + static void set(const QString &name, const int64_t &amount); static void increase(const QString &name, const int64_t &amount); From f24cca57c27d1644a40b3128e65437621850d53d Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 20:33:47 +0200 Subject: [PATCH 03/10] refactor: use std map --- src/util/DebugCount.cpp | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index e5691215d5c..4c036364c51 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -2,7 +2,7 @@ #include "common/UniqueAccess.hpp" -#include +#include namespace { @@ -14,7 +14,7 @@ struct Count { }; // NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) -UniqueAccess> COUNTS; +UniqueAccess> COUNTS; } // namespace @@ -27,11 +27,11 @@ void DebugCount::configure(const QString &name, Flags flags) auto it = counts->find(name); if (it == counts->end()) { - counts->insert(name, {.flags = flags}); + counts->emplace(name, Count{.flags = flags}); } else { - it.value().flags = flags; + it->second.flags = flags; } } @@ -42,11 +42,11 @@ void DebugCount::set(const QString &name, const int64_t &amount) auto it = counts->find(name); if (it == counts->end()) { - counts->insert(name, {amount}); + counts->emplace(name, Count{amount}); } else { - it.value().value = amount; + it->second.value = amount; } } @@ -57,11 +57,11 @@ void DebugCount::increase(const QString &name, const int64_t &amount) auto it = counts->find(name); if (it == counts->end()) { - counts->insert(name, {amount}); + counts->emplace(name, Count{amount}); } else { - it.value().value += amount; + it->second.value += amount; } } @@ -72,11 +72,11 @@ void DebugCount::decrease(const QString &name, const int64_t &amount) auto it = counts->find(name); if (it == counts->end()) { - counts->insert(name, {-amount}); + counts->emplace(name, Count{-amount}); } else { - it.value().value -= amount; + it->second.value -= amount; } } @@ -85,9 +85,9 @@ QString DebugCount::getDebugText() auto counts = COUNTS.access(); QString text; - for (auto it = counts->begin(); it != counts->end(); it++) + for (const auto &[key, count] : *counts) { - text += it.key() + ": " + QString::number(it.value().value) + "\n"; + text += key + ": " + QString::number(count.value) + "\n"; } return text; } From 62441bd0c65f39ee32b927b960003a57575256fb Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 20:34:57 +0200 Subject: [PATCH 04/10] refactor: use `QStringBuilder` --- src/util/DebugCount.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index 4c036364c51..e478a1c2202 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -2,6 +2,8 @@ #include "common/UniqueAccess.hpp" +#include + #include namespace { @@ -87,7 +89,7 @@ QString DebugCount::getDebugText() QString text; for (const auto &[key, count] : *counts) { - text += key + ": " + QString::number(count.value) + "\n"; + text += key % ": " % QString::number(count.value) % '\n'; } return text; } From 3afb01d9892eceae1d58d58c51ce675dc493fd14 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 20:47:54 +0200 Subject: [PATCH 05/10] feat: use QLocale for formatting numbers --- src/util/DebugCount.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index e478a1c2202..00b861451be 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -2,6 +2,7 @@ #include "common/UniqueAccess.hpp" +#include #include #include @@ -85,11 +86,12 @@ void DebugCount::decrease(const QString &name, const int64_t &amount) QString DebugCount::getDebugText() { auto counts = COUNTS.access(); + QLocale locale; QString text; for (const auto &[key, count] : *counts) { - text += key % ": " % QString::number(count.value) % '\n'; + text += key % ": " % locale.toString(count.value) % '\n'; } return text; } From 2a99c4501e601a9f0c962124445ef0f90ec10990 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 21:34:37 +0200 Subject: [PATCH 06/10] feat: add data size flag --- src/util/DebugCount.cpp | 19 +++++++++++++++++-- src/util/DebugCount.hpp | 2 ++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index 00b861451be..1fe915cf2f5 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -85,13 +85,28 @@ void DebugCount::decrease(const QString &name, const int64_t &amount) QString DebugCount::getDebugText() { +#if QT_VERSION > QT_VERSION_CHECK(5, 13, 0) + static const QLocale locale(QLocale::English); +#else + static QLocale locale(QLocale::English); +#endif + auto counts = COUNTS.access(); - QLocale locale; QString text; for (const auto &[key, count] : *counts) { - text += key % ": " % locale.toString(count.value) % '\n'; + QString formatted; + if (count.flags.has(Flag::DataSize)) + { + formatted = locale.formattedDataSize(count.value); + } + else + { + formatted = locale.toString(static_cast(count.value)); + } + + text += key % ": " % formatted % '\n'; } return text; } diff --git a/src/util/DebugCount.hpp b/src/util/DebugCount.hpp index 8ca1a3eb00f..ef23639840b 100644 --- a/src/util/DebugCount.hpp +++ b/src/util/DebugCount.hpp @@ -11,6 +11,8 @@ class DebugCount public: enum class Flag : uint16_t { None = 0, + /// The value is a data size in bytes + DataSize = 1 << 0, }; using Flags = FlagsEnum; From 0f23fa48060f7fa81fb2b3c351d04ff40966a79a Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 21:34:56 +0200 Subject: [PATCH 07/10] feat: use data size option for images --- src/messages/Image.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 0b8e8f9e57d..0531b136e17 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -608,6 +608,13 @@ ImageExpirationPool::ImageExpirationPool() this->freeTimer_->start( std::chrono::duration_cast( IMAGE_POOL_CLEANUP_INTERVAL)); + + // configure all debug counts used by images + DebugCount::configure("image bytes", DebugCount::Flag::DataSize); + DebugCount::configure("image bytes (ever loaded)", + DebugCount::Flag::DataSize); + DebugCount::configure("image bytes (ever unloaded)", + DebugCount::Flag::DataSize); } ImageExpirationPool &ImageExpirationPool::instance() From 2c7bedd620367fd19903fd060fa1d96685a19bbd Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 21:36:07 +0200 Subject: [PATCH 08/10] refactor: add copy button --- src/widgets/helper/DebugPopup.cpp | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/widgets/helper/DebugPopup.cpp b/src/widgets/helper/DebugPopup.cpp index 4eebce0b8c3..543b25f819c 100644 --- a/src/widgets/helper/DebugPopup.cpp +++ b/src/widgets/helper/DebugPopup.cpp @@ -1,29 +1,40 @@ -#include "DebugPopup.hpp" +#include "widgets/helper/DebugPopup.hpp" +#include "common/Literals.hpp" +#include "util/Clipboard.hpp" #include "util/DebugCount.hpp" #include -#include #include +#include #include +#include namespace chatterino { +using namespace literals; + DebugPopup::DebugPopup() { - auto *layout = new QHBoxLayout(this); + auto *layout = new QVBoxLayout(this); auto *text = new QLabel(this); auto *timer = new QTimer(this); + auto *copyButton = new QPushButton(u"&Copy"_s); - timer->setInterval(300); QObject::connect(timer, &QTimer::timeout, [text] { text->setText(DebugCount::getDebugText()); }); - timer->start(); + timer->start(300); + text->setText(DebugCount::getDebugText()); text->setFont(QFontDatabase::systemFont(QFontDatabase::FixedFont)); layout->addWidget(text); + layout->addWidget(copyButton, 1); + + QObject::connect(copyButton, &QPushButton::clicked, this, [text] { + crossPlatformCopy(text->text()); + }); } } // namespace chatterino From 955815aa1a9436c66040cbeaf6ea044f267aa111 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 21:50:54 +0200 Subject: [PATCH 09/10] fix: image usage --- src/messages/Image.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 0531b136e17..3736250708c 100644 --- a/src/messages/Image.cpp +++ b/src/messages/Image.cpp @@ -108,7 +108,7 @@ namespace detail { { auto sz = frame.image.size(); auto area = sz.width() * sz.height(); - auto memory = area * frame.image.depth(); + auto memory = area * frame.image.depth() / 8; usage += memory; } From 5c8265735bb61f0245307ce30468e14737102234 Mon Sep 17 00:00:00 2001 From: Nerixyz Date: Fri, 27 Oct 2023 22:12:27 +0200 Subject: [PATCH 10/10] chore: add changelog entry --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62d908ee670..38ba4b1a455 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -43,6 +43,7 @@ - Dev: Replace `boost::optional` with `std::optional`. (#4877) - Dev: Improve performance by reducing repaints caused by selections. (#4889) - Dev: Removed direct dependency on Qt 5 compatibility module. (#4906) +- Dev: Refactor `DebugCount` and add copy button to debug popup. (#4921) ## 2.4.6