Add aiofastnet to speedups extra#12822
Conversation
for more information, see https://pre-commit.ci
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #12822 +/- ##
==========================================
- Coverage 98.95% 98.94% -0.02%
==========================================
Files 131 131
Lines 48029 48084 +55
Branches 2495 2507 +12
==========================================
+ Hits 47529 47576 +47
- Misses 376 381 +5
- Partials 124 127 +3
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. |
Merging this PR will improve performance by 48.2%
|
for more information, see https://pre-commit.ci
for more information, see https://pre-commit.ci
|
@codspeedbot explain why this is faster |
… tests pass with aiofastnet
| _T_OnChunkSent = Optional[Callable[[bytes], Awaitable[None]]] | ||
|
|
||
|
|
||
| async def sendfile( |
There was a problem hiding this comment.
I think for these ones which are only referenced a single time, it probably makes more sense to do this inline, rather than wrapping the logic into a function.
There was a problem hiding this comment.
This function is convenient for mocking in tests. There is a fixture in test_web_sendfile_functional.py that now mocks it instead of loop.sendfile.
That mock is only needed to simulate NotImplementedError() from sendfile and to run all tests both for native sendfile and _sendfile_fallback.
I preferred to keep changes in tests trivial, but of course it's possible to detect aiofastnet presence and choose what to mock: loop.sendfile or aiofastnet.sendfile directly in the fixture.
Do you want to do it this way?
There was a problem hiding this comment.
As it's only one place in the tests, I'd probably just update the mocks.
| # aiofastnet is using sendfile on a non-blocking socket. | ||
| # blockbuster triggers anyway. Seems like a false positive | ||
| bb.functions["os.sendfile"].deactivate() |
There was a problem hiding this comment.
I'm unclear what all the changes in this file are about.
There was a problem hiding this comment.
These changes solve 2 problems:
- Load kernel
tlsmodule before runningtest_simple_web_file_response[ssl-*]benchmarks.
I also dump OpenSSL version information, this is very helpful for troubleshooting in case OpenSSL refuses to use KTLS.
test_simple_web_file_response[ssl-large] is around 35% faster than test_simple_web_file_sendfile_fallback_response[ssl-large]
thanks to KTLS.
- With uv python, which is used now for all
Test *jobs, aiofastnet is not being used.
Attempt to import aiofastnet with such python results in ImportError and implementation falls back to native loop methods.
I wanted CI to check if all aiohttp tests pass when aiofastnet is usable/importable.
For that, I needed a regular python installed by actions/setup-python@v6
So I added 2 extra Test jobs specifically for ubuntu and macos, where python is installed with actions/setup-python@v6, and not with astral-sh/setup-uv@v8.2.0. Python version doesn't matter, so I chose 3.13.
You can see them in the latest CI run https://github.com/aio-libs/aiohttp/actions/runs/27721497815
Test (3.13, ubuntu, setup-python, false)
Test (3.13, macos, setup-python, false)
and all other jobs looks like this:
Test (3.13, ubuntu, uv, false)
Test (3.13, macos, uv, false)
There is no need to do it for windows, because uv managed python on windows is dynamically linked against openssl, AFAIK aiofastnet always work on windows, with any python.
|
Looks to me like the PR is almost ready? |
Yes, it is. But there is one thing that bothers me. aiofastnet doesn't use stdlib ssl.SSLObject, instead it implements its own aiofastnet.SSLObject. Users can get SSLObject via I'm preparing a full list of what is not available and I'll put it into aiofastnet README. Here is what's available in the latest version: Should cover all legitimate use-cases, but it is still potentially breaking. |
Probably low enough risk. Just add a breaking changenote I guess. |
Renamed CHANGES/12822.feature.rst -> 12822.breaking.rst |
for more information, see https://pre-commit.ci
I've made a new PR instead of #12744
What do these changes do?
Discussion #12701
This change adds
aiofastnettospeedupsextras.aiofastnetprovides more efficient implementations foraiohttpwill use them ifaiofastnetcan be imported.On top of that,
aiofastnetprovides optional, transparent Kernel TLS support on Linux and native sendfile for TLS connections using SSL_sendfile. User can enable it via the standard ssl.OP_ENABLE_KTLS option in SSLContext.Are there changes in behavior for the user?
This is an optimization change, it should not change visible behavior.
Is it a substantial burden for the maintainers to support this?
Every time when users will complain about something that sounds like a networking layer issue, it would be very important to know: is aiofastnet activate or not? So it would be good to add this question to the issue template.
aiofastnetitself is a medium size project, functionally-wise it has a limited scope, and I hope I'll be able to maintain it the next 5 years. It is relatively new, I have written a good amount of tests already and I'm currently adding more. I want to get coverage close to 100%.One important caveat is that aiofastnet currently does NOT work with uv managed python distribution.
Attempt to import aiofastnet will result in ImportError exception with such Python.
aiofastnet uses OpenSSL directly, not through stdlib ssl module. aiofastnet locates dynamically loaded
libssl.soandlibcrypto.soand get addresses of OpenSSL exported functions likeSSL_readandSSL_writeandSSL_sendfilewithdlsym. This lets aiofastnet to cut off a huge portion of python ssl plumbing.uv managed python is linked statically against all of its stdlib modules and OpenSSL, and that makes it impossible to locate OpenSSL symbols.
Currently, all ubuntu and macos tests are run with uv standalone python.
Therefore I added 2 additional test runs with python installed by
actions/setup-pythonon macos and ubuntuto make sure that all tests pass when
aiofastnetis importable.Related issue number
Checklist
CONTRIBUTORS.txt