⚡️ Speed up method BaseSchemaGenerator._remove_converter by 12%
          #24
        
          
      
  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.
  
    
  
    
📄 12% (0.12x) speedup for
BaseSchemaGenerator._remove_converterinstarlette/schemas.py⏱️ Runtime :
409 microseconds→365 microseconds(best of346runs)📝 Explanation and details
The optimization adds a fast-path early return that avoids expensive regex operations for paths without closing braces (
}).Key optimization: Before calling the regex
_remove_converter_pattern.sub("}", path), the code now checksif '}' not in path: return path. This simple string containment check is much faster than regex pattern matching.Why this works: The converter pattern being removed has the format
:{converter_name}, which always ends with}. If there's no}in the path, there can't be any converters to remove, so the original path can be returned immediately.Performance impact:
}: Get 79-648% speedups (see test cases liketest_edge_empty_string,test_large_path_no_converters) because they skip regex entirelyThis optimization is particularly effective for applications with many simple routes (like
/users,/health,/api/status) that don't use path parameters, which is common in REST APIs where only a subset of endpoints need dynamic path segments.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
🔎 Concolic Coverage Tests and Runtime
codeflash_concolic_b9ikc1l3/tmpbzno8bzd/test_concolic_coverage.py::test_BaseSchemaGenerator__remove_converterTo edit these changes
git checkout codeflash/optimize-BaseSchemaGenerator._remove_converter-mhcbrps5and push.