Skip to content

[Repo Assist] perf(mcp): eliminate LINQ and ToArray() allocations in McpToolBridge#243

Merged
shanselman merged 1 commit intomasterfrom
repo-assist/improve-mcp-bridge-allocations-2026-04-29-e773006a7c0f2a4c
May 1, 2026
Merged

[Repo Assist] perf(mcp): eliminate LINQ and ToArray() allocations in McpToolBridge#243
shanselman merged 1 commit intomasterfrom
repo-assist/improve-mcp-bridge-allocations-2026-04-29-e773006a7c0f2a4c

Conversation

@github-actions
Copy link
Copy Markdown
Contributor

🤖 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.Linq removed — FirstOrDefaultforeach

HandleToolsCallAsync was using LINQ FirstOrDefault with a lambda to find the capability for a tool call. Replaced with a plain foreach + break:

// Before
var capability = caps.FirstOrDefault(c => c.CanHandle(name));

// After
INodeCapability? capability = null;
foreach (var c in caps)
{
    if (!c.CanHandle(name)) continue;
    capability = c;
    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 in WriteResult / WriteError

MemoryStream.ToArray() allocates a new byte[] containing a copy of the stream contents. Using GetBuffer() with the actual length avoids that copy before the UTF-8 decode:

// Before
return System.Text.Encoding.UTF8.GetString(ms.ToArray());

// After
return System.Text.Encoding.UTF8.GetString(ms.GetBuffer(), 0, (int)ms.Length);

Both WriteResult and WriteError are touched. This saves one byte array allocation per JSON-RPC response.

Test Status

Suite Result
OpenClaw.Shared.Tests (full suite) ✅ 964 passed, 20 skipped; 3 pre-existing McpHttpServerTests Linux failures unrelated to this change (tracked in PR #238)
OpenClaw.Tray.Tests ✅ 202 passed, 0 failed

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

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

- 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>
@shanselman shanselman marked this pull request as ready for review May 1, 2026 17:20
@shanselman shanselman merged commit 1773cc7 into master May 1, 2026
@shanselman shanselman deleted the repo-assist/improve-mcp-bridge-allocations-2026-04-29-e773006a7c0f2a4c branch May 1, 2026 17:20
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