WebSocketListener icon indicating copy to clipboard operation
WebSocketListener copied to clipboard

[Optimizations] Better way to avoid allocations?

Open sebas77 opened this issue 1 year ago • 2 comments

I am not sure yet that what I am doing here is what the lib expects me to do, but if so, it seems a waste allocation wise (games, unity)

 while (_textSenderQueue.TryDequeue(out var buffer))
                {
                    var writer = _socket.CreateMessageWriter(vtortola.WebSockets.WebSocketMessageType.Text);

                    await writer.WriteAsync(buffer, _cancellationToken);
                    await writer.CloseAsync();
                }

any advise?

Also this code works fine on pc, but on mobile there is a big lag not sure why

sebas77 avatar May 17 '24 16:05 sebas77

Hi @sebas77!

I have a few suggestions that might help improve your code and address the issues you're encountering: Instead of creating a new message writer for each buffer, consider using WriteAndCloseAsync() which will help reduce the number of data frames (each write and close operation generates a frame). To avoid unnecessary allocations, you can use the vtortola.WebSockets.BufferManager class to manage reusable buffers (part of WebSocketListenerOptions). Here's how you can adjust your code: Use BufferManager.TakeBuffer() to get a buffer. After WriteAndCloseAsync(), use BufferManager.ReturnBuffer(buffer) to return the buffer for reuse. Instead of queuing raw byte[], queue ArraySegment<byte> which allows you to work with buffers from BufferManager that may not exactly match the size of your message. This can help in better managing memory and reducing allocations.

Here’s an example of how you might refactor your code:


while (_textSenderQueue.TryDequeue(out var segment))
{
    var writer = _socket.CreateMessageWriter(vtortola.WebSockets.WebSocketMessageType.Text);

    await writer.WriteAndCloseAsync(segment.Array, segment.Offset, segment.Count, _cancellationToken);

    _bufferManager.ReturnBuffer(segment.Array);
}

The lag on mobile devices might be due to several factors. Consider profiling your application on mobile to pinpoint the exact cause of the lag. Tools like Unity Profiler can help identify performance bottlenecks. https://docs.unity3d.com/Manual/profiler-profiling-applications.html#profiling-target-platform

deniszykov avatar May 18 '24 15:05 deniszykov

Thanks.

The lag must be due to the way I use the library. It doesn't happen with the .net websocket class and happens only to the sending while receiving works alright

On Sat, 18 May 2024, 16:29 Denis Zykov, @.***> wrote:

Hi @sebas77 https://github.com/sebas77!

I have a few suggestions that might help improve your code and address the issues you're encountering: Instead of creating a new message writer for each buffer, consider using WriteAndCloseAsync() which will help reduce the number of data frames (each write and close operation generates a frame). To avoid unnecessary allocations, you can use the vtortola.WebSockets.BufferManager class to manage reusable buffers. Here's how you can adjust your code: Use BufferManager.TakeBuffer() to get a buffer. After WriteAndCloseAsync(), use BufferManager.ReturnBuffer(buffer) to return the buffer for reuse. Instead of queuing raw byte[], queue ArraySegment which allows you to work with buffers from BufferManager that may not exactly match the size of your message. This can help in better managing memory and reducing allocations.

Here’s an example of how you might refactor your code:

while (_textSenderQueue.TryDequeue(out var segment)){ var writer = _socket.CreateMessageWriter(vtortola.WebSockets.WebSocketMessageType.Text);

await writer.WriteAndCloseAsync(segment.Array, segment.Offset, segment.Count, _cancellationToken);

_bufferManager.ReturnBuffer(segment.Array);}

The lag on mobile devices might be due to several factors. Consider profiling your application on mobile to pinpoint the exact cause of the lag. Tools like Unity Profiler can help identify performance bottlenecks. https://docs.unity3d.com/Manual/profiler-profiling-applications.html#profiling-target-platform

— Reply to this email directly, view it on GitHub https://github.com/deniszykov/WebSocketListener/issues/65#issuecomment-2118858117, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHGZY3XYHNKPJKR2GXZT2LZC5XVPAVCNFSM6AAAAABH4N2CV6VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMJYHA2TQMJRG4 . You are receiving this because you were mentioned.Message ID: @.***>

sebas77 avatar May 18 '24 15:05 sebas77