jchristn/WatsonWebsocket

Allocation-free version of Send/Receive

Closed this issue · 6 comments

joreg commented

hey joel,

would you be up for having allocation-free Send/Receive options on the websockets? what do you think of the following:

for send:

private async Task<bool> MessageWriteAsync(byte[] data, WebSocketMessageType msgType, CancellationToken token)

the internal function is taking an ArraySegment. in our scenario it would come handy if we could just provide that instead of copying to an array first.

for receive:

private async Task<MessageReceivedEventArgs> MessageReadAsync()

same here, the internal function is returning an ArraySegment of which currently two copies are made: first into a MemoryStream, then into the buffer (which is also currently allocated twice: L607 and L614). returning an ArraySegment should allow you to get rid of all of this, right?

same is also applicable to the server.

Hi @joreg for sure! Anything that makes the library more efficient :) Do you have a PR handy by chance?

joreg commented

great! no PR handy, but if you prefer, i can attempt one.

That would be awesome @joreg - much appreciated!

joreg commented

ok, here is the first part, for sending: #91
this should be a non-breaking change.

for the receiving part i'm afraid this would be a minor breaking change, since the MessageReceivedEventArgs will need to have ArraySegment of byte instead of byte[] for data. would you still be accepting this? (i'd PR the same for SuperSimpleTCP then which has the same issue)

Really appreciate you putting this together @joreg - I updated the PR with the NuGet link and the commit. Thanks!

joreg commented

thanks for the quick merge.

and again re the same issue for the receiver. how do you think about a breaking change there as i outlined above?