From 94a38c72a7c8803d0947ac86a425152a8b1895ba Mon Sep 17 00:00:00 2001 From: Martin Sustrik Date: Sat, 21 Apr 2012 12:15:45 +0200 Subject: Initialise fd_set once for signler_t object Optimisation. Up to now new fd_set was initialised in each signaler_wait call. Now the fd_set is initialised once when signaler is created. This is useful espacially on Windows where fd_set is list of pointers rather than bitset and thus can be rather large. Signed-off-by: Martin Sustrik --- src/polling.hpp | 4 ++-- src/signaler.cpp | 39 +++++++++------------------------------ src/signaler.hpp | 25 +++++++++++++++++++++++++ src/upoll.hpp | 4 ++-- 4 files changed, 38 insertions(+), 34 deletions(-) diff --git a/src/polling.hpp b/src/polling.hpp index b8f0540..c0b2423 100644 --- a/src/polling.hpp +++ b/src/polling.hpp @@ -18,8 +18,8 @@ along with this program. If not, see . */ -#ifndef __XS_CORE_HPP_INCLUDED__ -#define __XS_CORE_HPP_INCLUDED__ +#ifndef __XS_POLLING_HPP_INCLUDED__ +#define __XS_POLLING_HPP_INCLUDED__ #include "platform.hpp" diff --git a/src/signaler.cpp b/src/signaler.cpp index 1f754f6..8fff9b4 100644 --- a/src/signaler.cpp +++ b/src/signaler.cpp @@ -18,31 +18,8 @@ along with this program. If not, see . */ -#include "platform.hpp" -#include "polling.hpp" - -// On AIX, poll.h has to be included before xs.h to get consistent -// definition of pollfd structure (AIX uses 'reqevents' and 'retnevents' -// instead of 'events' and 'revents' and defines macros to map from POSIX-y -// names to AIX-specific names). -#if defined XS_USE_SYNC_POLL -#include -#elif defined XS_USE_SYNC_SELECT -#if defined XS_HAVE_WINDOWS -#include "windows.hpp" -#elif defined XS_HAVE_HPUX -#include -#include -#include -#elif defined XS_HAVE_OPENVMS -#include -#include -#else -#include -#endif -#endif - #include "signaler.hpp" +#include "platform.hpp" #include "likely.hpp" #include "stdint.hpp" #include "config.hpp" @@ -287,6 +264,11 @@ int xs::signaler_init (xs::signaler_t *self_) // Set both fds to non-blocking mode. unblock_socket (self_->w); unblock_socket (self_->r); + +#if defined XS_USE_SYNC_SELECT + FD_ZERO (&self_->fds); +#endif + return 0; } @@ -362,21 +344,18 @@ int xs::signaler_wait (xs::signaler_t *self_, int timeout_) return 0; #elif defined XS_USE_SYNC_SELECT - - fd_set fds; - FD_ZERO (&fds); - FD_SET (self_->r, &fds); + FD_SET (self_->r, &self_->fds); struct timeval timeout; if (timeout_ >= 0) { timeout.tv_sec = timeout_ / 1000; timeout.tv_usec = timeout_ % 1000 * 1000; } #ifdef XS_HAVE_WINDOWS - int rc = select (0, &fds, NULL, NULL, + int rc = select (0, &self_->fds, NULL, NULL, timeout_ >= 0 ? &timeout : NULL); wsa_assert (rc != SOCKET_ERROR); #else - int rc = select (self_->r + 1, &fds, NULL, NULL, + int rc = select (self_->r + 1, &self_->fds, NULL, NULL, timeout_ >= 0 ? &timeout : NULL); if (unlikely (rc < 0)) { xs_assert (errno == EINTR); diff --git a/src/signaler.hpp b/src/signaler.hpp index 20c2c36..a47763f 100644 --- a/src/signaler.hpp +++ b/src/signaler.hpp @@ -22,6 +22,28 @@ #define __XS_SIGNALER_HPP_INCLUDED__ #include "fd.hpp" +#include "polling.hpp" + +// On AIX, poll.h has to be included before xs.h to get consistent +// definition of pollfd structure (AIX uses 'reqevents' and 'retnevents' +// instead of 'events' and 'revents' and defines macros to map from POSIX-y +// names to AIX-specific names). +#if defined XS_USE_SYNC_POLL +#include +#elif defined XS_USE_SYNC_SELECT +#if defined XS_HAVE_WINDOWS +#include "windows.hpp" +#elif defined XS_HAVE_HPUX +#include +#include +#include +#elif defined XS_HAVE_OPENVMS +#include +#include +#else +#include +#endif +#endif namespace xs { @@ -34,6 +56,9 @@ namespace xs typedef struct { fd_t w; fd_t r; +#if defined XS_USE_SYNC_SELECT + fd_set fds; +#endif } signaler_t; // Initialise the signaler. diff --git a/src/upoll.hpp b/src/upoll.hpp index d63df5e..d08cf5e 100644 --- a/src/upoll.hpp +++ b/src/upoll.hpp @@ -18,8 +18,8 @@ along with this program. If not, see . */ -#ifndef __XS_POLLING_HPP_INCLUDED__ -#define __XS_POLLING_HPP_INCLUDED__ +#ifndef __XS_UPOLL_HPP_INCLUDED__ +#define __XS_UPOLL_HPP_INCLUDED__ #include "../include/xs.h" -- cgit v1.2.3