From 17d96d35b549e5a4910e1e1abfa9c61577e2d876 Mon Sep 17 00:00:00 2001 From: Paul Sokolovsky Date: Mon, 7 Dec 2015 01:13:00 +0200 Subject: [PATCH] uasyncio: Optimize reader/writer callbacks with no arguments. Avoids allocating tuples. --- uasyncio/uasyncio/__init__.py | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/uasyncio/uasyncio/__init__.py b/uasyncio/uasyncio/__init__.py index 0c3367fa..1b9536b4 100644 --- a/uasyncio/uasyncio/__init__.py +++ b/uasyncio/uasyncio/__init__.py @@ -13,7 +13,10 @@ class EpollEventLoop(EventLoop): def add_reader(self, fd, cb, *args): if __debug__: log.debug("add_reader%s", (fd, cb, args)) - self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, (cb, args)) + if args: + self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, (cb, args)) + else: + self.poller.register(fd, select.EPOLLIN | select.EPOLLONESHOT, cb) def remove_reader(self, fd): if __debug__: @@ -23,7 +26,10 @@ class EpollEventLoop(EventLoop): def add_writer(self, fd, cb, *args): if __debug__: log.debug("add_writer%s", (fd, cb, args)) - self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, (cb, args)) + if args: + self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, (cb, args)) + else: + self.poller.register(fd, select.EPOLLOUT | select.EPOLLONESHOT, cb) def remove_writer(self, fd): if __debug__: @@ -48,8 +54,11 @@ class EpollEventLoop(EventLoop): #log.debug("epoll result: %s", res) for cb, ev in res: if __debug__: - log.debug("Calling IO callback: %s%s", cb[0], cb[1]) - cb[0](*cb[1]) + log.debug("Calling IO callback: %r", cb) + if isinstance(cb, tuple): + cb[0](*cb[1]) + else: + cb() class StreamReader: