-
Notifications
You must be signed in to change notification settings - Fork 8.2k
⚡️ Speed up function remove_server_by_urls by 62% in PR #10727 (feat/http-stream-mcp)
#10773
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
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 optimized code achieves a **61% speedup** through three key algorithmic improvements that reduce computational overhead:
**1. Exception Handling Elimination in `_normalize_url_list`:**
- **Original:** Used try-except block with explicit error handling (51% of function time)
- **Optimized:** Relies on Python's built-in list comprehension error handling
- **Impact:** Eliminates exception overhead for the common case where input is already a valid sequence
**2. Single-Pass Algorithm in `_args_reference_urls` (Primary Optimization):**
- **Original:** Made multiple passes through data - first creating `args_strings` list, then converting to set, then accessing last element (95% of function time spent in the final `any()` generator)
- **Optimized:** Single loop that builds the set and tracks `last_arg` simultaneously, then uses efficient set intersection
- **Impact:** Reduces O(n) list traversals from 3 to 1, and replaces expensive generator evaluation with fast set operations
**3. Dictionary Access Optimization in `remove_server_by_urls`:**
- **Original:** Multiple `config_data["mcpServers"]` lookups and existence checks
- **Optimized:** Single `config_data.get("mcpServers")` with cached reference
- **Impact:** Eliminates redundant dictionary key lookups
**Performance Analysis:**
The line profiler shows the critical bottleneck was in `_args_reference_urls` (229ms → 47ms), specifically the `any()` generator expression that performed repeated string comparisons. The optimized version's set intersection (`args_set & urls_set`) leverages Python's highly optimized C implementation for set operations, making bulk URL matching significantly faster.
**Test Case Benefits:**
- Large-scale tests (500+ servers) see the most improvement due to reduced algorithmic complexity
- Mixed argument types benefit from the single-pass filtering
- Multiple URL matching scenarios benefit from set intersection efficiency
The optimization maintains identical behavior while dramatically improving performance for scenarios with many servers and URLs, which appears to be the primary use case based on the test coverage.
|
Important Review skippedBot user detected. To trigger a single review, invoke the You can disable this status message by setting the Comment |
Codecov Report❌ Patch coverage is ❌ Your project check 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 @@
## feat/http-stream-mcp #10773 +/- ##
=====================================================
Coverage 32.38% 32.38%
=====================================================
Files 1368 1368
Lines 63412 63381 -31
Branches 9373 9383 +10
=====================================================
- Hits 20537 20529 -8
+ Misses 41843 41817 -26
- Partials 1032 1035 +3
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
⚡️ 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.📄 62% (0.62x) speedup for
remove_server_by_urlsinsrc/backend/base/langflow/api/v1/mcp_projects.py⏱️ Runtime :
32.0 milliseconds→19.8 milliseconds(best of52runs)📝 Explanation and details
The optimized code achieves a 61% speedup through three key algorithmic improvements that reduce computational overhead:
1. Exception Handling Elimination in
_normalize_url_list:2. Single-Pass Algorithm in
_args_reference_urls(Primary Optimization):args_stringslist, then converting to set, then accessing last element (95% of function time spent in the finalany()generator)last_argsimultaneously, then uses efficient set intersection3. Dictionary Access Optimization in
remove_server_by_urls:config_data["mcpServers"]lookups and existence checksconfig_data.get("mcpServers")with cached referencePerformance Analysis:
The line profiler shows the critical bottleneck was in
_args_reference_urls(229ms → 47ms), specifically theany()generator expression that performed repeated string comparisons. The optimized version's set intersection (args_set & urls_set) leverages Python's highly optimized C implementation for set operations, making bulk URL matching significantly faster.Test Case Benefits:
The optimization maintains identical behavior while dramatically improving performance for scenarios with many servers and URLs, which appears to be the primary use case based on the test coverage.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10727-2025-11-28T04.49.35and push.