⚡️ Speed up function extract_between_tags by 8%
          #102
        
          
      
  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.
  
    
  
    
📄 8% (0.08x) speedup for
extract_between_tagsinlitellm/litellm_core_utils/prompt_templates/factory.py⏱️ Runtime :
1.68 milliseconds→1.56 milliseconds(best of175runs)📝 Explanation and details
The optimization introduces regex pattern caching using a module-level dictionary
_tag_re_cacheto store pre-compiled regex patterns for each unique tag.Key changes:
re.findall()which compiles the regex pattern on every function call, the optimized version compiles the pattern once usingre.compile()and stores it in_tag_re_cachepattern.findall(string)Why this leads to speedup:
Regex compilation is computationally expensive. The original code's
re.findall(f"<{tag}>(.+?)</{tag}>", string, re.DOTALL)recompiles the same pattern every time, even for repeated tag names. By caching compiled patterns, we eliminate redundant compilation overhead.Performance characteristics from test results:
The caching strategy is most effective for workloads with repeated tag extractions, which appears to be the common usage pattern based on the test suite.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic__vli01p5/tmpjc3b71zz/test_concolic_coverage.py::test_extract_between_tagsTo edit these changes
git checkout codeflash/optimize-extract_between_tags-mhbopf4kand push.