[issue18932] Optimize selectors.EpollSelector.modify()

Giampaolo Rodola' report at bugs.python.org
Thu Feb 5 18:46:37 CET 2015


Giampaolo Rodola' added the comment:

I mean something like this (for epoll), as in avoiding to call unregister() / register() at all:


diff -r 017e7391ab58 Lib/selectors.py
--- a/Lib/selectors.py  Wed Feb 04 08:37:02 2015 -0800
+++ b/Lib/selectors.py  Thu Feb 05 18:42:26 2015 +0100
@@ -412,6 +412,23 @@
                 pass
             return key
 
+        def modify(self, fileobj, events, data=None):
+            try:
+                key = self._fd_to_key[self._fileobj_lookup(fileobj)]
+            except KeyError:
+                raise KeyError("{!r} is not registered".format(fileobj)) from None
+            if data != key.data:
+                key = key._replace(data=data)
+                self._fd_to_key[key.fd] = key
+            if events != key.events:
+                epoll_events = 0
+                if events & EVENT_READ:
+                    epoll_events |= select.POLLIN
+                if events & EVENT_WRITE:
+                    epoll_events |= select.POLLOUT
+                self._epoll.modify(key.fd, epoll_events)
+            return key
+
         def select(self, timeout=None):
             if timeout is None:
                 timeout = -1


Before the patch:

~/svn/python/3.5$ ./python -m timeit -s 'import os, selectors; s=selectors.EpollSelector(); r,w=os.pipe(); s.register(r, selectors.EVENT_READ)' 's.modify(r, selectors.EVENT_WRITE); s.modify(r, selectors.EVENT_READ)'
100000 loops, best of 3: 14.7 usec per loop


After the patch:

~/svn/python/3.5$ ./python -m timeit -s 'import os, selectors; s=selectors.EpollSelector(); r,w=os.pipe(); s.register(r, selectors.EVENT_READ)' 's.modify(r, selectors.EVENT_WRITE); s.modify(r, selectors.EVENT_READ)'
100000 loops, best of 3: 3.07 usec per loop

That's equal to about a 4.5x (+450%) speedup if I'm not mistaken.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18932>
_______________________________________


More information about the Python-bugs-list mailing list