⚡️ Speed up method MCPTransportRegistry.get_connector by 18%
#579
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
📄 18% (0.18x) speedup for
MCPTransportRegistry.get_connectorinmarimo/_server/ai/mcp/transport.py⏱️ Runtime :
252 microseconds→214 microseconds(best of104runs)📝 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 byreturn self._connectors[transport_type], performing two dictionary lookups for successful cases. The optimized version usestry/except KeyErrorpattern, requiring only one dictionary lookup in the common case where the key exists.2. Reduced Attribute Lookups in Constructor
Moved
MCPTransportType.STDIOandMCPTransportType.STREAMABLE_HTTPto local variables during dictionary construction, avoiding repeated attribute resolution during initialization.Performance Analysis:
trystatement itself is very fast at 188ns per hitTest Results Indicate:
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:
🌀 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
codeflash_concolic_k_oa4bjc/tmptdb1oms8/test_concolic_coverage.py::test_MCPTransportRegistry_get_connectorTo edit these changes
git checkout codeflash/optimize-MCPTransportRegistry.get_connector-mhty7a9vand push.