Skip to content

Commit 837ef22

Browse files
authored
Improve performance by using Python C API to enter/exit context (#627)
* Optimize run_in_context calls * Don't increase refcount in run_in_context since cython does it for us
1 parent 5910a18 commit 837ef22

File tree

1 file changed

+9
-13
lines changed

1 file changed

+9
-13
lines changed

uvloop/loop.pyx

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -92,31 +92,27 @@ cdef inline socket_dec_io_ref(sock):
9292

9393

9494
cdef inline run_in_context(context, method):
95-
# This method is internally used to workaround a reference issue that in
96-
# certain circumstances, inlined context.run() will not hold a reference to
97-
# the given method instance, which - if deallocated - will cause segfault.
98-
# See also: edgedb/edgedb#2222
99-
Py_INCREF(method)
95+
Context_Enter(context)
10096
try:
101-
return context.run(method)
97+
return method()
10298
finally:
103-
Py_DECREF(method)
99+
Context_Exit(context)
104100

105101

106102
cdef inline run_in_context1(context, method, arg):
107-
Py_INCREF(method)
103+
Context_Enter(context)
108104
try:
109-
return context.run(method, arg)
105+
return method(arg)
110106
finally:
111-
Py_DECREF(method)
107+
Context_Exit(context)
112108

113109

114110
cdef inline run_in_context2(context, method, arg1, arg2):
115-
Py_INCREF(method)
111+
Context_Enter(context)
116112
try:
117-
return context.run(method, arg1, arg2)
113+
return method(arg1, arg2)
118114
finally:
119-
Py_DECREF(method)
115+
Context_Exit(context)
120116

121117

122118
# Used for deprecation and removal of `loop.create_datagram_endpoint()`'s

0 commit comments

Comments
 (0)