Skip to content

Websocket message handler writes should consider setting MinimumSpanLength. #1234

@cg110

Description

@cg110

Hi,

I've been trying to track down a performance issue with multiple calls when using a websocket against localhost (not sure if remote calls would be different)

Using wireshark I was seeing messages be broken up into fragments, rather than one big message. On closer inspection it appears that the usage Sequence without a shared pool, or setting the minimum size means that the generated blocks are quite small, and it appears that every write to the websocket goes to the wire (maybe quirk of localhost that it's not buffered up)

Changing the write buffer:
https://github.com/microsoft/vs-streamjsonrpc/blob/main/src/StreamJsonRpc/WebSocketMessageHandler.cs#L141

To:

using (var contentSequenceBuilder = new Sequence<byte>(ArrayPool<byte>.Shared) { MinimumSpanLength = sizeHint})

Gives a decent improvement in response time when testing 10 threads making 100 calls concurrently through the same proxy object (so sharing the websocket), the average response time is down from 15ms to 5ms (for netfx, on net 9 the improvement isn't as large)

For netfx the websocket messages are no longer fragmented, and the arraypool is hopefully reducing memory allocations. Note that the main improvement is from the sizeHint, as the shared arraypool only dropped the response times slightly.

Thanks,
Chris

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions