From 5334cc8e107589cd79dbb8b992ae2eb150a8ee6f Mon Sep 17 00:00:00 2001 From: levlam Date: Thu, 21 Sep 2023 16:47:12 +0300 Subject: [PATCH] Improve types of vectors in SessionConnection. --- td/mtproto/SessionConnection.cpp | 61 +++++++++++++++++--------------- td/mtproto/SessionConnection.h | 8 ++--- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/td/mtproto/SessionConnection.cpp b/td/mtproto/SessionConnection.cpp index 20ce62be1..97708e96f 100644 --- a/td/mtproto/SessionConnection.cpp +++ b/td/mtproto/SessionConnection.cpp @@ -810,23 +810,23 @@ Result SessionConnection::send_query(BufferSlice buffer, bool gzip_flag, } void SessionConnection::get_state_info(uint64 message_id) { - if (to_get_state_info_.empty()) { + if (to_get_state_info_message_ids_.empty()) { send_before(Time::now_cached()); } - to_get_state_info_.push_back(static_cast(message_id)); + to_get_state_info_message_ids_.push_back(message_id); } void SessionConnection::resend_answer(uint64 message_id) { - if (to_resend_answer_.empty()) { + if (to_resend_answer_message_ids_.empty()) { send_before(Time::now_cached() + RESEND_ANSWER_DELAY); } - to_resend_answer_.push_back(static_cast(message_id)); + to_resend_answer_message_ids_.push_back(message_id); } void SessionConnection::cancel_answer(uint64 message_id) { - if (to_cancel_answer_.empty()) { + if (to_cancel_answer_message_ids_.empty()) { send_before(Time::now_cached() + RESEND_ANSWER_DELAY); } - to_cancel_answer_.push_back(static_cast(message_id)); + to_cancel_answer_message_ids_.push_back(message_id); } void SessionConnection::destroy_key() { @@ -859,23 +859,22 @@ std::pair SessionConnection::encrypted_bind(int64 perm_key, } void SessionConnection::force_ack() { - if (!to_ack_.empty()) { + if (!to_ack_message_ids_.empty()) { send_before(Time::now_cached()); } } void SessionConnection::send_ack(uint64 message_id) { VLOG(mtproto) << "Send ack: [msg_id:" << format::as_hex(message_id) << "]"; - if (to_ack_.empty()) { + if (to_ack_message_ids_.empty()) { send_before(Time::now_cached() + ACK_DELAY); } - auto ack = static_cast(message_id); // an easiest way to eliminate duplicated acknowledgements for gzipped packets - if (to_ack_.empty() || to_ack_.back() != ack) { - to_ack_.push_back(ack); + if (to_ack_message_ids_.empty() || to_ack_message_ids_.back() != message_id) { + to_ack_message_ids_.push_back(message_id); constexpr size_t MAX_UNACKED_PACKETS = 100; - if (to_ack_.size() >= MAX_UNACKED_PACKETS) { + if (to_ack_message_ids_.size() >= MAX_UNACKED_PACKETS) { send_before(Time::now_cached()); } } @@ -947,40 +946,46 @@ void SessionConnection::flush_packet() { bool destroy_auth_key = need_destroy_auth_key_ && !sent_destroy_auth_key_; - if (queries.empty() && to_ack_.empty() && ping_id == 0 && max_delay < 0 && future_salt_n == 0 && - to_resend_answer_.empty() && to_cancel_answer_.empty() && to_get_state_info_.empty() && !destroy_auth_key) { + if (queries.empty() && to_ack_message_ids_.empty() && ping_id == 0 && max_delay < 0 && future_salt_n == 0 && + to_resend_answer_message_ids_.empty() && to_cancel_answer_message_ids_.empty() && + to_get_state_info_message_ids_.empty() && !destroy_auth_key) { force_send_at_ = 0; return; } sent_destroy_auth_key_ |= destroy_auth_key; - VLOG(mtproto) << "Sent packet: " << tag("query_count", queries.size()) << tag("ack_count", to_ack_.size()) + VLOG(mtproto) << "Sent packet: " << tag("query_count", queries.size()) << tag("ack_count", to_ack_message_ids_.size()) << tag("ping", ping_id != 0) << tag("http_wait", max_delay >= 0) - << tag("future_salt", future_salt_n > 0) << tag("get_info", to_get_state_info_.size()) - << tag("resend", to_resend_answer_.size()) << tag("cancel", to_cancel_answer_.size()) - << tag("destroy_key", destroy_auth_key) << tag("auth_key_id", auth_data_->get_auth_key().id()); + << tag("future_salt", future_salt_n > 0) << tag("get_info", to_get_state_info_message_ids_.size()) + << tag("resend", to_resend_answer_message_ids_.size()) + << tag("cancel", to_cancel_answer_message_ids_.size()) << tag("destroy_key", destroy_auth_key) + << tag("auth_key_id", auth_data_->get_auth_key().id()); - auto cut_tail = [](vector &v, size_t size, Slice name) { + auto cut_tail = [](vector &v, size_t size, Slice name) { if (size >= v.size()) { - auto result = std::move(v); + auto result = transform(v, [](uint64 x) { return static_cast(x); }); v.clear(); return result; } LOG(WARNING) << "Too many message identifiers in container " << name << ": " << v.size() << " instead of " << size; - vector result(v.end() - size, v.end()); - v.resize(v.size() - size); + auto new_size = v.size() - size; + vector result(size); + for (size_t i = 0; i < size; i++) { + result[i] = static_cast(v[i + new_size]); + } + v.resize(new_size); return result; }; // no more than 8192 message identifiers per container.. - auto to_resend_answer = cut_tail(to_resend_answer_, 8192, "resend_answer"); + auto to_resend_answer = cut_tail(to_resend_answer_message_ids_, 8192, "resend_answer"); uint64 resend_answer_message_id = 0; CHECK(queries.size() <= 1020); - auto to_cancel_answer = cut_tail(to_cancel_answer_, 1020 - queries.size(), "cancel_answer"); - auto to_get_state_info = cut_tail(to_get_state_info_, 8192, "get_state_info"); + auto to_cancel_answer = cut_tail(to_cancel_answer_message_ids_, 1020 - queries.size(), "cancel_answer"); + auto to_get_state_info = cut_tail(to_get_state_info_message_ids_, 8192, "get_state_info"); uint64 get_state_info_message_id = 0; - auto to_ack = cut_tail(to_ack_, 8192, "ack"); + auto to_ack = cut_tail(to_ack_message_ids_, 8192, "ack"); uint64 ping_message_id = 0; bool use_quick_ack = @@ -1030,8 +1035,8 @@ void SessionConnection::flush_packet() { } } - if (to_send_.empty() && to_ack_.empty() && to_get_state_info_.empty() && to_resend_answer_.empty() && - to_cancel_answer_.empty()) { + if (to_send_.empty() && to_ack_message_ids_.empty() && to_get_state_info_message_ids_.empty() && + to_resend_answer_message_ids_.empty() && to_cancel_answer_message_ids_.empty()) { force_send_at_ = 0; } } diff --git a/td/mtproto/SessionConnection.h b/td/mtproto/SessionConnection.h index 335a5a7ac..02d6b6b07 100644 --- a/td/mtproto/SessionConnection.h +++ b/td/mtproto/SessionConnection.h @@ -161,7 +161,7 @@ class SessionConnection final static constexpr int HTTP_MAX_DELAY = 30; // 0.03s vector to_send_; - vector to_ack_; + vector to_ack_message_ids_; double force_send_at_ = 0; struct ServiceQuery { @@ -169,9 +169,9 @@ class SessionConnection final uint64 container_message_id; vector message_ids; }; - vector to_resend_answer_; - vector to_cancel_answer_; - vector to_get_state_info_; + vector to_resend_answer_message_ids_; + vector to_cancel_answer_message_ids_; + vector to_get_state_info_message_ids_; FlatHashMap service_queries_; // nobody cleans up this map. But it should be really small.