⚡️ Speed up function config_contains_server_url by 97% in PR #10727 (feat/http-stream-mcp)#10748
⚡️ Speed up function config_contains_server_url by 97% in PR #10727 (feat/http-stream-mcp)#10748codeflash-ai[bot] wants to merge 5 commits into
config_contains_server_url by 97% in PR #10727 (feat/http-stream-mcp)#10748Conversation
fix tests refactor mcp and mcp_projects backwards compat with SSE transport provide streamable http option for json mcp config remove streamable_http mgmt and update tests
The optimization achieves a **97% speedup** by fundamentally changing how URL matching is performed in the `_args_reference_urls` function. The key change replaces an expensive `any()` generator expression with efficient set operations.
**Primary Optimization:**
The original code used `any((url == last_arg) or (url in args_set) for url in urls)` which performs individual comparisons for each URL. The optimized version uses `bool(args_set.intersection(urls)) or last_arg in urls`, leveraging Python's highly optimized C-level set intersection operation.
**Performance Impact:**
- **Original**: 64.7ms spent in the critical line (93% of function time)
- **Optimized**: 4.6ms spent in the critical line (49% of function time)
- This represents a ~14x improvement on the bottleneck operation alone
**Why This Works:**
Set intersection (`args_set.intersection(urls)`) is implemented in C and processes multiple items simultaneously, while the generator expression processes URLs one-by-one in Python bytecode. The `or last_arg in urls` fallback handles the special case efficiently when no general intersection exists.
**Minor Optimization:**
The code also extracts `server_config.get("args", [])` into a variable to avoid repeated dictionary lookups, though this has minimal impact compared to the set intersection change.
**Test Case Performance:**
The optimization particularly excels with large-scale test cases involving many servers, URLs, or arguments, where set operations provide the most benefit over iterative comparisons. Basic functionality and edge cases maintain identical behavior while running significantly faster.
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Tip 📝 Customizable high-level summaries are now available in beta!You can now customize how CodeRabbit generates the high-level summary in your pull requests — including its content, structure, tone, and formatting.
Example instruction:
Note: This feature is currently in beta for Pro-tier users, and pricing will be announced later. Comment |
Codecov Report❌ Patch coverage is ❌ Your project status has failed because the head coverage (40.05%) is below the target coverage (60.00%). You can increase the head coverage or adjust the target coverage. Additional details and impacted files@@ Coverage Diff @@
## main #10748 +/- ##
==========================================
+ Coverage 32.38% 32.65% +0.27%
==========================================
Files 1368 1368
Lines 63412 63686 +274
Branches 9373 9367 -6
==========================================
+ Hits 20537 20798 +261
- Misses 41843 41858 +15
+ Partials 1032 1030 -2
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
d053266 to
9f7014d
Compare
|
Closing automated codeflash PR. |
⚡️ This pull request contains optimizations for PR #10727
If you approve this dependent PR, these changes will be merged into the original PR branch
feat/http-stream-mcp.📄 97% (0.97x) speedup for
config_contains_server_urlinsrc/backend/base/langflow/api/v1/mcp_projects.py⏱️ Runtime :
9.30 milliseconds→4.72 milliseconds(best of66runs)📝 Explanation and details
The optimization achieves a 97% speedup by fundamentally changing how URL matching is performed in the
_args_reference_urlsfunction. The key change replaces an expensiveany()generator expression with efficient set operations.Primary Optimization:
The original code used
any((url == last_arg) or (url in args_set) for url in urls)which performs individual comparisons for each URL. The optimized version usesbool(args_set.intersection(urls)) or last_arg in urls, leveraging Python's highly optimized C-level set intersection operation.Performance Impact:
Why This Works:
Set intersection (
args_set.intersection(urls)) is implemented in C and processes multiple items simultaneously, while the generator expression processes URLs one-by-one in Python bytecode. Theor last_arg in urlsfallback handles the special case efficiently when no general intersection exists.Minor Optimization:
The code also extracts
server_config.get("args", [])into a variable to avoid repeated dictionary lookups, though this has minimal impact compared to the set intersection change.Test Case Performance:
The optimization particularly excels with large-scale test cases involving many servers, URLs, or arguments, where set operations provide the most benefit over iterative comparisons. Basic functionality and edge cases maintain identical behavior while running significantly faster.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10727-2025-11-27T04.36.16and push.