Skip to content

Improve performance by using Python C API to enter/exit context - #627

Merged
fantix merged 5 commits into
MagicStack:masterfrom
tarasko:optimize_run_in_context
Jan 28, 2026
Merged

fantix merged 5 commits into
MagicStack:masterfrom
tarasko:optimize_run_in_context

Conversation

@tarasko

@tarasko tarasko commented Sep 8, 2024 •

Copy link
Copy Markdown
Contributor

From perf, callstack before the change:

--54.22%--__pyx_f_6uvloop_4loop___uv_stream_on_read
         |          
          --52.82%--__pyx_f_6uvloop_4loop_run_in_context1
                    |          
                     --51.06%--cfunction_vectorcall_FASTCALL_KEYWORDS
                               context_run
                               |          
                                --50.49%--_PyObject_VectorcallTstate.lto_priv.18
                                          |          
                                           --50.28%--method_vectorcall
                                                     |          
                                                      --49.45%--__pyx_pw_6picows_6picows_10WSProtocol_13data_received


After the change:

--51.71%--__pyx_f_6uvloop_4loop___uv_stream_on_read
         |          
          --50.42%--__pyx_f_6uvloop_4loop_run_in_context1
                    |          
                     --49.13%--__pyx_pw_6picows_6picows_10WSProtocol_13data_received

On a side note, I have removed Py_INCREF/Py_DECREF because cython does it itself when method is called directly.

static CYTHON_INLINE PyObject *__pyx_f_6uvloop_4loop_run_in_context(PyObject *__pyx_v_context, PyObject *__pyx_v_method) {
  PyObject *__pyx_r = NULL;
  __Pyx_RefNannyDeclarations
  int __pyx_t_1;
  PyObject *__pyx_t_2 = NULL;
  PyObject *__pyx_t_3 = NULL;
  PyObject *__pyx_t_4 = NULL;
  unsigned int __pyx_t_5;
  int __pyx_t_6;
  char const *__pyx_t_7;
  PyObject *__pyx_t_8 = NULL;
  PyObject *__pyx_t_9 = NULL;
  PyObject *__pyx_t_10 = NULL;
  PyObject *__pyx_t_11 = NULL;
  PyObject *__pyx_t_12 = NULL;
  PyObject *__pyx_t_13 = NULL;
  int __pyx_t_14;
  int __pyx_lineno = 0;
  const char *__pyx_filename = NULL;
  int __pyx_clineno = 0;
  __Pyx_RefNannySetupContext("run_in_context", 1);

  /* "uvloop/loop.pyx":95
 * 
 * cdef inline run_in_context(context, method):
 *     Context_Enter(context)             # <<<<<<<<<<<<<<
 *     try:
 *         return method()
 */
  __pyx_t_1 = Context_Enter(__pyx_v_context); if (unlikely(__pyx_t_1 == ((int)-1))) __PYX_ERR(0, 95, __pyx_L1_error)

  /* "uvloop/loop.pyx":96
 * cdef inline run_in_context(context, method):
 *     Context_Enter(context)
 *     try:             # <<<<<<<<<<<<<<
 *         return method()
 *     finally:
 */
  /*try:*/ {

    /* "uvloop/loop.pyx":97
 *     Context_Enter(context)
 *     try:
 *         return method()             # <<<<<<<<<<<<<<
 *     finally:
 *         Context_Exit(context)
 */
    __Pyx_XDECREF(__pyx_r);
    __Pyx_INCREF(__pyx_v_method);
    __pyx_t_3 = __pyx_v_method; __pyx_t_4 = NULL;
    __pyx_t_5 = 0;
    #if CYTHON_UNPACK_METHODS
    if (unlikely(PyMethod_Check(__pyx_t_3))) {
      __pyx_t_4 = PyMethod_GET_SELF(__pyx_t_3);
      if (likely(__pyx_t_4)) {
        PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_3);
        __Pyx_INCREF(__pyx_t_4);
        __Pyx_INCREF(function);
        __Pyx_DECREF_SET(__pyx_t_3, function);
        __pyx_t_5 = 1;
      }
    }
    #endif
    {
      PyObject *__pyx_callargs[2] = {__pyx_t_4, NULL};
      __pyx_t_2 = __Pyx_PyObject_FastCall(__pyx_t_3, __pyx_callargs+1-__pyx_t_5, 0+__pyx_t_5);
      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 97, __pyx_L4_error)
      __Pyx_GOTREF(__pyx_t_2);
      __Pyx_DECREF(__pyx_t_3); __pyx_t_3 = 0;
    }
    __pyx_r = __pyx_t_2;
    __pyx_t_2 = 0;
    goto __pyx_L3_return;
  }
  /* "uvloop/loop.pyx":99
 *         return method()
 *     finally:
 *         Context_Exit(context)             # <<<<<<<<<<<<<<
 * 
 * 
 */
  /*finally:*/ {
    __pyx_L4_error:;
    /*exception exit:*/{
      __Pyx_PyThreadState_declare
      __Pyx_PyThreadState_assign
      __pyx_t_8 = 0; __pyx_t_9 = 0; __pyx_t_10 = 0; __pyx_t_11 = 0; __pyx_t_12 = 0; __pyx_t_13 = 0;
      __Pyx_XDECREF(__pyx_t_2); __pyx_t_2 = 0;
      __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0;
      __Pyx_XDECREF(__pyx_t_4); __pyx_t_4 = 0;
      if (PY_MAJOR_VERSION >= 3) __Pyx_ExceptionSwap(&__pyx_t_11, &__pyx_t_12, &__pyx_t_13);
      if ((PY_MAJOR_VERSION < 3) || unlikely(__Pyx_GetException(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10) < 0)) __Pyx_ErrFetch(&__pyx_t_8, &__pyx_t_9, &__pyx_t_10);
      __Pyx_XGOTREF(__pyx_t_8);
      __Pyx_XGOTREF(__pyx_t_9);
      __Pyx_XGOTREF(__pyx_t_10);
      __Pyx_XGOTREF(__pyx_t_11);
      __Pyx_XGOTREF(__pyx_t_12);
      __Pyx_XGOTREF(__pyx_t_13);
      __pyx_t_1 = __pyx_lineno; __pyx_t_6 = __pyx_clineno; __pyx_t_7 = __pyx_filename;
      {
        __pyx_t_14 = Context_Exit(__pyx_v_context); if (unlikely(__pyx_t_14 == ((int)-1))) __PYX_ERR(0, 99, __pyx_L7_error)
      }
      if (PY_MAJOR_VERSION >= 3) {
        __Pyx_XGIVEREF(__pyx_t_11);
        __Pyx_XGIVEREF(__pyx_t_12);
        __Pyx_XGIVEREF(__pyx_t_13);
        __Pyx_ExceptionReset(__pyx_t_11, __pyx_t_12, __pyx_t_13);
      }
      __Pyx_XGIVEREF(__pyx_t_8);
      __Pyx_XGIVEREF(__pyx_t_9);
      __Pyx_XGIVEREF(__pyx_t_10);
      __Pyx_ErrRestore(__pyx_t_8, __pyx_t_9, __pyx_t_10);
      __pyx_t_8 = 0; __pyx_t_9 = 0; __pyx_t_10 = 0; __pyx_t_11 = 0; __pyx_t_12 = 0; __pyx_t_13 = 0;
      __pyx_lineno = __pyx_t_1; __pyx_clineno = __pyx_t_6; __pyx_filename = __pyx_t_7;
      goto __pyx_L1_error;
      __pyx_L7_error:;
      if (PY_MAJOR_VERSION >= 3) {
        __Pyx_XGIVEREF(__pyx_t_11);
        __Pyx_XGIVEREF(__pyx_t_12);
        __Pyx_XGIVEREF(__pyx_t_13);
        __Pyx_ExceptionReset(__pyx_t_11, __pyx_t_12, __pyx_t_13);
      }
      __Pyx_XDECREF(__pyx_t_8); __pyx_t_8 = 0;
      __Pyx_XDECREF(__pyx_t_9); __pyx_t_9 = 0;
      __Pyx_XDECREF(__pyx_t_10); __pyx_t_10 = 0;
      __pyx_t_11 = 0; __pyx_t_12 = 0; __pyx_t_13 = 0;
      goto __pyx_L1_error;
    }
    __pyx_L3_return: {
      __pyx_t_13 = __pyx_r;
      __pyx_r = 0;
      __pyx_t_6 = Context_Exit(__pyx_v_context); if (unlikely(__pyx_t_6 == ((int)-1))) __PYX_ERR(0, 99, __pyx_L1_error)
      __pyx_r = __pyx_t_13;
      __pyx_t_13 = 0;
      goto __pyx_L0;
    }
  }

@tarasko
tarasko force-pushed the optimize_run_in_context branch 2 times, most recently from 0701321 to 1757aa8 Compare September 10, 2024 23:17
@tarasko
tarasko force-pushed the optimize_run_in_context branch 2 times, most recently from 64459a4 to 1721fb9 Compare September 11, 2024 00:26
@tarasko
tarasko force-pushed the optimize_run_in_context branch from 1721fb9 to b5873d5 Compare September 11, 2024 00:45
@tarasko tarasko changed the title Optimize run_in_context functions Oct 15, 2024
@tarasko tarasko changed the title Optimize performance by using Python C API to enter/exit context Oct 15, 2024

@fantix fantix left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On a side note, I have removed Py_INCREF/Py_DECREF because cython does it itself when method is called directly.

Yes, INCREF/DECREF was added in #399 as a temporary fix for Python 3.9 and lower. Now that Python 3.9 is EOL, it is safe to drop this temporary fix.

@fantix
fantix merged commit 837ef22 into MagicStack:master Jan 28, 2026
@tarasko
tarasko deleted the optimize_run_in_context branch January 31, 2026 21:44
@fantix fantix mentioned this pull request Sep 30, 2026
jensbjorgensen pushed a commit to jensbjorgensen/uvloop that referenced this pull request Oct 6, 2026
Changes
=======

* Add Python 3.15 and 3.15t wheel builds and CI coverage (MagicStack#758)
  (by @honglei @fantix in f7c0547)

* Add support for the `eager_start` keyword argument in create_task() (MagicStack#748)
  (by @samypr100 in 3cbb095 for MagicStack#746 MagicStack#718)

* Add thread name prefix to the default thread pool executor (MagicStack#636)
  (by @inikolaev @fantix in 0582f94 for MagicStack#562)

* Add support for special hostname `<broadcast>` (MagicStack#592)
  (by @jpbede in 3060ceb for MagicStack#540)

* Upgrade libuv to v1.52.1 (MagicStack#753)
  (by @fantix in e8efea4 for MagicStack#752)

* Improve performance by using Python C API to enter/exit context (MagicStack#627)
  (by @tarasko in 837ef22)

* Improve performance/latency of Transport.write (MagicStack#619)
  (by @tarasko in 1d9b6e0)

* Replace some SSL vectorcall with direct methods (MagicStack#626)
  (by @tarasko in 6a27cbe)

* Optimize SSL buffered reads using C values (MagicStack#629)
  (by @tarasko in a308f75)

Fixes
=====

* Detach socket on create_connection cancellation to prevent fd double-close (MagicStack#740)
  (by @junjzhang @fantix in dc680eb for MagicStack#645 MagicStack#738)

* Remove `loop._ready_len` in favor of `len(loop._ready)` (MagicStack#721)
  (by @x42005e1f in 5910a18 for MagicStack#720)

* Prefer inspect.iscoroutinefunction (MagicStack#705)
  (by @MatthieuDartiailh in fd65027 for MagicStack#703)

* Fix context tests by explicitly yielding after run_in_executor (MagicStack#743)
  (by @samypr100 in 6cd24cb)

* Fix test_create_connection_open_con_addr with Python 3.13.9+ (MagicStack#713)
  (by @shadchin in b93141a for MagicStack#701)

* Skip flaky test_cancel_post_init on asyncio 3.13+ (MagicStack#714)
  (by @fantix in 3ea5c85 for MagicStack#709)

* Fix flaky test_fs_event (MagicStack#717)
  (by @fantix in 8da4547)

* Use C __atomic builtins for debug counters (MagicStack#719)
  (by @fantix in 836e3b2)

* Update the example to work with Python 3.14 (MagicStack#710)
  (by @Jamie-Chang in b74c2f1)

Build
=====

* Replace pkg_resources with packaging and use Cython 3.1 (MagicStack#742)
  (by @samypr100 in b377b7c for MagicStack#729)

* Remove wheel as a build dependency (MagicStack#696)
  (by @DimitriPapadopoulos in 173e88c)

* Support any number of flags in UVLOOP_OPT_CFLAGS (MagicStack#630)
  (by @mgorny in 963a5f3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants