[Python-checkins] cpython: Close #20275: Optimize BaseEventLoop._run_once()

victor.stinner python-checkins at python.org
Mon Jan 20 23:56:57 CET 2014


http://hg.python.org/cpython/rev/8bb2c3ae9402
changeset:   88591:8bb2c3ae9402
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Mon Jan 20 23:56:40 2014 +0100
summary:
  Close #20275: Optimize BaseEventLoop._run_once()

Logger.log() is "slow", logger.isEnabledFor() is faster and the logger is
disabled in most cases. A microbenchmark executing 100,000 dummy tasks is 22%
faster with this change.

files:
  Lib/asyncio/base_events.py |  19 +++++++++++--------
  1 files changed, 11 insertions(+), 8 deletions(-)


diff --git a/Lib/asyncio/base_events.py b/Lib/asyncio/base_events.py
--- a/Lib/asyncio/base_events.py
+++ b/Lib/asyncio/base_events.py
@@ -610,15 +610,18 @@
                 timeout = min(timeout, deadline)
 
         # TODO: Instrumentation only in debug mode?
-        t0 = self.time()
-        event_list = self._selector.select(timeout)
-        t1 = self.time()
-        argstr = '' if timeout is None else ' {:.3f}'.format(timeout)
-        if t1-t0 >= 1:
-            level = logging.INFO
+        if logger.isEnabledFor(logging.INFO):
+            t0 = self.time()
+            event_list = self._selector.select(timeout)
+            t1 = self.time()
+            argstr = '' if timeout is None else ' {:.3f}'.format(timeout)
+            if t1-t0 >= 1:
+                level = logging.INFO
+            else:
+                level = logging.DEBUG
+            logger.log(level, 'poll%s took %.3f seconds', argstr, t1-t0)
         else:
-            level = logging.DEBUG
-        logger.log(level, 'poll%s took %.3f seconds', argstr, t1-t0)
+            event_list = self._selector.select(timeout)
         self._process_events(event_list)
 
         # Handle 'later' callbacks that are ready.

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list