From 7ecbfa0cdb41ce7ae102db59981eacab8fd53968 Mon Sep 17 00:00:00 2001 From: nerix Date: Sat, 28 Oct 2023 21:17:32 +0200 Subject: [PATCH] refactor: debug count and popup (#4921) * Moved implementation of the methods to the `cpp` file. * Added `DebugCount::Flag(s)` and `DebugCount::configure(name, flags)`. * Moved from `QMap` to `std::map` (order is important here). * Used `QStringBuilder` for concatenations. * Used `QLocale` for formatting (adds separators). * Added `DebugCount::Flag::DataSize` for data sizes in bytes (and fixed language to English). * Used `DataSize` for image sizes (maybe this should be moved somewhere else?). * Added copy button to popup. * Fixed Image usage reporting being eight times too large (could be another PR, but honestly it's four characters). --- CHANGELOG.md | 1 + src/messages/Image.cpp | 9 ++- src/util/DebugCount.cpp | 111 +++++++++++++++++++++++++++++- src/util/DebugCount.hpp | 107 +++++----------------------- src/widgets/helper/DebugPopup.cpp | 21 ++++-- 5 files changed, 151 insertions(+), 98 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 62d908ee6..38ba4b1a4 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 diff --git a/src/messages/Image.cpp b/src/messages/Image.cpp index 0b8e8f9e5..373625070 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; } @@ -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() diff --git a/src/util/DebugCount.cpp b/src/util/DebugCount.cpp index 331aa605a..1fe915cf2 100644 --- a/src/util/DebugCount.cpp +++ b/src/util/DebugCount.cpp @@ -1,7 +1,114 @@ -#include "DebugCount.hpp" +#include "util/DebugCount.hpp" + +#include "common/UniqueAccess.hpp" + +#include +#include + +#include + +namespace { + +using namespace chatterino; + +struct Count { + int64_t value = 0; + DebugCount::Flags flags = DebugCount::Flag::None; +}; + +// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables) +UniqueAccess> COUNTS; + +} // namespace namespace chatterino { -UniqueAccess> DebugCount::counts_; +void DebugCount::configure(const QString &name, Flags flags) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->emplace(name, Count{.flags = flags}); + } + else + { + it->second.flags = flags; + } +} + +void DebugCount::set(const QString &name, const int64_t &amount) +{ + auto counts = COUNTS.access(); + + auto it = counts->find(name); + if (it == counts->end()) + { + counts->emplace(name, Count{amount}); + } + else + { + it->second.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->emplace(name, Count{amount}); + } + else + { + it->second.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->emplace(name, Count{-amount}); + } + else + { + it->second.value -= 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(); + + QString text; + for (const auto &[key, count] : *counts) + { + 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; +} } // namespace chatterino diff --git a/src/util/DebugCount.hpp b/src/util/DebugCount.hpp index 35f9fc621..ef2363984 100644 --- a/src/util/DebugCount.hpp +++ b/src/util/DebugCount.hpp @@ -1,111 +1,38 @@ #pragma once -#include "common/UniqueAccess.hpp" +#include "common/FlagsEnum.hpp" -#include #include -#include -#include - namespace chatterino { class DebugCount { public: + enum class Flag : uint16_t { + None = 0, + /// The value is a data size in bytes + DataSize = 1 << 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); 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) - { - 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) - { - 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; - } + DebugCount::decrease(name, 1); } - 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 ""; - } - -private: - static UniqueAccess> counts_; + static QString getDebugText(); }; } // namespace chatterino diff --git a/src/widgets/helper/DebugPopup.cpp b/src/widgets/helper/DebugPopup.cpp index 4eebce0b8..543b25f81 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