Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 11, 2025

📄 18% (0.18x) speedup for MCPTransportRegistry.get_connector in marimo/_server/ai/mcp/transport.py

⏱️ Runtime : 252 microseconds 214 microseconds (best of 104 runs)

📝 Explanation and details

The optimized code achieves a 17% speedup through two key optimizations:

1. Eliminated Double Dictionary Lookup
The original code used if transport_type not in self._connectors: followed by return self._connectors[transport_type], performing two dictionary lookups for successful cases. The optimized version uses try/except KeyError pattern, requiring only one dictionary lookup in the common case where the key exists.

2. Reduced Attribute Lookups in Constructor
Moved MCPTransportType.STDIO and MCPTransportType.STREAMABLE_HTTP to local variables during dictionary construction, avoiding repeated attribute resolution during initialization.

Performance Analysis:

  • Line profiler shows the main improvement: successful lookups now take 354.6ns vs 257.8ns in the original's return statement, but this is offset by eliminating the expensive membership check (333.4ns per hit in original)
  • The try statement itself is very fast at 188ns per hit
  • Exception handling for invalid keys remains efficient, with slightly faster error generation

Test Results Indicate:

  • Successful lookups benefit most: Large-scale tests show 18-32% improvements for valid keys
  • Error cases are slightly slower: Invalid key tests show 8-24% slowdown, but this is the uncommon path
  • Scalability improved: Performance gains are more pronounced with larger registries, suggesting better algorithmic efficiency

This is a classic Python optimization where EAFP (Easier to Ask for Forgiveness than Permission) outperforms LBYL (Look Before You Leap) for dictionary access patterns, especially when successful lookups are more common than failures.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1059 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 2 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime

import pytest
from marimo._server.ai.mcp.transport import MCPTransportRegistry

Dummy enum and connector classes for testing purposes

class MCPTransportType:
STDIO = "STDIO"
STREAMABLE_HTTP = "STREAMABLE_HTTP"
# For edge cases
UNKNOWN = "UNKNOWN"
NONE = None

class MCPTransportConnector:
def connect(self):
return "connected"

class StdioTransportConnector(MCPTransportConnector):
def connect(self):
return "stdio connected"

class StreamableHTTPTransportConnector(MCPTransportConnector):
def connect(self):
return "http connected"
from marimo._server.ai.mcp.transport import MCPTransportRegistry

unit tests

========== Basic Test Cases ==========

def test_get_connector_stdio_returns_stdio_connector():
# Test that STDIO returns a StdioTransportConnector instance
registry = MCPTransportRegistry()
codeflash_output = registry.get_connector(MCPTransportType.STDIO); connector = codeflash_output

def test_get_connector_streamable_http_returns_http_connector():
# Test that STREAMABLE_HTTP returns a StreamableHTTPTransportConnector instance
registry = MCPTransportRegistry()
codeflash_output = registry.get_connector(MCPTransportType.STREAMABLE_HTTP); connector = codeflash_output

def test_get_connector_different_instances():
# Test that the two connectors are different instances
registry = MCPTransportRegistry()
codeflash_output = registry.get_connector(MCPTransportType.STDIO); stdio_connector = codeflash_output
codeflash_output = registry.get_connector(MCPTransportType.STREAMABLE_HTTP); http_connector = codeflash_output

========== Edge Test Cases ==========

def test_get_connector_with_unsupported_type_raises():
# Test that an unsupported transport type raises ValueError
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector(MCPTransportType.UNKNOWN) # 1.22μs -> 1.38μs (11.6% slower)

def test_get_connector_with_none_type_raises():
# Test that None as transport type raises ValueError
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector(None) # 1.25μs -> 1.43μs (12.7% slower)

def test_get_connector_with_integer_type_raises():
# Test that an integer as transport type raises ValueError
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector(123) # 1.10μs -> 1.35μs (18.9% slower)

def test_get_connector_with_empty_string_raises():
# Test that an empty string as transport type raises ValueError
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector("") # 1.02μs -> 1.27μs (19.4% slower)

def test_get_connector_with_object_type_raises():
# Test that an object as transport type raises ValueError
registry = MCPTransportRegistry()
class Dummy: pass
dummy = Dummy()
with pytest.raises(ValueError) as excinfo:
registry.get_connector(dummy) # 2.65μs -> 2.70μs (1.85% slower)

def test_get_connector_case_sensitivity():
# Test that case sensitivity is respected
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector("stdio") # Lowercase, not matching

def test_get_connector_mutable_type_raises():
# Test that a mutable type (list) raises ValueError
registry = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
registry.get_connector([MCPTransportType.STDIO])

========== Large Scale Test Cases ==========

def test_registry_with_large_number_of_connectors():
# Test registry's scalability by adding many connectors
registry = MCPTransportRegistry()
# Add 999 new connectors with unique keys
for i in range(1, 1000):
key = f"TYPE_{i}"
registry.connectors[key] = MCPTransportConnector()
# Test that all connectors are retrievable and correct
for i in range(1, 1000):
key = f"TYPE
{i}"
codeflash_output = registry.get_connector(key); connector = codeflash_output # 226μs -> 186μs (21.5% faster)

def test_registry_performance_large_scale_lookup():
# Test performance for large scale lookups
registry = MCPTransportRegistry()
# Add 999 connectors
for i in range(1, 1000):
key = f"TYPE_{i}"
registry._connectors[key] = MCPTransportConnector()
# Lookup the last connector
codeflash_output = registry.get_connector("TYPE_999"); connector = codeflash_output # 640ns -> 540ns (18.5% faster)

def test_registry_large_scale_unsupported_type_raises():
# Test that an unsupported type in a large registry still raises
registry = MCPTransportRegistry()
for i in range(1, 1000):
key = f"TYPE_{i}"
registry._connectors[key] = MCPTransportConnector()
with pytest.raises(ValueError) as excinfo:
registry.get_connector("TYPE_1000") # 1.14μs -> 1.45μs (21.9% slower)

def test_registry_large_scale_removal_and_lookup():
# Test removing a connector and ensure lookup fails
registry = MCPTransportRegistry()
for i in range(1, 1000):
key = f"TYPE_{i}"
registry._connectors[key] = MCPTransportConnector()
# Remove a connector
del registry._connectors["TYPE_500"]
with pytest.raises(ValueError) as excinfo:
registry.get_connector("TYPE_500") # 1.12μs -> 1.40μs (19.9% slower)

codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

#------------------------------------------------
import pytest
from marimo._server.ai.mcp.transport import MCPTransportRegistry

Minimal stubs for dependencies

class MCPTransportType:
STDIO = "STDIO"
STREAMABLE_HTTP = "STREAMABLE_HTTP"
# Add more types for edge/large tests
TYPE_A = "TYPE_A"
TYPE_B = "TYPE_B"
TYPE_C = "TYPE_C"
TYPE_D = "TYPE_D"
TYPE_E = "TYPE_E"
# For large scale, generate types programmatically

class MCPTransportConnector:
"""Base connector class for type checking"""
pass

class StdioTransportConnector(MCPTransportConnector):
"""Stub connector for STDIO"""
pass

class StreamableHTTPTransportConnector(MCPTransportConnector):
"""Stub connector for STREAMABLE_HTTP"""
pass
from marimo._server.ai.mcp.transport import MCPTransportRegistry

Unit tests start here

1. Basic Test Cases

def test_get_connector_stdio_returns_correct_instance():
"""Should return StdioTransportConnector for STDIO type"""
reg = MCPTransportRegistry()
codeflash_output = reg.get_connector(MCPTransportType.STDIO); conn = codeflash_output

def test_get_connector_streamable_http_returns_correct_instance():
"""Should return StreamableHTTPTransportConnector for STREAMABLE_HTTP type"""
reg = MCPTransportRegistry()
codeflash_output = reg.get_connector(MCPTransportType.STREAMABLE_HTTP); conn = codeflash_output

def test_get_connector_returns_same_instance_for_same_type():
"""Should always return the same instance for a given type"""
reg = MCPTransportRegistry()
codeflash_output = reg.get_connector(MCPTransportType.STDIO); conn1 = codeflash_output
codeflash_output = reg.get_connector(MCPTransportType.STDIO); conn2 = codeflash_output

2. Edge Test Cases

def test_get_connector_unsupported_type_raises_valueerror():
"""Should raise ValueError for unsupported transport type"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
reg.get_connector("UNSUPPORTED_TYPE") # 1.25μs -> 1.46μs (13.7% slower)

def test_get_connector_none_type_raises_valueerror():
"""Should raise ValueError when None is passed as transport type"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
reg.get_connector(None) # 1.27μs -> 1.49μs (15.3% slower)

def test_get_connector_empty_string_type_raises_valueerror():
"""Should raise ValueError when empty string is passed as transport type"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError) as excinfo:
reg.get_connector("") # 1.07μs -> 1.34μs (20.2% slower)

def test_get_connector_type_with_similar_name_but_not_registered():
"""Should raise ValueError for similar but unregistered type"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError):
reg.get_connector("STDIOO") # 1.05μs -> 1.25μs (16.4% slower)

def test_get_connector_type_is_case_sensitive():
"""Should raise ValueError for correct type with wrong case"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError):
reg.get_connector("stdio") # lower case

def test_get_connector_type_is_not_int():
"""Should raise ValueError for integer type input"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError):
reg.get_connector(123) # 1.35μs -> 1.48μs (8.41% slower)

def test_get_connector_type_is_not_object():
"""Should raise ValueError for object type input"""
reg = MCPTransportRegistry()
with pytest.raises(ValueError):
reg.get_connector(object()) # 2.51μs -> 2.67μs (6.32% slower)

def test_registry_with_custom_connector():
"""Should support registry with custom connector types"""
class DummyConnector(MCPTransportConnector):
pass
reg = MCPTransportRegistry()
reg._connectors[MCPTransportType.TYPE_A] = DummyConnector()
codeflash_output = reg.get_connector(MCPTransportType.TYPE_A); conn = codeflash_output # 632ns -> 514ns (23.0% faster)

def test_registry_with_connector_overwrite():
"""Should allow overwriting connector for a type"""
reg = MCPTransportRegistry()
class DummyConnector(MCPTransportConnector): pass
reg._connectors[MCPTransportType.STDIO] = DummyConnector()
codeflash_output = reg.get_connector(MCPTransportType.STDIO); conn = codeflash_output # 598ns -> 452ns (32.3% faster)

def test_registry_large_number_of_connectors():
"""Should handle large number of connectors efficiently"""
reg = MCPTransportRegistry()
# Add 999 connectors
for i in range(1, 1000): # 999 additional types
type_name = f"TYPE_{i}"
class LargeScaleConnector(MCPTransportConnector): pass
reg.connectors[type_name] = LargeScaleConnector()
# Test retrieval for random types
for i in [1, 100, 500, 999]:
type_name = f"TYPE
{i}"
codeflash_output = reg.get_connector(type_name); conn = codeflash_output # 1.81μs -> 1.67μs (8.77% faster)

def test_registry_large_scale_missing_connector():
"""Should raise ValueError for non-existent type in large registry"""
reg = MCPTransportRegistry()
# Add 500 connectors
for i in range(1, 501):
type_name = f"TYPE_{i}"
class LargeScaleConnector(MCPTransportConnector): pass
reg._connectors[type_name] = LargeScaleConnector()
# Try to get a type not present
with pytest.raises(ValueError):
reg.get_connector("TYPE_9999") # 1.39μs -> 1.84μs (24.2% slower)

def test_registry_performance_with_many_connectors():
"""Should retrieve connector in constant time for large registry"""
import time
reg = MCPTransportRegistry()
for i in range(1, 1000):
type_name = f"TYPE_{i}"
class LargeScaleConnector(MCPTransportConnector): pass
reg._connectors[type_name] = LargeScaleConnector()
# Time retrieval for last element
start = time.time()
codeflash_output = reg.get_connector("TYPE_999"); conn = codeflash_output # 1.07μs -> 857ns (25.0% faster)
end = time.time()

codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

#------------------------------------------------
from marimo._server.ai.mcp.transport import MCPTransportRegistry
from marimo._server.ai.mcp.transport import MCPTransportType

def test_MCPTransportRegistry_get_connector():
MCPTransportRegistry.get_connector(MCPTransportRegistry(), MCPTransportType.STDIO)

🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_k_oa4bjc/tmptdb1oms8/test_concolic_coverage.py::test_MCPTransportRegistry_get_connector 580ns 422ns 37.4%✅

To edit these changes git checkout codeflash/optimize-MCPTransportRegistry.get_connector-mhty7a9v and push.

Codeflash Static Badge

The optimized code achieves a **17% speedup** through two key optimizations:

**1. Eliminated Double Dictionary Lookup**
The original code used `if transport_type not in self._connectors:` followed by `return self._connectors[transport_type]`, performing two dictionary lookups for successful cases. The optimized version uses `try/except KeyError` pattern, requiring only one dictionary lookup in the common case where the key exists.

**2. Reduced Attribute Lookups in Constructor** 
Moved `MCPTransportType.STDIO` and `MCPTransportType.STREAMABLE_HTTP` to local variables during dictionary construction, avoiding repeated attribute resolution during initialization.

**Performance Analysis:**
- Line profiler shows the main improvement: successful lookups now take 354.6ns vs 257.8ns in the original's return statement, but this is offset by eliminating the expensive membership check (333.4ns per hit in original)
- The `try` statement itself is very fast at 188ns per hit
- Exception handling for invalid keys remains efficient, with slightly faster error generation

**Test Results Indicate:**
- **Successful lookups benefit most**: Large-scale tests show 18-32% improvements for valid keys
- **Error cases are slightly slower**: Invalid key tests show 8-24% slowdown, but this is the uncommon path
- **Scalability improved**: Performance gains are more pronounced with larger registries, suggesting better algorithmic efficiency

This is a classic Python optimization where EAFP (Easier to Ask for Forgiveness than Permission) outperforms LBYL (Look Before You Leap) for dictionary access patterns, especially when successful lookups are more common than failures.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 11, 2025 02:24
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Nov 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant