⚡️ Speed up method HttpxBinaryResponseContent.iter_bytes by 74%
#26
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.
📄 74% (0.74x) speedup for
HttpxBinaryResponseContent.iter_bytesinsrc/openai/_legacy_response.py⏱️ Runtime :
19.1 microseconds→11.0 microseconds(best of253runs)📝 Explanation and details
The optimization replaces
return self.response.iter_bytes(chunk_size)withyield from self.response.iter_bytes(chunk_size), eliminating an unnecessary function call overhead.Key Change:
response.iter_bytes()yield fromto delegate iteration directly to the underlying iteratorWhy it's faster:
In Python,
return iteratorcreates a function call that returns an iterator object, whileyield from iteratormakes the current function a generator that delegates directly to the underlying iterator. This eliminates the overhead of the extra function call and iterator wrapping, reducing the call stack depth and associated overhead.Performance characteristics:
This micro-optimization is especially beneficial when
iter_bytes()is called frequently in streaming scenarios or when processing large amounts of binary data, as it reduces per-call overhead without changing the API or behavior.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-HttpxBinaryResponseContent.iter_bytes-mhcxq9fqand push.