⚡️ Speed up function contains_tag by 6%
          #103
        
          
      
                
     Open
            
            
          
  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.
  
    
  
    
📄 6% (0.06x) speedup for
contains_taginlitellm/litellm_core_utils/prompt_templates/factory.py⏱️ Runtime :
362 microseconds→340 microseconds(best of165runs)📝 Explanation and details
The optimization removes the capturing group
(.+?)and replaces it with a non-capturing group.+?in the regex pattern. This changesf"<{tag}>(.+?)</{tag}>"tof"<{tag}>.+?</{tag}>".Key optimization: Eliminating the unnecessary capturing group reduces regex engine overhead. Since the function only needs to check if the pattern exists (not extract the matched content), capturing groups create unnecessary work for the regex engine - it has to store and track the captured text even though it's never used.
Why this leads to speedup: Python's regex engine allocates memory and performs additional bookkeeping for each capturing group. By removing the parentheses, the engine can focus purely on pattern matching without the overhead of capturing and storing the matched content.
Test case performance patterns: The optimization shows particularly strong gains (15-37% faster) on large content tests with long strings inside tags, where the regex engine would otherwise capture and store substantial amounts of text. For basic cases with short content, gains are more modest (1-11%) but still consistent. The few slower cases (1-5%) appear to be within measurement noise and don't indicate a real regression.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__vli01p5/tmp4j4ivq06/test_concolic_coverage.py::test_contains_tagTo edit these changes
git checkout codeflash/optimize-contains_tag-mhboykhcand push.