⚡️ Speed up method VertexBase._credentials_from_service_account by 15%
          #152
        
          
      
  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.
  
    
  
    
📄 15% (0.15x) speedup for
VertexBase._credentials_from_service_accountinlitellm/llms/vertex_ai/vertex_llm_base.py⏱️ Runtime :
10.1 microseconds→8.79 microseconds(best of15runs)📝 Explanation and details
The optimization moves the
import google.oauth2.service_accountstatement from inside the_credentials_from_service_accountmethod to module-level, eliminating repeated import overhead on every function call.Key changes:
_service_account_credsthat wraps the credential creation logicWhy this speeds up the code:
Python's import system has overhead even for already-cached modules. The line profiler shows the import statement consumed 98.3% of the original function's execution time (104ms out of 106ms total). By moving the import to module-level, this overhead is paid only once when the module loads, rather than on every method invocation.
Test case performance:
The optimization provides consistent 14-15% speedup across all test scenarios, from basic validation cases to error handling paths. This improvement is particularly valuable for applications that frequently create service account credentials, as the per-call overhead reduction compounds with usage frequency.
The optimization maintains identical behavior and error handling while eliminating a significant performance bottleneck in credential creation workflows.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-VertexBase._credentials_from_service_account-mhdatu20and push.