Don't use std::map and Container to store DC OptionStat.

This commit is contained in:
levlam 2024-03-01 14:01:10 +03:00
parent 0761329c61
commit 29a8e8062b
2 changed files with 19 additions and 17 deletions

View File

@ -23,7 +23,7 @@
namespace td { namespace td {
void DcOptionsSet::add_dc_options(DcOptions dc_options) { void DcOptionsSet::add_dc_options(DcOptions dc_options) {
std::vector<DcOptionId> new_ordered_options; vector<DcOptionId> new_ordered_options;
for (auto &option : dc_options.dc_options) { for (auto &option : dc_options.dc_options) {
auto *info = register_dc_option(std::move(option)); auto *info = register_dc_option(std::move(option));
new_ordered_options.push_back(DcOptionId{info->pos}); new_ordered_options.push_back(DcOptionId{info->pos});
@ -55,8 +55,8 @@ vector<DcOptionsSet::ConnectionInfo> DcOptionsSet::find_all_connections(DcId dc_
bool only_http) { bool only_http) {
LOG(DEBUG) << "Find all " << (allow_media_only ? "media " : "") << "connections in " << dc_id LOG(DEBUG) << "Find all " << (allow_media_only ? "media " : "") << "connections in " << dc_id
<< ". use_static = " << use_static << ", prefer_ipv6 = " << prefer_ipv6 << ", only_http = " << only_http; << ". use_static = " << use_static << ", prefer_ipv6 = " << prefer_ipv6 << ", only_http = " << only_http;
std::vector<ConnectionInfo> options; vector<ConnectionInfo> options;
std::vector<ConnectionInfo> static_options; vector<ConnectionInfo> static_options;
if (prefer_ipv6) { if (prefer_ipv6) {
use_static = false; use_static = false;
@ -188,15 +188,19 @@ DcOptionsSet::DcOptionInfo *DcOptionsSet::register_dc_option(DcOption &&option)
void DcOptionsSet::init_option_stat(DcOptionInfo *option_info) { void DcOptionsSet::init_option_stat(DcOptionInfo *option_info) {
const auto &ip_address = option_info->option.get_ip_address(); const auto &ip_address = option_info->option.get_ip_address();
auto it_ok = option_to_stat_id_.emplace(ip_address, 0); for (size_t i = 0; i < option_stats_.size(); i++) {
if (it_ok.second) { if (option_stats_[i].first == ip_address) {
it_ok.first->second = option_stats_.create(make_unique<OptionStat>()); option_info->stat_id = i;
return;
}
} }
option_info->stat_id = it_ok.first->second; option_stats_.emplace_back(ip_address, make_unique<OptionStat>());
option_info->stat_id = option_stats_.size() - 1;
} }
DcOptionsSet::OptionStat *DcOptionsSet::get_option_stat(const DcOptionInfo *option_info) { DcOptionsSet::OptionStat *DcOptionsSet::get_option_stat(const DcOptionInfo *option_info) {
return option_stats_.get(option_info->stat_id)->get(); CHECK(option_info->stat_id < option_stats_.size());
return option_stats_[option_info->stat_id].second.get();
} }
} // namespace td } // namespace td

View File

@ -10,12 +10,11 @@
#include "td/telegram/net/DcOptions.h" #include "td/telegram/net/DcOptions.h"
#include "td/utils/common.h" #include "td/utils/common.h"
#include "td/utils/Container.h"
#include "td/utils/port/IPAddress.h" #include "td/utils/port/IPAddress.h"
#include "td/utils/Status.h" #include "td/utils/Status.h"
#include "td/utils/Time.h" #include "td/utils/Time.h"
#include <map> #include <utility>
namespace td { namespace td {
@ -79,8 +78,8 @@ class DcOptionsSet {
struct DcOptionInfo { struct DcOptionInfo {
DcOption option; DcOption option;
int64 stat_id = -1; size_t stat_id = static_cast<size_t>(-1);
size_t pos; size_t pos = 0;
size_t order = 0; size_t order = 0;
DcOptionInfo(DcOption &&option, size_t pos) : option(std::move(option)), pos(pos) { DcOptionInfo(DcOption &&option, size_t pos) : option(std::move(option)), pos(pos) {
@ -88,7 +87,7 @@ class DcOptionsSet {
}; };
struct DcOptionId { struct DcOptionId {
size_t pos; size_t pos = 0;
auto as_tie() const { auto as_tie() const {
return pos; return pos;
} }
@ -100,10 +99,9 @@ class DcOptionsSet {
} }
}; };
std::vector<unique_ptr<DcOptionInfo>> options_; vector<unique_ptr<DcOptionInfo>> options_;
std::vector<DcOptionId> ordered_options_; vector<DcOptionId> ordered_options_;
std::map<IPAddress, int64> option_to_stat_id_; vector<std::pair<IPAddress, unique_ptr<OptionStat>>> option_stats_;
Container<unique_ptr<OptionStat>> option_stats_;
DcOptionInfo *register_dc_option(DcOption &&option); DcOptionInfo *register_dc_option(DcOption &&option);
void init_option_stat(DcOptionInfo *option_info); void init_option_stat(DcOptionInfo *option_info);