Adding logic to allow the client to decide if the websocket frame mask should be used when sending messages. #999
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
After debugging perf issues on lower-end devices like a Pi4 and Pi3, I found that the WebSocket lib was performing a masking operation over the entire buffer sent, as the websocket standard requires. Unfortunately, that masking operation is quite costly; I measured that it doubled the amount of time required to send a ~10kb buffer.
I looked into why the masking is done, and it was added due to the security issue discussed in this paper. The problem is that the paper was published in 2011 and deals with unencrypted web sockets. Since almost all internet traffic WebSockets now use SSL, there have been a few attempts to remove the masking from the protocol since it adds overhead and isn't needed anymore.
I added a simple optional flag to the send call that defaults to masking enabled (as the standard requires), but it allows the client to disable the mask if desired. As I said above, if the client knows the websocket is opened via SSL or it's only being used on a local LAN, the mask is not needed. As I also said, skipping the mask resulted in dropping the send time by half and reducing the CPU percentage required by 10% in my application.
From my testing, most modern web servers will accept client messages without masking, so this seems to be a helpful option for lower-end IOT devices.
If there are any questions, I would love to discuss them!