From 45275368588e5b87838f01633f908a41fc7eb7fd Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Thu, 7 Nov 2024 09:39:33 +0300 Subject: [PATCH] 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. --- src/base/bittorrent/torrentimpl.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 4b230e9fa..5ebc5919c 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -49,6 +49,7 @@ #include #include +#include #include #include #include @@ -94,7 +95,15 @@ namespace QString toString(const lt::tcp::endpoint <TCPEndpoint) { - return QString::fromStdString((std::stringstream() << ltTCPEndpoint).str()); + static QCache 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