From 8c965718031f0de6401aa30579da2e10afbcaf96 Mon Sep 17 00:00:00 2001 From: Determinant Date: Tue, 31 Jul 2018 20:48:37 -0400 Subject: optimize by replacing RcObj with pointers --- include/salticidae/conn.h | 8 ++++---- include/salticidae/network.h | 22 ++++++++++------------ 2 files changed, 14 insertions(+), 16 deletions(-) (limited to 'include') diff --git a/include/salticidae/conn.h b/include/salticidae/conn.h index f8156ee..4f17665 100644 --- a/include/salticidae/conn.h +++ b/include/salticidae/conn.h @@ -147,7 +147,7 @@ class ConnPool { /** The handle to a bi-directional connection. */ using conn_t = RcObj; /** The type of callback invoked when connection status is changed. */ - using conn_callback_t = std::function; + using conn_callback_t = std::function; /** Abstraction for a bi-directional connection. */ class Conn { @@ -228,15 +228,15 @@ class ConnPool { /** Called when new data is available. */ virtual void on_read() { - if (cpool->read_cb) cpool->read_cb(self()); + if (cpool->read_cb) cpool->read_cb(this); } /** Called when the underlying connection is established. */ virtual void on_setup() { - if (cpool->conn_cb) cpool->conn_cb(self()); + if (cpool->conn_cb) cpool->conn_cb(this); } /** Called when the underlying connection breaks. */ virtual void on_teardown() { - if (cpool->conn_cb) cpool->conn_cb(self()); + if (cpool->conn_cb) cpool->conn_cb(this); } }; diff --git a/include/salticidae/network.h b/include/salticidae/network.h index 8ede187..8128119 100644 --- a/include/salticidae/network.h +++ b/include/salticidae/network.h @@ -125,7 +125,7 @@ class MsgNetwork: public ConnPool { private: std::unordered_map< typename Msg::opcode_t, - std::function> handler_map; + std::function> handler_map; protected: #ifdef SALTICIDAE_MSG_STAT @@ -149,14 +149,14 @@ class MsgNetwork: public ConnPool { typename callback_traits::msg_type, DataStream &&>::value>::type reg_handler(Func handler) { using callback_t = callback_traits; - handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, conn_t conn) { + handler_map[callback_t::msg_type::opcode] = [handler](const Msg &msg, Conn *conn) { handler(typename callback_t::msg_type(msg.get_payload()), - static_pointer_cast(conn)); + static_cast(conn)); }; } template - void send_msg(const MsgType &msg, conn_t conn); + void send_msg(const MsgType &msg, Conn *conn); using ConnPool::listen; #ifdef SALTICIDAE_MSG_STAT msg_stat_by_opcode_t &get_sent_by_opcode() const { @@ -323,8 +323,8 @@ class PeerNetwork: public MsgNetwork { } }; - void msg_ping(MsgPing &&msg, conn_t conn); - void msg_pong(MsgPong &&msg, conn_t conn); + void msg_ping(MsgPing &&msg, const Conn *conn); + void msg_pong(MsgPong &&msg, const Conn *conn); void reset_conn_timeout(conn_t conn); bool check_new_conn(conn_t conn, uint16_t port); void start_active_conn(const NetAddr &paddr); @@ -405,7 +405,7 @@ void MsgNetwork::Conn::on_read() { SALTICIDAE_LOG_DEBUG("got message %s from %s", std::string(msg).c_str(), std::string(*this).c_str()); - it->second(msg, static_pointer_cast(self())); + it->second(msg, this); #ifdef SALTICIDAE_MSG_STAT nrecv++; mn->recv_by_opcode.add(msg); @@ -498,8 +498,7 @@ bool PeerNetwork::check_new_conn(conn_t conn, uint16_t port) { } template -void PeerNetwork::msg_ping(MsgPing &&msg, conn_t conn_) { - auto conn = static_pointer_cast(conn_); +void PeerNetwork::msg_ping(MsgPing &&msg, const Conn *conn) { uint16_t port = msg.port; SALTICIDAE_LOG_INFO("ping from %s, port %u", std::string(*conn).c_str(), ntohs(port)); if (check_new_conn(conn, port)) return; @@ -508,8 +507,7 @@ void PeerNetwork::msg_ping(MsgPing &&msg, conn_t conn_) { } template -void PeerNetwork::msg_pong(MsgPong &&msg, conn_t conn_) { - auto conn = static_pointer_cast(conn_); +void PeerNetwork::msg_pong(MsgPong &&msg, const Conn *conn) { auto it = id2peer.find(conn->peer_id); if (it == id2peer.end()) { @@ -573,7 +571,7 @@ bool PeerNetwork::has_peer(const NetAddr &paddr) const { template template -void MsgNetwork::send_msg(const MsgType &_msg, conn_t conn) { +void MsgNetwork::send_msg(const MsgType &_msg, Conn *conn) { Msg msg(_msg); bytearray_t msg_data = msg.serialize(); SALTICIDAE_LOG_DEBUG("wrote message %s to %s", -- cgit v1.2.3-70-g09d2