[Repo Assist] improve: pool receive buffer in WebSocketClientBase via ArrayPool#204
Merged
shanselman merged 1 commit intomasterfrom Apr 23, 2026
Conversation
The ListenForMessagesAsync method previously allocated a new byte[] of 16-64 KB per connection, which can land on the Large Object Heap and increase GC pressure on long-lived connections. SendRawAsync already uses ArrayPool<byte>.Shared, so this change makes the receive path consistent. Key changes: - Replace new byte[ReceiveBufferSize] with ArrayPool<byte>.Shared.Rent - Slice ArraySegment to ReceiveBufferSize to prevent ArrayPool oversize from exposing extra bytes to ReceiveAsync - Return buffer in a finally block covering all exit paths No behaviour change; test suite unaffected (630 passed, 20 skipped). Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
13 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
🤖 This is an automated draft PR from Repo Assist.
Summary
ListenForMessagesAsyncpreviously allocated anew byte[ReceiveBufferSize]per connection — a 16 KB (gateway) or 64 KB (node) allocation that can land on the Large Object Heap and increase GC pressure on long-lived connections.SendRawAsyncalready rents fromArrayPool<byte>.Shared; this change makes the receive path consistent.Changes
src/OpenClaw.Shared/WebSocketClientBase.csnew byte[ReceiveBufferSize]withArrayPool<byte>.Shared.Rent(ReceiveBufferSize)ArraySegmenttoReceiveBufferSizeto prevent an oversized rented buffer from exposing extra bytes toReceiveAsyncfinallyblock that covers all exit paths (normal close, exceptions, reconnect)Rationale
new byte[16384]per connection — heap allocated, GC-collectedSendRawAsyncwhich already usesArrayPoolArrayPoolThe
ArrayPooloverhead is a single dictionary lookup on rent/return; the benefit is avoiding LOH pressure on services with frequent reconnects.Test Status
dotnet build OpenClaw.Shared— succeeded, 0 errors