⚡️ Speed up function is_mcp_config_empty by 16%
          #2
        
          
      
  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.
  
    
  
    
📄 16% (0.16x) speedup for
is_mcp_config_emptyinmarimo/_server/ai/mcp/config.py⏱️ Runtime :
44.7 microseconds→38.5 microseconds(best of216runs)📝 Explanation and details
The optimized code achieves a 16% speedup by implementing short-circuit evaluation to avoid unnecessary dictionary lookups and boolean operations.
Key optimization:
config.get("mcpServers")andconfig.get("presets")in a single boolean expression, the optimized version checksmcpServersfirst and returnsFalseimmediately if it's truthymcpServersis non-empty (which happens in many test cases), the code avoids the secondconfig.get("presets")call entirelyPerformance impact by test case type:
mcpServersis non-empty, because the optimization completely skips thepresetslookuppresetsis non-empty butmcpServersis empty, due to the extra variable assignment and conditional check overheadThe optimization is particularly effective for the common scenario where configurations have non-empty
mcpServers, allowing the function to return early without evaluating the second condition.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-is_mcp_config_empty-mgiig95tand push.