Skip to content

Conversation

@tarasko
Copy link

@tarasko tarasko commented Sep 7, 2024

After profiling my app with perf I identified a couple of things that make SSLProtocol a little bit faster:

  • SSLProtocol.get_buffer and buffer_updated can be called directly from UVStream since it is our own type and we know it's definition
  • Added inline directive to all private methods of SSLProtocol to avoid vtable calls

Before:

--42.75%--__pyx_f_6uvloop_4loop___uv_stream_buffered_on_read
       |          
        --42.19%--__pyx_f_6uvloop_4loop_run_in_context1
                  |          
                   --41.92%--cfunction_vectorcall_FASTCALL_KEYWORDS
                             context_run
                             |          
                              --41.83%--_PyObject_VectorcallTstate.lto_priv.18
                                        |          
                                         --41.73%--method_vectorcall
                                                   |          
                                                    --41.32%--__pyx_pw_6uvloop_4loop_11SSLProtocol_15buffer_updated


--2.51%--__pyx_f_6uvloop_4loop___uv_stream_buffered_alloc
       |          
        --1.40%--__pyx_f_6uvloop_4loop_run_in_context1
                  |          
                   --0.67%--cfunction_vectorcall_FASTCALL_KEYWORDS
                             context_run

After:

--43.18%--__pyx_f_6uvloop_4loop___uv_stream_buffered_on_read
       |          
        --42.57%--__pyx_f_6uvloop_4loop_11SSLProtocol_buffer_updated_impl


--0.79%--__pyx_f_6uvloop_4loop___uv_stream_buffered_alloc
       |          
        --0.60%--PyGILState_Ensure

@tarasko tarasko changed the title Optimizations for SSLProtocol Optimize SSL performance by calling SSLProtocol methods directly, not through the python's vectorcall interface Oct 15, 2024
@tarasko tarasko changed the title Optimize SSL performance by calling SSLProtocol methods directly, not through the python's vectorcall interface Improve SSL performance by calling SSLProtocol methods directly, not through the python's vectorcall interface Oct 15, 2024
Copy link
Member

@fantix fantix left a comment

Choose a reason for hiding this comment

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

Great improvement!


# Fast pass for our own SSLProtocol
# avoid python calls, memoryviews, context enter/exit, etc
if isinstance(sc._protocol, SSLProtocol):
Copy link
Member

Choose a reason for hiding this comment

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

isinstance is cheap, but maybe we can repurpose UVStream._buffered to an integer enum of 3 values:

  • 0 for non-buffered protocol
  • 1 for general buffered protocol
  • 2 for uvloop SSLProtocol

So that we can replace such isinstance calls with even cheaper int check.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants