⚡️ Speed up method `BaseClient._calculate_retry_timeout` by 9% by codeflash-ai[bot] · Pull Request #38 · codeflash-ai/openai-python
The optimization focuses on restructuring the `_parse_retry_after_header` method to reduce unnecessary exception handling and redundant operations.
**Key optimizations applied:**
1. **Eliminated redundant try-catch blocks**: The original code used try-catch around `response_headers.get("retry-after-ms", None)` and `float(retry_ms_header)` even when `retry_ms_header` could be `None`. 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.
2. **Reduced exception overhead**: Instead of catching `TypeError`/`ValueError` exceptions 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.
3. **Improved control flow**: The optimized version uses a cleaner nested structure where date parsing (`email.utils.parsedate_tz`) only occurs after confirming the `retry_header` exists and the float conversion failed. This reduces unnecessary expensive date parsing operations.
4. **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-ms` headers are absent and `retry-after` headers are either missing or contain non-numeric values, reducing the cost of these frequent negative cases.