Daniele Varrazzo avatar Daniele Varrazzo committed 32e4fa1

Using LOAD_FAST instead of LOAD_GLOBAL in the innermost loop.

Comments (0)

Files changed (2)

eventlet/psyco_eventlet.py

 # THE SOFTWARE.
 
 import psycopg2
-from psycopg2 import extensions
+import psycopg2.extensions
 
 from eventlet.hubs import trampoline
 
 def make_psycopg_green():
     """Configure Psycopg to be used with eventlet in non-blocking way."""
-    if not hasattr(extensions, 'set_wait_callback'):
+    if not hasattr(psycopg2.extensions, 'set_wait_callback'):
         raise ImportError(
             "support for coroutines not available in this Psycopg version (%s)"
             % psycopg2.__version__)
 
-    extensions.set_wait_callback(eventlet_wait_callback)
+    psycopg2.extensions.set_wait_callback(eventlet_wait_callback)
 
-def eventlet_wait_callback(conn, timeout=-1):
+def eventlet_wait_callback(conn, timeout=-1,
+        # access these objects with LOAD_FAST instead of LOAD_GLOBAL lookup
+        POLL_OK = psycopg2.extensions.POLL_OK,
+        POLL_READ = psycopg2.extensions.POLL_READ,
+        POLL_WRITE = psycopg2.extensions.POLL_WRITE,
+        trampoline = trampoline):
     """A wait callback useful to allow eventlet to work with Psycopg."""
     while 1:
         state = conn.poll()
-        if state == extensions.POLL_OK:
+        if state == POLL_OK:
             break
-        elif state == extensions.POLL_READ:
+        elif state == POLL_READ:
             trampoline(conn.fileno(), read=True)
-        elif state == extensions.POLL_WRITE:
+        elif state == POLL_WRITE:
             trampoline(conn.fileno(), write=True)
         else:
             raise psycopg2.OperationalError(

gevent/psyco_gevent.py

 # THE SOFTWARE.
 
 import psycopg2
-from psycopg2 import extensions
+import psycopg2.extensions
 
-from gevent.socket import wait_read, wait_write
+import gevent.socket
 
 def make_psycopg_green():
     """Configure Psycopg to be used with gevent in non-blocking way."""
-    if not hasattr(extensions, 'set_wait_callback'):
+    if not hasattr(psycopg2.extensions, 'set_wait_callback'):
         raise ImportError(
             "support for coroutines not available in this Psycopg version (%s)"
             % psycopg2.__version__)
 
-    extensions.set_wait_callback(gevent_wait_callback)
+    psycopg2.extensions.set_wait_callback(gevent_wait_callback)
 
-def gevent_wait_callback(conn, timeout=-1):
+def gevent_wait_callback(conn, timeout=-1,
+        # access these objects with LOAD_FAST instead of LOAD_GLOBAL lookup
+        POLL_OK = psycopg2.extensions.POLL_OK,
+        POLL_READ = psycopg2.extensions.POLL_READ,
+        POLL_WRITE = psycopg2.extensions.POLL_WRITE,
+        wait_read = gevent.socket.wait_read,
+        wait_write = gevent.socket.wait_write):
     """A wait callback useful to allow gevent to work with Psycopg."""
     while 1:
         state = conn.poll()
-        if state == extensions.POLL_OK:
+        if state == POLL_OK:
             break
-        elif state == extensions.POLL_READ:
+        elif state == POLL_READ:
             wait_read(conn.fileno(), timeout=timeout)
-        elif state == extensions.POLL_WRITE:
+        elif state == POLL_WRITE:
             wait_write(conn.fileno(), timeout=timeout)
         else:
             raise psycopg2.OperationalError(
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.