From a327ab13a56077e007f4f74de99b950b99d66f73 Mon Sep 17 00:00:00 2001 From: Adam Holmberg Date: Fri, 19 Jun 2015 13:12:02 -0500 Subject: [PATCH] micro-optimize no timer case --- cassandra/connection.py | 36 +++++++++++++++--------------------- cassandra/io/libevreactor.py | 7 ++++--- 2 files changed, 19 insertions(+), 24 deletions(-) diff --git a/cassandra/connection.py b/cassandra/connection.py index 95858bd8..20b69c11 100644 --- a/cassandra/connection.py +++ b/cassandra/connection.py @@ -973,20 +973,22 @@ class TimerManager(object): :return: next end time, or None """ queue = self._queue - new_timers = self._new_timers - while new_timers: - heappush(queue, new_timers.pop()) + if self._new_timers: + new_timers = self._new_timers + while new_timers: + heappush(queue, new_timers.pop()) - now = time.time() - while queue: - try: - timer = queue[0][1] - if timer.finish(now): - heappop(queue) - else: - return timer.end - except Exception: - log.exception("Exception while servicing timeout callback: ") + if queue: + now = time.time() + while queue: + try: + timer = queue[0][1] + if timer.finish(now): + heappop(queue) + else: + return timer.end + except Exception: + log.exception("Exception while servicing timeout callback: ") @property def next_timeout(self): @@ -994,11 +996,3 @@ class TimerManager(object): return self._queue[0][0] except IndexError: pass - - @property - def next_offset(self): - try: - next_end = self._queue[0][0] - return next_end - time.time() - except IndexError: - pass diff --git a/cassandra/io/libevreactor.py b/cassandra/io/libevreactor.py index af0c3a60..9114af13 100644 --- a/cassandra/io/libevreactor.py +++ b/cassandra/io/libevreactor.py @@ -19,6 +19,7 @@ import os import socket import ssl from threading import Lock, Thread +import time import weakref from six.moves import range @@ -144,9 +145,9 @@ class LibevLoop(object): def _update_timer(self): if not self._shutdown: - self._timers.service_timeouts() - offset = self._timers.next_offset or 100000 # none pending; will be updated again when something new happens - self._loop_timer.start(offset) + next_end = self._timers.service_timeouts() + if next_end: + self._loop_timer.start(next_end - time.time()) # timer handles negative values else: self._loop_timer.stop()