[Repo Assist] perf(mcp): eliminate LINQ and ToArray() allocations in McpToolBridge#243
Merged
shanselman merged 1 commit intomasterfrom May 1, 2026
Conversation
- Remove System.Linq import; replace FirstOrDefault with foreach loop in HandleToolsCallAsync — avoids delegate allocation on every tool call - Replace ms.ToArray() with ms.GetBuffer() + slice in WriteResult and WriteError — avoids copying the byte array before UTF-8 decoding Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
This was referenced Apr 30, 2026
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 PR from Repo Assist.
Two small allocation wins in the MCP hot path — one per tool call dispatch, one per JSON-RPC response:
1.
System.Linqremoved —FirstOrDefault→foreachHandleToolsCallAsyncwas using LINQFirstOrDefaultwith a lambda to find the capability for a tool call. Replaced with a plainforeach+break:This follows the existing pattern in the codebase of eliminating LINQ in hot paths (see previous PRs removing LINQ from
SystemCapability,ExecShellWrapperParser, etc.).2.
ms.ToArray()→ms.GetBuffer()+ slice inWriteResult/WriteErrorMemoryStream.ToArray()allocates a newbyte[]containing a copy of the stream contents. UsingGetBuffer()with the actual length avoids that copy before the UTF-8 decode:Both
WriteResultandWriteErrorare touched. This saves one byte array allocation per JSON-RPC response.Test Status
OpenClaw.Shared.Tests(full suite)McpHttpServerTestsLinux failures unrelated to this change (tracked in PR #238)OpenClaw.Tray.Tests