From f2b2a2b034fbc1990d8135a68969632a1585de49 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 3ed0d53f4..385868126 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