Skip to content

Add aiofastnet to speedups extra#12822

Draft
tarasko wants to merge 51 commits into
aio-libs:masterfrom
tarasko:feature/speedups_aiofastnet
Draft

Add aiofastnet to speedups extra#12822
tarasko wants to merge 51 commits into
aio-libs:masterfrom
tarasko:feature/speedups_aiofastnet

Conversation

@tarasko

@tarasko tarasko commented Jun 6, 2026

Copy link
Copy Markdown
Contributor

I've made a new PR instead of #12744

What do these changes do?

Discussion #12701

This change adds aiofastnet to speedups extras.
aiofastnet provides more efficient implementations for

  • loop.create_connection
  • loop.start_tls
  • loop.create_server
  • loop.sendfile

aiohttp will use them if aiofastnet can be imported.

On top of that, aiofastnet provides 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.

aiofastnet itself 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.so and libcrypto.so and get addresses of OpenSSL exported functions like SSL_read and SSL_write and SSL_sendfile with dlsym. 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-python on macos and ubuntu
to make sure that all tests pass when aiofastnet is importable.

Related issue number

Checklist

  • I think the code is well written
  • Unit tests for the changes exist
  • Documentation reflects the changes
  • Add yourself to CONTRIBUTORS.txt
  • Add a new news fragment: 12822.feature.rst

@tarasko tarasko marked this pull request as draft June 6, 2026 05:58
Comment thread aiohttp/connector.py Fixed
Comment thread aiohttp/web_fileresponse.py Fixed
Comment thread aiohttp/web_runner.py Fixed
@codecov

codecov Bot commented Jun 6, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.02970% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 98.78%. Comparing base (db5c238) to head (0e8f88f).
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
tests/test_benchmarks_web_fileresponse.py 72.72% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master   #12822      +/-   ##
==========================================
- Coverage   98.95%   98.78%   -0.18%     
==========================================
  Files         131      131              
  Lines       47998    48047      +49     
  Branches     2494     2503       +9     
==========================================
- Hits        47498    47463      -35     
- Misses        376      444      +68     
- Partials      124      140      +16     
Flag Coverage Δ
Autobahn 22.30% <44.55%> (+0.03%) ⬆️
CI-GHA 98.22% <90.09%> (-0.68%) ⬇️
OS-Linux 97.98% <86.13%> (-0.68%) ⬇️
OS-Windows 96.74% <75.24%> (-0.30%) ⬇️
OS-macOS 97.17% <82.17%> (-0.77%) ⬇️
Py-3.10 97.25% <82.17%> (-0.88%) ⬇️
Py-3.11 97.72% <87.12%> (-0.69%) ⬇️
Py-3.12 ?
Py-3.13 97.58% <84.15%> (-0.90%) ⬇️
Py-3.14 96.57% <75.24%> (-1.92%) ⬇️
Py-3.14t 97.54% <84.15%> (-0.04%) ⬇️
Py-pypy-3.11 ?
PythonProvider-uv 98.22% <90.09%> (?)
VM-macos 97.17% <82.17%> (-0.77%) ⬇️
VM-ubuntu 97.98% <86.13%> (-0.68%) ⬇️
VM-windows 96.74% <75.24%> (-0.30%) ⬇️
cython-coverage 38.02% <19.80%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

@tarasko tarasko changed the title Added aiofastnet to speedups extra Add aiofastnet to speedups extra Jun 6, 2026
@codspeed-hq

codspeed-hq Bot commented Jun 6, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 51.36%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 14 improved benchmarks
✅ 69 untouched benchmarks
⏩ 83 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
test_simple_web_file_sendfile_fallback_response[tcp-large] 128.5 ms 39 ms ×3.3
test_simple_web_file_response[ssl-large] 243.8 ms 76.7 ms ×3.2
test_simple_web_file_sendfile_fallback_response[ssl-large] 243 ms 103.7 ms ×2.3
test_one_thousand_round_trip_websocket_binary_messages[ssl-small] 39.7 ms 24.9 ms +59.09%
test_one_thousand_round_trip_websocket_binary_messages[ssl-large] 768.3 ms 485.2 ms +58.36%
test_simple_web_file_response[ssl-small] 117.2 ms 84.3 ms +39.01%
test_one_hundred_get_requests_with_1mb_chunked_payload[ssl] 1,265.9 ms 953.4 ms +32.78%
test_one_thousand_round_trip_websocket_binary_messages[tcp-small] 16.4 ms 13.1 ms +25.34%
test_one_thousand_round_trip_websocket_text_messages 16.8 ms 13.6 ms +23.95%
test_simple_web_file_sendfile_fallback_response[ssl-small] 117.4 ms 101.9 ms +15.23%
test_one_hundred_simple_get_requests[ssl] 46.9 ms 40.9 ms +14.63%
test_simple_web_file_response[tcp-small] 84.9 ms 74.1 ms +14.54%
test_one_hundred_get_requests_with_1024_chunked_payload[ssl] 50.8 ms 44.7 ms +13.73%
test_ten_streamed_responses_iter_any 21.7 ms 20 ms +8.47%

Tip

Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.


Comparing tarasko:feature/speedups_aiofastnet (0e8f88f) with master (db5c238)

Open in CodSpeed

Footnotes

  1. 83 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@tarasko

tarasko commented Jun 6, 2026

Copy link
Copy Markdown
Contributor Author

@codspeedbot explain why this is faster

@tarasko tarasko marked this pull request as ready for review June 6, 2026 18:38
@Dreamsorcerer Dreamsorcerer added the backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot label Jun 8, 2026
Comment thread aiohttp/connector.py Outdated
@tarasko tarasko marked this pull request as draft June 12, 2026 00:55
_T_OnChunkSent = Optional[Callable[[bytes], Awaitable[None]]]


async def sendfile(

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.

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.

Comment thread tests/conftest.py
Comment on lines +113 to +115
# aiofastnet is using sendfile on a non-blocking socket.
# blockbuster triggers anyway. Seems like a false positive
bb.functions["os.sendfile"].deactivate()

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.

I'll let @bdraco check this one.

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.

I'm unclear what all the changes in this file are about.

@Dreamsorcerer

Copy link
Copy Markdown
Member

Looks to me like the PR is almost ready?

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

Labels

backport-3.15 Trigger automatic backporting to the 3.15 release branch by Patchback robot bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants