You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Implement zero copy writes in SelectorSocketTransport in asyncio #91166
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
assignee=Noneclosed_at=Nonecreated_at=<Date2022-03-14.09:18:41.974>labels= ['3.11', 'expert-asyncio', 'performance']
title='Implement zero copy writes in SelectorSocketTransport in asyncio'updated_at=<Date2022-03-21.23:44:35.077>user='https://github.com/kumaraditya303'
Currently, _SelectorSocketTransport transport creates a copy of the data before sending which in case of large amount of data, can create multiple giga bytes copies of data before sending.
To make it zero copy, python's buffer protocol can be used and use memory views of data to save RAM. The writelines method currently joins all the data before sending whereas it can use socket.sendmsg to make it more memory efficient.
Just for fun I ran the benchmark against uvloop and with my implementation asyncio either meets or beats uvloop by small margin. This is great considering that my PR does not involves C code or any third party libraries.
Activity
kumaraditya303 commentedon Mar 14, 2022
Currently,
_SelectorSocketTransporttransport creates a copy of the data before sending which in case of large amount of data, can create multiple giga bytes copies of data before sending.Script demonstrating current behavior:
Memory profile result:
To make it zero copy, python's buffer protocol can be used and use memory views of data to save RAM. The writelines method currently joins all the data before sending whereas it can use
socket.sendmsgto make it more memory efficient.Links:
writelines -
cpython/Lib/asyncio/transports.py
Line 116 in 2153daf
socket.sendmsg - https://docs.python.org/3/library/socket.html#socket.socket.sendmsg
memory_profiler -
https://pypi.org/project/memory-profiler/
asvetlov commentedon Mar 14, 2022
Known problem, PR is welcome!
I expect the fix is not trivial.
kumaraditya303 commentedon Oct 24, 2022
New benchmark comparing various chunk and packet sizes:
Fallback implementation with
send:Faster Implementation with
sendmsg:Memory usage:
Benchmark hidden because not significant (2): echo 10 -- 1024, echo 100 -- 1024
The new implementation is faster on both
sendandsendmsgacross the board with #31871 and avoids memory copying so "zero copy writes".kumaraditya303 commentedon Dec 23, 2022
Just for fun I ran the benchmark against
uvloopand with my implementationasyncioeither meets or beatsuvloopby small margin. This is great considering that my PR does not involves C code or any third party libraries.gvanrossum commentedon Dec 23, 2022
❤️
GH-91166: Implement zero copy writes for `SelectorSocketTransport` in…
Merged main into regmachine branch (#50)
pythonGH-91166: Implement zero copy writes for `SelectorSocketTranspo…
Implement zerocopy writes for the encrypted protocol
Implement zerocopy writes for the encrypted protocol
Implement zerocopy writes for the encrypted protocol
Implement zerocopy writes for the encrypted protocol (#476)
V4.9.2 (#482)