⚡️ Speed up method CustomGuardrail._guardrail_is_in_requested_guardrails by 156%
          #180
        
          
      
  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.
  
    
  
    
📄 156% (1.56x) speedup for
CustomGuardrail._guardrail_is_in_requested_guardrailsinlitellm/integrations/custom_guardrail.py⏱️ Runtime :
821 microseconds→320 microseconds(best of7runs)📝 Explanation and details
The optimization introduces two key performance improvements to the
_guardrail_is_in_requested_guardrailsmethod:1. Fast-path optimization for string lists: The optimized code detects when
requested_guardrailsis a list of strings and uses Python's built-ininoperator instead of iterating through each element. This leverages Python's highly optimized string membership testing, which is significantly faster than manual iteration withisinstance()checks.2. Early None check: The code now checks if
guardrail_nameis None upfront and immediately returns False, avoiding any unnecessary iteration when the guardrail name is not set.Why this leads to speedup:
inoperator for lists uses optimized C code that's much faster than Python loops with type checksguardrail_nameis None eliminates wasted cyclesTest case performance patterns:
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
guardrails_tests/test_custom_guardrail.py::test_guardrail_is_in_requested_guardrails🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_zbim32de/tmpir8ytiis/test_concolic_coverage.py::test_CustomGuardrail__guardrail_is_in_requested_guardrailscodeflash_concolic_zbim32de/tmpir8ytiis/test_concolic_coverage.py::test_CustomGuardrail__guardrail_is_in_requested_guardrails_2codeflash_concolic_zbim32de/tmpir8ytiis/test_concolic_coverage.py::test_CustomGuardrail__guardrail_is_in_requested_guardrails_3To edit these changes
git checkout codeflash/optimize-CustomGuardrail._guardrail_is_in_requested_guardrails-mhdw73k6and push.