⚡️ Speed up method MCPComposerService._obfuscate_command_secrets by 137% in PR #10347 (cz/fix-mcptimeout-1.6.5)
          #10451
        
          
      
      
        
          +12
        
        
          −15
        
        
          
        
      
    
  
  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.
  
    
  
    
⚡️ This pull request contains optimizations for PR #10347
If you approve this dependent PR, these changes will be merged into the original PR branch
cz/fix-mcptimeout-1.6.5.📄 137% (1.37x) speedup for
MCPComposerService._obfuscate_command_secretsinsrc/backend/base/langflow/services/mcp_composer/service.py⏱️ Runtime :
2.63 milliseconds→1.11 milliseconds(best of114runs)📝 Explanation and details
The optimization achieves a 136% speedup (2.63ms → 1.11ms) by eliminating expensive Python operations in a tight loop through several key improvements:
What was optimized:
safe_cmd.appendandsafe_cmd.extendas local variables to avoid repeated attribute lookups on each iterationlen(cmd)ascmd_lento avoid recalculating the list length on every loop condition checkany()generator expression with directorcomparisons to eliminate the overhead of creating a generator object and callingany()env_key.lower()outside the comparison chain to avoid multiple callsWhy this leads to speedup:
The original code's bottleneck was the
any()generator expression (35.6% of total time), which creates a temporary generator object and performs function calls for each secret check. The optimized version uses direct booleanoroperations that are much faster in Python. Additionally, caching method references eliminates Python's attribute lookup overhead, which is significant in tight loops.Test case performance:
These optimizations are particularly effective for the large-scale test cases (
test_large_*functions) that process 500+ command arguments, where the loop overhead compounds. The optimization maintains identical behavior while being most beneficial when processing commands with many--envarguments, especially those containing secret keys that trigger the substring matching logic.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-pr10347-2025-10-30T14.41.06and push.