-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
change incoming_message to bytearray to improve += performance #15
change incoming_message to bytearray to improve += performance #15
Conversation
…eceiving messages
The code that you edited handles both text and binary messages, so it cannot be changed to bytearray as that breaks text. |
Ah yeah that’s a huge oversight on my part, I’ll close the PR when I get a
chance and see if it can be fixed another way.
…On Fri, Jun 24, 2022 at 6:43 PM Miguel Grinberg ***@***.***> wrote:
The code that you edited handles both text and binary messages, so it
cannot be changed to bytearray as that breaks text.
—
Reply to this email directly, view it on GitHub
<#15 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AIKKMJHH6XOIRWWQJWDDBLDVQZBZZANCNFSM5ZZCMYEA>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Made an update to differentiate between text and binary data. I opted to split checking for Would returning |
Update to fix flake8 errors, I separated |
Codecov Report
@@ Coverage Diff @@
## main #15 +/- ##
==========================================
+ Coverage 97.66% 97.83% +0.17%
==========================================
Files 2 2
Lines 171 185 +14
Branches 35 39 +4
==========================================
+ Hits 167 181 +14
Misses 3 3
Partials 1 1
📣 Codecov can now indicate which changes are the most critical in Pull Requests. Learn more |
I'm not sure. I need to find out if |
Data is copied in these conversions, doing the following shows it has a different id, which should mean that it's in a different place in memory.
So after knowing that a copy is taking place I tested using timeit
Would this be expensive for clients/servers that are sending a ton of small packets at a given time? I've been focusing on doing large transfers of data and not paying too much attention to the efficiency of small packets. Just out of curiosity I did a similar test with 1MB of data and saw that it takes about a 1/10th of a millisecond. Doing this type of coversion only once is a very small price compared to the many copy operations being done currently, but that can only be said for large messages at the moment.
So without any further testing would it make sense to generalize that this change would add about 350 nanoseconds per message but it would start to save more time if messages get much larger (really in the MB range is when it is visually noticable on the command line). But when messages do get past 10MB the receiver slows down significantly when it should only take a few tens to hundreds of milliseconds to transfer depending on network speed. I'll probably take some time to test out the time differences with and without the changes to be a little more thorough. It might make sense to only do these conversions if the incoming message has to be reassembled. |
Thanks, this was a useful discussion. I ended up implementing a slightly different solution that switches both text and byte messages to bytearray for faster appending, but only on arrival of the second part. So single part messages, which are the most common, do not suffer any performance penalties. Multipart messages are then converted back to str or bytes once the final part is received. Could you please give the main branch a try, and let me know if this is satisfactory for your use case? |
Sorry for the late reply, just got back from a short vacation. These changes do work for me when using simple-websockets. How can these changes be reflected in the flask-sock package? This was how I was originally turned onto simple-websockets. |
@Mitch-Martinez This fix is now release. Upgrade |
Using += on
bytes
is inefficient becausebytes
is immutable. Makingincoming_message
abytearray
allows += to efficiently reallocate memory forincoming_message
when incoming messages are large.It is not the usual use case for websockets but when a client attempts to send a 50MB file the += operation on a
bytes
object begins to become extremely expensive. After modifying theimcoming_message
object it greatly improved the performance of reading large messages, mainly ones greater than 10MB.