Dead code in accept
Serguei I. Ivantsov
manowar at gsc-game.kiev.ua
Sun Aug 4 21:15:04 UTC 2013
Hi,
While researching nginx, I found that there are a lot of dead code, mostly
in ngx_event_accept.c
One of those blocks:
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
ev->available = 1;
} else if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
ev->available = ecf->multi_accept;
}
I have EPOLL and do not have neither RTSIG nor KQUEUE on my Linux, but
this conditionals are executed on every accept. This wastes CPU cycles and
cause possible branch miss-predictions.
I see two ways, how to address this.
1. nginx-style - wrap this code blocks with pre-processor #if/#endif the
same way it is done in other parts of nginx code. Resulting code will look
like this:
#if (NGX_HAVE_RTSIG)
if (ngx_event_flags & NGX_USE_RTSIG_EVENT) {
ev->available = 1;
} else
#endif
#if (NGX_HAVE_KQUEUE)
if (!(ngx_event_flags & NGX_USE_KQUEUE_EVENT)) {
#endif
ev->available = ecf->multi_accept;
#if (NGX_HAVE_KQUEUE)
}
#endif
Yep, some kind of spaghetti.
2. A little "hack".
In ngx_event.h we can conditionally define NGX_USE_*_EVENT constants to
zero, if we have no support for specific event module.
Thus, we do not need to touch the code, and optimizing compiler will
remove this code blocks, because condition expression is constant and
available at compile time.
A little test with high volume of simple requests shows 0.5% overall speed
improvement.
I have patches for both, so just need to know which approach is better in
terms of nginx ideology.
More information about the nginx-devel
mailing list