⚡️ Speed up method PetalsConfig.get_config by 11%
          #177
        
          
      
      
        
          +26
        
        
          −54
        
        
          
        
      
    
  
  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.
  
    
  
    
📄 11% (0.11x) speedup for
PetalsConfig.get_configinlitellm/llms/petals/completion/transformation.py⏱️ Runtime :
59.5 microseconds→53.4 microseconds(best of74runs)📝 Explanation and details
The optimization applies two key performance improvements to the
BaseConfig.get_config()method:1. Pre-computed function types tuple: The original code recreates a tuple of function types
(types.FunctionType, types.BuiltinFunctionType, classmethod, staticmethod, property)on every method call. The optimized version moves this to module level as_function_types, eliminating repeated tuple construction.2. Enhanced filtering strategy: Instead of just using
isinstance(v, _function_types), the optimized version addsnot callable(v)as an additional filter. This provides a faster path for excluding callable objects before the more expensiveisinstancecheck, sincecallable()is a lightweight builtin function.3. Direct dictionary access: Replaced
cls.__dict__.items()withvars(cls).items(). Whilevars(cls)returns the same__dict__object, it's slightly more direct and avoids attribute lookup overhead.The line profiler shows the optimization reduces the total time in
BaseConfig.get_config()from 307.88ns to 254.52ns (17% faster), with the dictionary comprehension itself becoming more efficient.These optimizations are particularly effective for the test cases shown because:
get_config()multiple times benefit from the pre-computed tuple✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-PetalsConfig.get_config-mhdt495xand push.