Skip to content

⚡ Optimize mcpService _refreshToolList with nested loops - #20

Open
Rukafuu wants to merge 1 commit into
mainfrom
perf-mcp-refresh-opt-10615532815197895461
Open

⚡ Optimize mcpService _refreshToolList with nested loops#20
Rukafuu wants to merge 1 commit into
mainfrom
perf-mcp-refresh-opt-10615532815197895461

Conversation

@Rukafuu

@Rukafuu Rukafuu commented Jun 15, 2026

Copy link
Copy Markdown
Owner

This PR optimizes the _refreshToolList method in Chat/backend/services/mcpService.js.

💡 What

Replaced:

        for (const server of this.servers.values()) {
            allTools.push(...server.tools.map(t => ({
                ...t,
                _server: server.name
            })));
        }

with:

        for (const server of this.servers.values()) {
            const serverName = server.name;
            for (const tool of server.tools) {
                allTools.push({
                    ...tool,
                    _server: serverName
                });
            }
        }

🎯 Why

The original code used Array.prototype.map followed by a spread push. For large tool lists, the spread operator can exceed stack limits. Furthermore, my benchmarks indicated that even Array.prototype.flatMap (the initially suggested optimization) was slower than a simple nested loop in this Node.js environment due to intermediate array allocations.

📊 Measured Improvement

Benchmarking with 100 servers and 50 tools per server (5000 tools total):

  • Original: 0.2464ms avg
  • Optimized (Nested Loops): 0.2273ms avg (~7.74% faster)
  • Proposed flatMap: 0.3491ms avg (approx 41% slower than original)

The nested loop approach provides the best performance and highest safety against large datasets.

Verification and benchmark scripts have been added to Chat/backend/tests/ and Chat/backend/benchmarks/ respectively.


PR created automatically by Jules for task 10615532815197895461 started by @Rukafuu

Replaced the inefficient spread-push approach in _refreshToolList with a nested loop.
Benchmarks showed that for 100 servers and 5000 tools total:
- Original (spread-push): ~0.24ms per call
- flatMap approach: ~0.35ms per call
- Nested loops: ~0.22ms per call (approx 8% improvement over original, 37% over flatMap)

Nested loops avoid the overhead of intermediate array creation and potential stack limit issues with large spreads.

Co-authored-by: Rukafuu <111822334+Rukafuu@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel

vercel Bot commented Jun 15, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
lira-os Ready Ready Preview, Comment Jun 15, 2026 5:28pm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant