Optimize converting TCP endpoints to strings

There may be quite a few endpoint names (one for each available network card), and they usually remain unchanged throughout the session, while previously producing such names was performed every time they were accessed. Now they are retrieved from the cache.

PR #21770.
This commit is contained in:
Vladimir Golovnev
2024-11-07 09:39:33 +03:00
committed by Vladimir Golovnev (Glassez)
parent 10499dffe9
commit f2b2a2b034

View File

@@ -49,6 +49,7 @@
#include <QtSystemDetection> #include <QtSystemDetection>
#include <QByteArray> #include <QByteArray>
#include <QCache>
#include <QDebug> #include <QDebug>
#include <QPointer> #include <QPointer>
#include <QSet> #include <QSet>
@@ -94,7 +95,15 @@ namespace
QString toString(const lt::tcp::endpoint &ltTCPEndpoint) QString toString(const lt::tcp::endpoint &ltTCPEndpoint)
{ {
return QString::fromStdString((std::stringstream() << ltTCPEndpoint).str()); static QCache<lt::tcp::endpoint, QString> cache;
if (const QString *endpointName = cache.object(ltTCPEndpoint))
return *endpointName;
const std::string tmp = (std::ostringstream() << ltTCPEndpoint).str();
const auto endpointName = QString::fromLatin1(tmp.c_str(), tmp.size());
cache.insert(ltTCPEndpoint, new QString(endpointName));
return endpointName;
} }
template <typename FromLTTimePoint32Func> template <typename FromLTTimePoint32Func>