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 GitHub
parent 3da9444688
commit 4527536858
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -49,6 +49,7 @@
#include <QtSystemDetection>
#include <QByteArray>
#include <QCache>
#include <QDebug>
#include <QPointer>
#include <QSet>
@ -94,7 +95,15 @@ namespace
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>