⚡️ Speed up method BaseClient._calculate_retry_timeout by 9%
#38
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.
📄 9% (0.09x) speedup for
BaseClient._calculate_retry_timeoutinsrc/openai/_base_client.py⏱️ Runtime :
448 microseconds→410 microseconds(best of8runs)📝 Explanation and details
The optimization focuses on restructuring the
_parse_retry_after_headermethod to reduce unnecessary exception handling and redundant operations.Key optimizations applied:
Eliminated redundant try-catch blocks: The original code used try-catch around
response_headers.get("retry-after-ms", None)andfloat(retry_ms_header)even whenretry_ms_headercould beNone. The optimized version first checks if the header exists (retry_ms_header is not None) before attempting the float conversion, avoiding the expensive exception handling path when headers are missing.Reduced exception overhead: Instead of catching
TypeError/ValueErrorexceptions for every header lookup, the code now uses explicit null checks. This is significantly faster since exceptions in Python have substantial overhead compared to simple conditional checks.Improved control flow: The optimized version uses a cleaner nested structure where date parsing (
email.utils.parsedate_tz) only occurs after confirming theretry_headerexists and the float conversion failed. This reduces unnecessary expensive date parsing operations.Early returns with guards: By checking header existence before processing, the code avoids creating exception objects and unwinding the call stack in the common case where headers are missing.
The 9% speedup comes primarily from avoiding exception handling overhead when response headers don't contain retry information, which appears to be a common case in the test scenarios. The optimization is most effective when
retry-after-msheaders are absent andretry-afterheaders are either missing or contain non-numeric values, reducing the cost of these frequent negative cases.✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
test_client.py::TestOpenAI.test_parse_retry_after_headerTo edit these changes
git checkout codeflash/optimize-BaseClient._calculate_retry_timeout-mhdacyw1and push.