⚡️ Speed up function _safe_get_request_query_params by 43%
          #162
        
          
      
  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.
  
    
  
    
📄 43% (0.43x) speedup for
_safe_get_request_query_paramsinlitellm/proxy/common_utils/http_parsing_utils.py⏱️ Runtime :
551 microseconds→385 microseconds(best of392runs)📝 Explanation and details
The optimization achieves a 43% speedup by making two key changes to how query parameters are accessed and converted:
1. Replaced
hasattr+ attribute access withgetattrif hasattr(request, "query_params"): return dict(request.query_params)qp = getattr(request, "query_params", None)hasattrinternally performs the same lookup as direct attribute access, so the original code was doing the work twice2. Direct access to internal
_dictattribute when availableQueryParamsobjects (common in FastAPI), the optimization checks if the internal_dictattribute exists and directly copies it:return qp_dict.copy()dict()constructor that would iterate through all key-value pairsdict(qp)for non-Starlette objects to maintain compatibilityPerformance gains are most significant for:
_dictshortcut avoids iterating through all parametersdict()constructorThe optimization maintains identical behavior and error handling while dramatically reducing computational overhead, especially for the common case of FastAPI/Starlette request objects.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_safe_get_request_query_params-mhdfc9uaand push.