Redundant epoll_ctl with ev_io watcher

Marc Lehmann schmorp at
Fri Nov 6 13:15:12 CET 2015

On Thu, Nov 05, 2015 at 08:57:43PM +0300, Nick Zavaritsky <mejedi at> wrote:
> Unfortunately we can’t get rid of that ev_io_init. In fact we are using C coroutines and we have a function like wait_fd(int). When called, wait_fd initializes a brand new ev_io watcher, starts the watcher and finally switches to another coroutine. Eventually the ev_io handler transfers control back to the original coroutine, resuming wait_fd. Once wait_fd completes, the watcher is no more.

I don't quite see why you couldn't reuse the I/O watcher? I also use
coroutines, and typically it is possible to reuse the watcher (for
performance reasons, it is basically always faster to reuse watchers).

> How wrong would it be to bypass ev_io_set when we know for sure that the descriptor didn’t get reused in the meantime?

It is a complex decision that might differ between event loops, which is why
it's hard to do that from user code, and there is no other interface provided
than the existing one (same watcher).

Since it should be close to trivial for you to reuse existing I/O watchers
(just put them into an array indexed by fd for example, or you could
attach them to your filehandle abstraction if you have one), at this
moment, this sounds like the preferable solution, and might even be faster
than what you do now (what you try at the moment sounds as if you decided
on a suboptimal way to do things and try to force it through).

You could then even try lazy processing, by not stopping the watcher at
all if it didn't change, and just keep it running as long as a coroutine
waits for it: if there is no coroutine waiting for your watcher at each
event, epoll_ctl is unavoidable, and if a coroutine is waiting, then you
completely skip any overheads of manipulating the watcher (not just skip
syscalls). This is what libev does internally itself, btw., which should
already reduce the epoll_ctl calls by half.

> Can we change watcher flags directly provided that it is inactive?

You can do whatever you want as long as you don't complain later, however,
libev doesn't really have a concept of "watcher flags" that is exposed.

                The choice of a       Deliantra, the free code+content MORPG
      -----==-     _GNU_    
      ----==-- _       generation
      ---==---(_)__  __ ____  __      Marc Lehmann
      --==---/ / _ \/ // /\ \/ /      schmorp at
      -=====/_/_//_/\_,_/ /_/\_\

More information about the libev mailing list