Open kuzux opened 3 years ago
:white_check_mark: Build Fleck 0.0.61-ci completed (commit https://github.com/statianzo/Fleck/commit/ba7ddb68ff by @kuzux)
:white_check_mark: Build Fleck 0.0.61-ci completed (commit https://github.com/statianzo/Fleck/commit/ba7ddb68ff by @kuzux)
Could you provide context beyond the new code?
Here's the case where that would be useful:
In a codebase using Fleck, I was creating a buffer, filling the buffer in native code and then sending that buffer. Something like
byte[] buf = new byte[MaxSize];
while(some_condition) {
// ...
int bufsize = fillBuffer(buf); // native method called with p/invoke
byte[] sendBuf = new byte[bufsize];
buf.CopyTo(sendBuf, 0);
conn.Send(sendBuf);
// ...
}
So, I want to send just a portion of that buffer (the part filled by the native code). To extract that part as a new byte array, the only "solution" I was able to find (to creating a smaller byte array from a larger one) was allocating a new buffer and copying the data to that (Causes latency spikes when working with buffers of large data).
The way to do that is, I believe, using an ArraySegment<T>
object, which is just a separate "view" to the existing underlying array. So instead of creating a new byte array and copying existing data to it, I can create an ArraySegment object and pass that to the socket
And you’ve found a measurable improvement with this code?
I ask because it still writes to a memory stream that gets written to an array. To avoid allocation of the intermediate array, I believe you’d need to write directly to the socket.
About measurable improvement: Well there was measurable (in fact noticeable) improvement when I eliminated other allocate & copy operations elsewhere in the code. So I decided to go on an allocation-hunting spree, essentially. This was the low-hanging fruit.
I know about the memory stream allocation, but eliminating that as well would probably take way too much modification in the source, which I wasn't comfortable doing. After all, having 2 allocation & copy operations made a significant improvement over having 3. Having 1 would still be better than having 2 of them (Even if it's not as good as having 0 :D)
:white_check_mark: Build Fleck 0.0.62-ci completed (commit https://github.com/statianzo/Fleck/commit/4d644b9f28 by @kuzux)
:white_check_mark: Build Fleck 0.0.62-ci completed (commit https://github.com/statianzo/Fleck/commit/4d644b9f28 by @kuzux)
+1 for this, definitely a needed feature
@kuzux Don't forget to add the Send
method to the IWebSocketConnection
!
If we are populating a very large buffer and want to send that via a websocket; we need that buffer to be of "right" size. To doi that, we need to either create a new
byte[]
for each message, or truncate the large buffer to the message size. If we do the truncration thing for abyte[]
object, what we get is anArraySegment<byte>
object, not a newbyte[]
object. So, it should be possible to pass an ArraySegment instead of just a byte array for binary data.