Fix the hotspot
Valgrind seems to be quite happy with it now.
This commit is contained in:
parent
bdb3b9b959
commit
734931d84f
26
utils.c
26
utils.c
|
@ -1161,22 +1161,29 @@ poller_set (struct poller *self, struct poller_fd *fd)
|
||||||
modifying ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd->fd, &event) != -1);
|
modifying ? EPOLL_CTL_MOD : EPOLL_CTL_ADD, fd->fd, &event) != -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: this is by far the slowest function in the whole program
|
static int
|
||||||
|
poller_compare_fds (const void *ax, const void *bx)
|
||||||
|
{
|
||||||
|
const struct epoll_event *ay = ax, *by = bx;
|
||||||
|
struct poller_fd *a = ay->data.ptr, *b = by->data.ptr;
|
||||||
|
return a->fd - b->fd;
|
||||||
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
poller_remove_from_dispatch (struct poller *self, const struct poller_fd *fd)
|
poller_remove_from_dispatch (struct poller *self, const struct poller_fd *fd)
|
||||||
{
|
{
|
||||||
if (!self->dispatch_total)
|
if (!self->dispatch_total)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
int i;
|
struct epoll_event key = { .data.ptr = (void *) fd }, *fd_event;
|
||||||
for (i = self->dispatch_next; i < self->dispatch_total; i++)
|
if (!(fd_event = bsearch (&key, self->revents,
|
||||||
if (self->revents[i].data.ptr == fd)
|
self->dispatch_total, sizeof *self->revents, poller_compare_fds)))
|
||||||
break;
|
|
||||||
if (i == self->dispatch_total)
|
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (i != --self->dispatch_total)
|
ssize_t i = fd_event - self->revents;
|
||||||
self->revents[i] = self->revents[self->dispatch_total];
|
if (i < self->dispatch_next)
|
||||||
|
self->dispatch_next--;
|
||||||
|
memmove (fd_event, fd_event + 1, (--self->dispatch_total - i) * sizeof key);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -1219,6 +1226,9 @@ poller_run (struct poller *self)
|
||||||
poller_timers_dispatch (&self->timers);
|
poller_timers_dispatch (&self->timers);
|
||||||
poller_idle_dispatch (self->idle);
|
poller_idle_dispatch (self->idle);
|
||||||
|
|
||||||
|
// Sort them by file descriptor number for binary search
|
||||||
|
qsort (self->revents, n_fds, sizeof *self->revents, poller_compare_fds);
|
||||||
|
|
||||||
while (self->dispatch_next < self->dispatch_total)
|
while (self->dispatch_next < self->dispatch_total)
|
||||||
{
|
{
|
||||||
struct epoll_event *revents = self->revents + self->dispatch_next;
|
struct epoll_event *revents = self->revents + self->dispatch_next;
|
||||||
|
|
Loading…
Reference in New Issue