From 50497f9cc1749a69497f76ceb318cc1c517a26bf Mon Sep 17 00:00:00 2001 From: taras Date: Sun, 8 Sep 2024 09:00:08 +0200 Subject: [PATCH 1/3] Optimize run_in_context calls --- uvloop/loop.pyx | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index f9a5a239..431a43e0 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -98,7 +98,11 @@ cdef inline run_in_context(context, method): # See also: edgedb/edgedb#2222 Py_INCREF(method) try: - return context.run(method) + Context_Enter(context) + try: + return method() + finally: + Context_Exit(context) finally: Py_DECREF(method) @@ -106,7 +110,11 @@ cdef inline run_in_context(context, method): cdef inline run_in_context1(context, method, arg): Py_INCREF(method) try: - return context.run(method, arg) + Context_Enter(context) + try: + return method(arg) + finally: + Context_Exit(context) finally: Py_DECREF(method) @@ -114,7 +122,11 @@ cdef inline run_in_context1(context, method, arg): cdef inline run_in_context2(context, method, arg1, arg2): Py_INCREF(method) try: - return context.run(method, arg1, arg2) + Context_Enter(context) + try: + return method(arg1, arg2) + finally: + Context_Exit(context) finally: Py_DECREF(method) From 54ac4f59478efd3ddf3050104d0b06ee8d19ef63 Mon Sep 17 00:00:00 2001 From: taras Date: Wed, 11 Sep 2024 00:12:46 +0200 Subject: [PATCH 2/3] Remove unnecessary try-finally blocks --- uvloop/loop.pyx | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index 431a43e0..d7876113 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -96,39 +96,33 @@ cdef inline run_in_context(context, method): # certain circumstances, inlined context.run() will not hold a reference to # the given method instance, which - if deallocated - will cause segfault. # See also: edgedb/edgedb#2222 + Context_Enter(context) Py_INCREF(method) try: - Context_Enter(context) - try: - return method() - finally: - Context_Exit(context) + return method() finally: Py_DECREF(method) + Context_Exit(context) cdef inline run_in_context1(context, method, arg): + Context_Enter(context) Py_INCREF(method) try: - Context_Enter(context) - try: - return method(arg) - finally: - Context_Exit(context) + return method(arg) finally: Py_DECREF(method) + Context_Exit(context) cdef inline run_in_context2(context, method, arg1, arg2): + Context_Enter(context) Py_INCREF(method) try: - Context_Enter(context) - try: - return method(arg1, arg2) - finally: - Context_Exit(context) + return method(arg1, arg2) finally: Py_DECREF(method) + Context_Exit(context) # Used for deprecation and removal of `loop.create_datagram_endpoint()`'s From b5873d5131c633cdeb665e257f734ca3d2ffe1a7 Mon Sep 17 00:00:00 2001 From: taras Date: Wed, 11 Sep 2024 02:03:25 +0200 Subject: [PATCH 3/3] Don't increase refcount in run_in_context since cython does it for us --- uvloop/loop.pyx | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/uvloop/loop.pyx b/uvloop/loop.pyx index d7876113..fc3dbff3 100644 --- a/uvloop/loop.pyx +++ b/uvloop/loop.pyx @@ -92,36 +92,26 @@ cdef inline socket_dec_io_ref(sock): cdef inline run_in_context(context, method): - # This method is internally used to workaround a reference issue that in - # certain circumstances, inlined context.run() will not hold a reference to - # the given method instance, which - if deallocated - will cause segfault. - # See also: edgedb/edgedb#2222 Context_Enter(context) - Py_INCREF(method) try: return method() finally: - Py_DECREF(method) Context_Exit(context) cdef inline run_in_context1(context, method, arg): Context_Enter(context) - Py_INCREF(method) try: return method(arg) finally: - Py_DECREF(method) Context_Exit(context) cdef inline run_in_context2(context, method, arg1, arg2): Context_Enter(context) - Py_INCREF(method) try: return method(arg1, arg2) finally: - Py_DECREF(method) Context_Exit(context)