⚡️ Speed up function get_binary_stdin
by 14%
#46
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.
📄 14% (0.14x) speedup for
get_binary_stdin
insrc/click/_compat.py
⏱️ Runtime :
39.7 microseconds
→34.8 microseconds
(best of172
runs)📝 Explanation and details
To optimize the given program, we can make a few changes. The original program is already efficient, but there are some minor improvements that can be made, such as removing unnecessary type casting and minimizing function calls.
Here is the optimized version of the program.
Changes Made.
if _is_binary_reader(stream, False)
directly returns the stream without casting since it's known to be binary.t.cast(t.BinaryIO, stream)
since the type is already correctly inferred._is_binary_reader
to ensure streamlined logic.This maintains the original intent and logic of the code but with slight performance improvements.
✅ Correctness verification report:
🌀 Generated Regression Tests Details
📢 Feedback on this optimization?