From 0b32fb36290a937a9013d7ddf4c1e3bc2a017f4a Mon Sep 17 00:00:00 2001 From: Gudmundur Adalsteinsson Date: Mon, 13 Apr 2020 20:30:45 +0000 Subject: [PATCH] Problem: poller item lookup can be simplified Solution: Extract generic find function --- src/socket_poller.cpp | 76 ++++++++++++++++++------------------------- src/socket_poller.hpp | 31 +++++++++++------- 2 files changed, 52 insertions(+), 55 deletions(-) diff --git a/src/socket_poller.cpp b/src/socket_poller.cpp index c761646e..4a41e5fc 100644 --- a/src/socket_poller.cpp +++ b/src/socket_poller.cpp @@ -41,6 +41,18 @@ static bool is_thread_safe (const zmq::socket_base_t &socket_) return socket_.is_thread_safe (); } +// compare elements to value +template +static It find_if2 (It b_, It e_, const T &value, Pred pred) +{ + for (; b_ != e_; ++b_) { + if (pred (*b_, value)) { + break; + } + } + return b_; +} + zmq::socket_poller_t::socket_poller_t () : _tag (0xCAFEBABE), _signaler (NULL) @@ -101,12 +113,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_, void *user_data_, short events_) { - for (items_t::iterator it = _items.begin (), end = _items.end (); it != end; - ++it) { - if (it->socket == socket_) { - errno = EINVAL; - return -1; - } + if (find_if2 (_items.begin (), _items.end (), socket_, &is_socket) + != _items.end ()) { + errno = EINVAL; + return -1; } if (is_thread_safe (*socket_)) { @@ -151,12 +161,10 @@ int zmq::socket_poller_t::add (socket_base_t *socket_, int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_) { - for (items_t::iterator it = _items.begin (), end = _items.end (); it != end; - ++it) { - if (!it->socket && it->fd == fd_) { - errno = EINVAL; - return -1; - } + if (find_if2 (_items.begin (), _items.end (), fd_, &is_fd) + != _items.end ()) { + errno = EINVAL; + return -1; } const item_t item = { @@ -183,15 +191,10 @@ int zmq::socket_poller_t::add_fd (fd_t fd_, void *user_data_, short events_) int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_) { - const items_t::iterator end = _items.end (); - items_t::iterator it; + const items_t::iterator it = + find_if2 (_items.begin (), _items.end (), socket_, &is_socket); - for (it = _items.begin (); it != end; ++it) { - if (it->socket == socket_) - break; - } - - if (it == end) { + if (it == _items.end ()) { errno = EINVAL; return -1; } @@ -205,15 +208,10 @@ int zmq::socket_poller_t::modify (const socket_base_t *socket_, short events_) int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_) { - const items_t::iterator end = _items.end (); - items_t::iterator it; + const items_t::iterator it = + find_if2 (_items.begin (), _items.end (), fd_, &is_fd); - for (it = _items.begin (); it != end; ++it) { - if (!it->socket && it->fd == fd_) - break; - } - - if (it == end) { + if (it == _items.end ()) { errno = EINVAL; return -1; } @@ -227,15 +225,10 @@ int zmq::socket_poller_t::modify_fd (fd_t fd_, short events_) int zmq::socket_poller_t::remove (socket_base_t *socket_) { - const items_t::iterator end = _items.end (); - items_t::iterator it; + const items_t::iterator it = + find_if2 (_items.begin (), _items.end (), socket_, &is_socket); - for (it = _items.begin (); it != end; ++it) { - if (it->socket == socket_) - break; - } - - if (it == end) { + if (it == _items.end ()) { errno = EINVAL; return -1; } @@ -252,15 +245,10 @@ int zmq::socket_poller_t::remove (socket_base_t *socket_) int zmq::socket_poller_t::remove_fd (fd_t fd_) { - const items_t::iterator end = _items.end (); - items_t::iterator it; + const items_t::iterator it = + find_if2 (_items.begin (), _items.end (), fd_, &is_fd); - for (it = _items.begin (); it != end; ++it) { - if (!it->socket && it->fd == fd_) - break; - } - - if (it == end) { + if (it == _items.end ()) { errno = EINVAL; return -1; } diff --git a/src/socket_poller.hpp b/src/socket_poller.hpp index 204c1723..5917e018 100644 --- a/src/socket_poller.hpp +++ b/src/socket_poller.hpp @@ -86,6 +86,17 @@ class socket_poller_t bool check_tag () const; private: + typedef struct item_t + { + socket_base_t *socket; + fd_t fd; + void *user_data; + short events; +#if defined ZMQ_POLL_BASED_ON_POLL + int pollfd_index; +#endif + } item_t; + static void zero_trail_events (zmq::socket_poller_t::event_t *events_, int n_events_, int found_); @@ -103,6 +114,15 @@ class socket_poller_t uint64_t &now_, uint64_t &end_, bool &first_pass_); + static bool is_socket (const item_t &item, const socket_base_t *socket_) + { + return item.socket == socket_; + } + static bool is_fd (const item_t &item, fd_t fd_) + { + return !item.socket && item.fd == fd_; + } + int rebuild (); // Used to check whether the object is a socket_poller. @@ -111,17 +131,6 @@ class socket_poller_t // Signaler used for thread safe sockets polling signaler_t *_signaler; - typedef struct item_t - { - socket_base_t *socket; - fd_t fd; - void *user_data; - short events; -#if defined ZMQ_POLL_BASED_ON_POLL - int pollfd_index; -#endif - } item_t; - // List of sockets typedef std::vector items_t; items_t _items;