Skip to content

Commit

Permalink
Minor improvements.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Oct 18, 2021
1 parent 01976be commit 44a186c
Show file tree
Hide file tree
Showing 53 changed files with 106 additions and 107 deletions.
3 changes: 1 addition & 2 deletions benchmark/bench_actor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,7 @@ class RingBench final : public td::Benchmark {
} else {
// TODO: it is three times faster than send_event
// maybe send event could be further optimized?
::td::Scheduler::instance()->hack(static_cast<td::ActorId<Actor>>(next_actor),
td::Event::raw(static_cast<td::uint32>(n - 1)));
next_actor.get_actor_unsafe()->raw_event(td::Event::raw(static_cast<td::uint32>(n - 1)).data);
}
} else if (type == 4) {
send_lambda(next_actor, [n, ptr = next_actor.get_actor_unsafe()] { ptr->pass(n - 1); });
Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/AuthData.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ std::vector<ServerSalt> AuthData::get_future_salts() const {

int64 AuthData::next_message_id(double now) {
double server_time = get_server_time(now);
int64 t = static_cast<int64>(server_time * (1ll << 32));
auto t = static_cast<int64>(server_time * (1ll << 32));

// randomize lower bits for clocks with low precision
// TODO(perf) do not do this for systems with good precision?..
Expand Down
7 changes: 4 additions & 3 deletions td/mtproto/CryptoStorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ class CryptoImpl {
public:
CryptoImpl(const vector<MtprotoQuery> &to_send, Slice header, vector<int64> &&to_ack, int64 ping_id, int ping_timeout,
int max_delay, int max_after, int max_wait, int future_salt_n, vector<int64> get_info,
vector<int64> resend, vector<int64> cancel, bool destroy_key, AuthData *auth_data, uint64 *container_id,
uint64 *get_info_id, uint64 *resend_id, uint64 *ping_message_id, uint64 *parent_message_id)
vector<int64> resend, const vector<int64> &cancel, bool destroy_key, AuthData *auth_data,
uint64 *container_id, uint64 *get_info_id, uint64 *resend_id, uint64 *ping_message_id,
uint64 *parent_message_id)
: query_storer_(to_send, header)
, ack_empty_(to_ack.empty())
, ack_storer_(!ack_empty_, mtproto_api::msgs_ack(std::move(to_ack)), auth_data)
Expand All @@ -206,7 +207,7 @@ class CryptoImpl {
, resend_storer_(resend_not_empty_, mtproto_api::msg_resend_req(std::move(resend)), auth_data, true)
, cancel_not_empty_(!cancel.empty())
, cancel_cnt_(static_cast<int32>(cancel.size()))
, cancel_storer_(cancel_not_empty_, std::move(cancel), auth_data, true)
, cancel_storer_(cancel_not_empty_, cancel, auth_data, true)
, destroy_key_storer_(destroy_key, mtproto_api::destroy_auth_key(), auth_data, true)
, tmp_storer_(query_storer_, ack_storer_)
, tmp2_storer_(tmp_storer_, http_wait_storer_)
Expand Down
5 changes: 3 additions & 2 deletions td/mtproto/Handshake.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRs
}
auto rsa_key = r_rsa_key.move_as_ok();

string p, q;
string p;
string q;
if (pq_factorize(res_pq->pq_, &p, &q) == -1) {
return Status::Error("Failed to factorize");
}
Expand Down Expand Up @@ -129,7 +130,7 @@ Status AuthKeyHandshake::on_res_pq(Slice message, Callback *connection, PublicRs
string aes_key(32, '\0');
Random::secure_bytes(MutableSlice(aes_key));

string data_with_hash = data + sha256(aes_key + data);
string data_with_hash = PSTRING() << data << sha256(aes_key + data);
std::reverse(data_with_hash.begin(), data_with_hash.begin() + data.size());

string decrypted_data(256, '\0');
Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/Handshake.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class AuthKeyHandshake {
static string store_object(const mtproto_api::Object &object);

void send(Callback *connection, const Storer &storer);
void do_send(Callback *connection, const Storer &storer);
static void do_send(Callback *connection, const Storer &storer);

Status on_start(Callback *connection) TD_WARN_UNUSED_RESULT;
Status on_res_pq(Slice message, Callback *connection, PublicRsaKeyInterface *public_rsa_key) TD_WARN_UNUSED_RESULT;
Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/HandshakeConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class HandshakeConnection final
}

Status on_raw_packet(const PacketInfo &packet_info, BufferSlice packet) final {
if (packet_info.no_crypto_flag == false) {
if (!packet_info.no_crypto_flag) {
return Status::Error("Expected not encrypted packet");
}

Expand Down
4 changes: 2 additions & 2 deletions td/mtproto/Ping.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
namespace td {
namespace mtproto {

ActorOwn<> create_ping_actor(string debug, unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
ActorOwn<> create_ping_actor(Slice actor_name, unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
Promise<unique_ptr<RawConnection>> promise, ActorShared<> parent) {
class PingActor final : public Actor {
public:
Expand Down Expand Up @@ -99,7 +99,7 @@ ActorOwn<> create_ping_actor(string debug, unique_ptr<RawConnection> raw_connect
}
}
};
return ActorOwn<>(create_actor<PingActor>(PSLICE() << "PingActor<" << debug << ">", std::move(raw_connection),
return ActorOwn<>(create_actor<PingActor>(PSLICE() << "PingActor<" << actor_name << ">", std::move(raw_connection),
std::move(auth_data), std::move(promise), std::move(parent)));
}

Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/Ping.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
namespace td {
namespace mtproto {

ActorOwn<> create_ping_actor(string debug, unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
ActorOwn<> create_ping_actor(Slice actor_name, unique_ptr<RawConnection> raw_connection, unique_ptr<AuthData> auth_data,
Promise<unique_ptr<RawConnection>> promise, ActorShared<> parent);

} // namespace mtproto
Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/RawConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class RawConnectionDefault final : public RawConnection {
public:
RawConnectionDefault(SocketFd socket_fd, TransportType transport_type, unique_ptr<StatsCallback> stats_callback)
: socket_fd_(std::move(socket_fd))
, transport_(create_transport(transport_type))
, transport_(create_transport(std::move(transport_type)))
, stats_callback_(std::move(stats_callback)) {
transport_->init(&socket_fd_.input_buffer(), &socket_fd_.output_buffer());
}
Expand Down
2 changes: 1 addition & 1 deletion td/mtproto/RawConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class RawConnection {
virtual bool can_send() const = 0;
virtual TransportType get_transport_type() const = 0;
virtual void send_crypto(const Storer &storer, int64 session_id, int64 salt, const AuthKey &auth_key,
uint64 quick_ack_token = 0) = 0;
uint64 quick_ack_token) = 0;
virtual uint64 send_no_crypto(const Storer &storer) = 0;

virtual PollableFdInfo &get_poll_info() = 0;
Expand Down
3 changes: 2 additions & 1 deletion td/mtproto/SessionConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,8 @@ void SessionConnection::flush_packet() {
}
}

size_t send_till = 0, send_size = 0;
size_t send_till = 0;
size_t send_size = 0;
// send at most 1020 queries, of total size 2^15
// don't send anything if have no salt
if (has_salt) {
Expand Down
3 changes: 2 additions & 1 deletion td/mtproto/SessionConnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ class SessionConnection final
};
}

Status parse_message(TlParser &parser, MsgInfo *info, Slice *packet, bool crypto_flag = true) TD_WARN_UNUSED_RESULT;
static Status parse_message(TlParser &parser, MsgInfo *info, Slice *packet,
bool crypto_flag = true) TD_WARN_UNUSED_RESULT;
Status parse_packet(TlParser &parser) TD_WARN_UNUSED_RESULT;
Status on_packet_container(const MsgInfo &info, Slice packet) TD_WARN_UNUSED_RESULT;
Status on_packet_rpc_result(const MsgInfo &info, Slice packet) TD_WARN_UNUSED_RESULT;
Expand Down
4 changes: 2 additions & 2 deletions td/mtproto/TcpTransport.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ void ObfuscatedTransport::do_write_tls(BufferBuilder &&builder) {
do_write(builder.extract());
}

void ObfuscatedTransport::do_write(BufferSlice &&slice) {
output_->append(std::move(slice));
void ObfuscatedTransport::do_write(BufferSlice &&message) {
output_->append(std::move(message));
}

} // namespace tcp
Expand Down
5 changes: 3 additions & 2 deletions td/mtproto/TcpTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ class OldTransport final : public IStreamTransport {

class ObfuscatedTransport final : public IStreamTransport {
public:
ObfuscatedTransport(int16 dc_id, const ProxySecret &secret)
: dc_id_(dc_id), secret_(secret), impl_(secret_.use_random_padding()) {
ObfuscatedTransport(int16 dc_id, ProxySecret secret)
: dc_id_(dc_id), secret_(std::move(secret)), impl_(secret_.use_random_padding()) {
}

Result<size_t> read_next(BufferSlice *message, uint32 *quick_ack) final TD_WARN_UNUSED_RESULT;
Expand Down Expand Up @@ -172,6 +172,7 @@ class ObfuscatedTransport final : public IStreamTransport {
TransportType get_type() const final {
return TransportType{TransportType::ObfuscatedTcp, dc_id_, secret_};
}

bool use_random_padding() const final {
return secret_.use_random_padding();
}
Expand Down
2 changes: 1 addition & 1 deletion td/telegram/net/ConnectionCreator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ void ConnectionCreator::ping_proxy_socket_fd(IPAddress ip_address, SocketFd sock
auto raw_connection =
mtproto::RawConnection::create(ip_address, std::move(socket_fd), std::move(transport_type), nullptr);
children_[token] = {
false, create_ping_actor(std::move(debug_str), std::move(raw_connection), nullptr,
false, create_ping_actor(debug_str, std::move(raw_connection), nullptr,
PromiseCreator::lambda([promise = std::move(promise)](
Result<unique_ptr<mtproto::RawConnection>> result) mutable {
if (result.is_error()) {
Expand Down
24 changes: 9 additions & 15 deletions tdactor/td/actor/PromiseFuture.h
Original file line number Diff line number Diff line change
Expand Up @@ -184,35 +184,29 @@ class SafePromise;
template <class T = Unit>
class Promise;

constexpr std::false_type is_promise_interface(...) {
return {};
}
constexpr std::false_type is_promise_interface(...);

template <class T>
constexpr std::true_type is_promise_interface(const PromiseInterface<T> &promise) {
return {};
}
constexpr std::true_type is_promise_interface(const PromiseInterface<T> &promise);

template <class T>
constexpr std::true_type is_promise_interface(const Promise<T> &promise) {
return {};
}
constexpr std::true_type is_promise_interface(const Promise<T> &promise);

template <class F>
constexpr bool is_promise_interface() {
return decltype(is_promise_interface(std::declval<F>()))::value;
}

constexpr std::false_type is_promise_interface_ptr(...) {
return {};
}
constexpr std::false_type is_promise_interface_ptr(...);

template <class T>
constexpr std::true_type is_promise_interface_ptr(const unique_ptr<T> &promise) {
return {};
}
constexpr std::true_type is_promise_interface_ptr(const unique_ptr<T> &promise);

template <class F>
constexpr bool is_promise_interface_ptr() {
return decltype(is_promise_interface_ptr(std::declval<F>()))::value;
}

template <class T = void, class F = void, std::enable_if_t<std::is_same<T, void>::value, bool> has_t = false>
auto lambda_promise(F &&f) {
return detail::LambdaPromise<detail::drop_result_t<detail::get_arg_t<std::decay_t<F>>>, std::decay_t<F>>(
Expand Down
2 changes: 1 addition & 1 deletion tdactor/td/actor/impl/Actor.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ inline void Actor::do_stop() {
CHECK(empty());
}
inline bool Actor::has_timeout() const {
return Scheduler::instance()->has_actor_timeout(this);
return get_info()->get_heap_node()->in_heap();
}
inline double Actor::get_timeout() const {
return Scheduler::instance()->get_actor_timeout(this);
Expand Down
2 changes: 1 addition & 1 deletion tdactor/td/actor/impl/EventFull-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class EventFull {
data_.link_token = actor_ref.token();
}
template <class T>
EventFull(ActorId<T> actor_id, Event &&data) : actor_id_(actor_id), data_(std::move(data)) {
EventFull(ActorId<T> actor_id, Event &&data) : actor_id_(std::move(actor_id)), data_(std::move(data)) {
}

ActorId<> actor_id_;
Expand Down
5 changes: 0 additions & 5 deletions tdactor/td/actor/impl/Scheduler-decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ class Scheduler {
template <ActorSendType send_type>
void send(ActorRef actor_ref, Event &&event);

void hack(const ActorId<> &actor_id, Event &&event) {
actor_id.get_actor_unsafe()->raw_event(event.data);
}
void before_tail_send(const ActorId<> &actor_id);

static void subscribe(PollableFd fd, PollFlags flags = PollFlags::ReadWrite());
Expand All @@ -125,7 +122,6 @@ class Scheduler {
void start_migrate_actor(Actor *actor, int32 dest_sched_id);
void finish_migrate_actor(Actor *actor);

bool has_actor_timeout(const Actor *actor) const;
double get_actor_timeout(const Actor *actor) const;
void set_actor_timeout_in(Actor *actor, double timeout);
void set_actor_timeout_at(Actor *actor, double timeout_at);
Expand Down Expand Up @@ -176,7 +172,6 @@ class Scheduler {
void do_migrate_actor(ActorInfo *actor_info, int32 dest_sched_id);
void start_migrate_actor(ActorInfo *actor_info, int32 dest_sched_id);

bool has_actor_timeout(const ActorInfo *actor_info) const;
double get_actor_timeout(const ActorInfo *actor_info) const;
void set_actor_timeout_in(ActorInfo *actor_info, double timeout);
void set_actor_timeout_at(ActorInfo *actor_info, double timeout_at);
Expand Down
8 changes: 0 additions & 8 deletions tdactor/td/actor/impl/Scheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,9 +299,6 @@ inline void Scheduler::finish_migrate_actor(Actor *actor) {
register_migrated_actor(actor->get_info());
}

inline bool Scheduler::has_actor_timeout(const Actor *actor) const {
return has_actor_timeout(actor->get_info());
}
inline double Scheduler::get_actor_timeout(const Actor *actor) const {
return get_actor_timeout(actor->get_info());
}
Expand All @@ -315,11 +312,6 @@ inline void Scheduler::cancel_actor_timeout(Actor *actor) {
cancel_actor_timeout(actor->get_info());
}

inline bool Scheduler::has_actor_timeout(const ActorInfo *actor_info) const {
const HeapNode *heap_node = actor_info->get_heap_node();
return heap_node->in_heap();
}

inline void Scheduler::cancel_actor_timeout(ActorInfo *actor_info) {
HeapNode *heap_node = actor_info->get_heap_node();
if (heap_node->in_heap()) {
Expand Down
6 changes: 1 addition & 5 deletions tddb/td/db/SqliteKeyValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@ class SqliteKeyValue {

Status init_with_connection(SqliteDb connection, string table_name) TD_WARN_UNUSED_RESULT;

Result<bool> try_regenerate_index() TD_WARN_UNUSED_RESULT {
return false;
}

void close() {
*this = SqliteKeyValue();
}
Expand Down Expand Up @@ -118,7 +114,7 @@ class SqliteKeyValue {
SqliteStatement get_by_prefix_stmt_;
SqliteStatement get_by_prefix_rare_stmt_;

string next_prefix(Slice prefix);
static string next_prefix(Slice prefix);
};

} // namespace td
3 changes: 3 additions & 0 deletions tddb/td/db/SqliteKeyValueAsync.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface {
public:
explicit Impl(std::shared_ptr<SqliteKeyValueSafe> kv_safe) : kv_safe_(std::move(kv_safe)) {
}

void set(string key, string value, Promise<> promise) {
auto it = buffer_.find(key);
if (it != buffer_.end()) {
Expand All @@ -57,6 +58,7 @@ class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface {
cnt_++;
do_flush(false /*force*/);
}

void erase(string key, Promise<> promise) {
auto it = buffer_.find(key);
if (it != buffer_.end()) {
Expand All @@ -70,6 +72,7 @@ class SqliteKeyValueAsync final : public SqliteKeyValueAsyncInterface {
cnt_++;
do_flush(false /*force*/);
}

void erase_by_prefix(string key_prefix, Promise<> promise) {
do_flush(true /*force*/);
kv_->erase_by_prefix(key_prefix);
Expand Down
2 changes: 1 addition & 1 deletion tddb/td/db/TQueue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ bool EventId::operator<(const EventId &other) const {
return id_ < other.id_;
}

StringBuilder &operator<<(StringBuilder &string_builder, const EventId id) {
StringBuilder &operator<<(StringBuilder &string_builder, EventId id) {
return string_builder << "EventId{" << id.value() << "}";
}

Expand Down
6 changes: 3 additions & 3 deletions tddb/td/db/TQueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class TQueue {
virtual void close(Promise<> promise) = 0;
};

StringBuilder &operator<<(StringBuilder &string_builder, const TQueue::EventId id);
StringBuilder &operator<<(StringBuilder &string_builder, TQueue::EventId id);

struct BinlogEvent;

Expand All @@ -131,7 +131,7 @@ class TQueueBinlog final : public TQueue::StorageCallback {
void set_binlog(std::shared_ptr<BinlogT> binlog) {
binlog_ = std::move(binlog);
}
virtual void close(Promise<> promise) final;
void close(Promise<> promise) final;

private:
std::shared_ptr<BinlogT> binlog_;
Expand All @@ -143,7 +143,7 @@ class TQueueMemoryStorage final : public TQueue::StorageCallback {
uint64 push(QueueId queue_id, const RawEvent &event) final;
void pop(uint64 log_event_id) final;
void replay(TQueue &q) const;
virtual void close(Promise<> promise) final;
void close(Promise<> promise) final;

private:
uint64 next_log_event_id_{1};
Expand Down
Loading

0 comments on commit 44a186c

Please sign in to comment.