]> git.ipfire.org Git - thirdparty/psycopg.git/commitdiff
perf(c): small optimization tweaks to wait_c
authorDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 11 Dec 2022 19:48:32 +0000 (19:48 +0000)
committerDaniele Varrazzo <daniele.varrazzo@gmail.com>
Sun, 11 Dec 2022 20:04:31 +0000 (20:04 +0000)
psycopg_c/psycopg_c/_psycopg/generators.pyx
psycopg_c/psycopg_c/_psycopg/waiting.pyx

index 29f6fb40e15d919a530269fb5b4dcc5e48baaaa4..9ce9e54f74f44034e50c4b970f9c6c1da5d02d13 100644 (file)
@@ -18,8 +18,12 @@ from psycopg._encodings import conninfo_encoding
 cdef object WAIT_W = Wait.W
 cdef object WAIT_R = Wait.R
 cdef object WAIT_RW = Wait.RW
+cdef object PY_READY_R = Ready.R
+cdef object PY_READY_W = Ready.W
+cdef object PY_READY_RW = Ready.RW
 cdef int READY_R = Ready.R
 cdef int READY_W = Ready.W
+cdef int READY_RW = Ready.RW
 
 def connect(conninfo: str) -> PQGenConn[abc.PGconn]:
     """
index bdecfda72855ac6fa02ecc0596299e25fe3ce1bb..0af6c57c1870feb34d77832e29bbe40294f13641 100644 (file)
@@ -4,6 +4,8 @@ C implementation of waiting functions
 
 # Copyright (C) 2022 The Psycopg Team
 
+from cpython.object cimport PyObject_CallFunctionObjArgs
+
 cdef extern from *:
     """
 #if defined(HAVE_POLL) && !defined(HAVE_BROKEN_POLL)
@@ -152,8 +154,6 @@ finally:
 
 }
     """
-    const int SELECT_EV_READ
-    const int SELECT_EV_WRITE
     cdef int wait_c_impl(int fileno, int wait, float timeout) except -1
 
 
@@ -163,11 +163,16 @@ def wait_c(gen: PQGen[RV], int fileno, timeout = None) -> RV:
     """
     cdef float ctimeout
     cdef int wait, ready
+    cdef PyObject *pyready
 
-    if timeout is None or timeout < 0:
+    if timeout is None:
         ctimeout = -1.0
     else:
         ctimeout = float(timeout)
+        if ctimeout < 0.0:
+            ctimeout = -1.0
+
+    send = gen.send
 
     try:
         wait = next(gen)
@@ -176,8 +181,16 @@ def wait_c(gen: PQGen[RV], int fileno, timeout = None) -> RV:
             ready = wait_c_impl(fileno, wait, ctimeout)
             if ready == 0:
                 continue
-
-            wait = gen.send(ready)
+            elif ready == READY_R:
+                pyready = <PyObject *>PY_READY_R
+            elif ready == READY_RW:
+                pyready = <PyObject *>PY_READY_RW
+            elif ready == READY_W:
+                pyready = <PyObject *>PY_READY_W
+            else:
+                raise AssertionError(f"unexpected ready value: {ready}")
+
+            wait = PyObject_CallFunctionObjArgs(send, pyready, NULL)
 
     except StopIteration as ex:
         rv: RV = ex.args[0] if ex.args else None