A datapoint for asgiref performance

I have been working on some perf issues in some code (that, granted, has many problems intrinsic to its design).

I’ve found that async_to_sync and sync_to_async-wrapped calls tend to cost me 3ms-5ms per call. This is obviously super dependent on use cases, but I’m glad to have an idea of the number now. (This number doesn’t include the initial wrap, in general I have been declaring a wrapped function then calling that).

Obviously for a single call it doesn’t matter, but in our case we have calls in pretty hot loops, and it is a bit painful.

and of course something like:

await sync_to_async(getattr)(instance, 'field_name')

to get the field name is a good way to cause pain.

Thanks for the measurement. I agree that is slow and would like to see this optimized, if possible.

Some questions for more investigation, if you want:

Are you using Python 3.12? Could you try 3.13 (nearly at final release)?

Are you using the latest asgiref, and have you checked for performance-related issues in its repo?

Could you try using cProfile to check in-depth what’s happening? Here’s a post with my preferred toolchain that I used for optimizing the system checks.

1 Like