perf: reduce memory profile in announce failed handling (#7748)

This commit is contained in:
Yat Ho
2025-10-30 23:11:56 +08:00
committed by GitHub
parent 648c65868c
commit 555d4681c5
3 changed files with 22 additions and 33 deletions

View File

@@ -147,9 +147,7 @@ struct tr_announce_response
* https://www.bittorrent.org/beps/bep_0024.html */ * https://www.bittorrent.org/beps/bep_0024.html */
std::optional<tr_address> external_ip; std::optional<tr_address> external_ip;
static constexpr struct static constexpr int compare_failed(tr_announce_response const& lhs, tr_announce_response const& rhs) noexcept
{
static constexpr int compare(tr_announce_response const& lhs, tr_announce_response const& rhs)
{ {
if (auto val = tr_compare_3way(lhs.did_connect, rhs.did_connect); val != 0) if (auto val = tr_compare_3way(lhs.did_connect, rhs.did_connect); val != 0)
{ {
@@ -164,12 +162,6 @@ struct tr_announce_response
// Non-empty error message most likely means we reached the tracker // Non-empty error message most likely means we reached the tracker
return -tr_compare_3way(std::empty(lhs.errmsg), std::empty(rhs.errmsg)); return -tr_compare_3way(std::empty(lhs.errmsg), std::empty(rhs.errmsg));
} }
constexpr bool operator()(tr_announce_response const& lhs, tr_announce_response const& rhs) const noexcept
{
return compare(lhs, rhs) > 0;
}
} CompareFailed{};
}; };
// --- SCRAPE // --- SCRAPE

View File

@@ -15,7 +15,6 @@
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <utility> #include <utility>
#include <vector>
#include <curl/curl.h> #include <curl/curl.h>
@@ -92,11 +91,10 @@ struct http_announce_data
, on_response{ std::move(on_response_in) } , on_response{ std::move(on_response_in) }
, log_name{ log_name_in } , log_name{ log_name_in }
{ {
failed_responses.reserve(NUM_TR_AF_INET_TYPES);
} }
tr_sha1_digest_t info_hash = {}; tr_sha1_digest_t info_hash = {};
std::vector<tr_announce_response> failed_responses; std::optional<tr_announce_response> failed_response;
tr_announce_response_func on_response; tr_announce_response_func on_response;
@@ -162,16 +160,14 @@ void onAnnounceDone(tr_web::FetchResponse const& web_response)
} }
else else
{ {
data->failed_responses.emplace_back(std::move(response)); if (!data->failed_response || tr_announce_response::compare_failed(*data->failed_response, response) < 0)
{
data->failed_response = std::move(response);
}
if (got_all_responses) if (got_all_responses)
{ {
auto const begin = std::begin(data->failed_responses); data->on_response(*data->failed_response);
std::partial_sort(
begin,
std::next(begin),
std::end(data->failed_responses),
tr_announce_response::CompareFailed);
data->on_response(data->failed_responses.front());
} }
} }
} }

View File

@@ -201,13 +201,14 @@ public:
} }
else if (!succeeded_) else if (!succeeded_)
{ {
failed_responses_.emplace_back(std::move(response)); if (!failed_responses_ || tr_announce_response::compare_failed(*failed_responses_, response) < 0)
{
failed_responses_ = std::move(response);
}
if (got_all_responses) if (got_all_responses)
{ {
auto const begin = std::begin(failed_responses_); on_response_(*failed_responses_);
std::partial_sort(begin, std::next(begin), std::end(failed_responses_), tr_announce_response::CompareFailed);
on_response_(failed_responses_.front());
} }
} }
} }
@@ -215,7 +216,7 @@ public:
private: private:
bool succeeded_ = false; bool succeeded_ = false;
std::vector<tr_announce_response> failed_responses_; std::optional<tr_announce_response> failed_responses_;
tr_announce_response_func on_response_; tr_announce_response_func on_response_;