Skip to content

[Repo Assist] improve: pool receive buffer in WebSocketClientBase via ArrayPool#204

Merged
shanselman merged 1 commit intomasterfrom
repo-assist/improve-receive-buffer-pooling-2026-04-23-c42756da67d8f312
Apr 23, 2026
Merged

[Repo Assist] improve: pool receive buffer in WebSocketClientBase via ArrayPool#204
shanselman merged 1 commit intomasterfrom
repo-assist/improve-receive-buffer-pooling-2026-04-23-c42756da67d8f312

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 This is an automated draft PR from Repo Assist.

Summary

ListenForMessagesAsync previously allocated a new 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. SendRawAsync already rents from ArrayPool<byte>.Shared; this change makes the receive path consistent.

Changes

src/OpenClaw.Shared/WebSocketClientBase.cs

  • Replace new byte[ReceiveBufferSize] with ArrayPool<byte>.Shared.Rent(ReceiveBufferSize)
  • Slice ArraySegment to ReceiveBufferSize to prevent an oversized rented buffer from exposing extra bytes to ReceiveAsync
  • Return buffer in a finally block that covers all exit paths (normal close, exceptions, reconnect)

Rationale

Before After
new byte[16384] per connection — heap allocated, GC-collected Rented from shared pool — reused across connections
Inconsistent with SendRawAsync which already uses ArrayPool Both send and receive paths use ArrayPool

The ArrayPool overhead is a single dictionary lookup on rent/return; the benefit is avoiding LOH pressure on services with frequent reconnects.

Test Status

  • ✅ Build: dotnet build OpenClaw.Sharedsucceeded, 0 errors
  • ✅ Shared tests: 630 passed, 20 skipped
  • ✅ Tray tests: 122 passed, 0 failures
  • No source logic changes; only the receive buffer allocation strategy is updated.

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a

Generated by 🌈 Repo Assist, see workflow run. Learn more.

To install this agentic workflow, run

gh aw add githubnext/agentics/workflows/repo-assist.md@97143ac59cb3a13ef2a77581f929f06719c7402a

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>
@shanselman shanselman marked this pull request as ready for review April 23, 2026 17:25
@shanselman shanselman merged commit 89322f2 into master Apr 23, 2026
@shanselman shanselman deleted the repo-assist/improve-receive-buffer-pooling-2026-04-23-c42756da67d8f312 branch April 23, 2026 17:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant