⚡️ Speed up function validate_float_arg by 20%#2
Open
codeflash-ai[bot] wants to merge 1 commit into
Open
Conversation
Here’s an optimized version of your program, focusing on removing redundant computations, minimizing branching, and cutting function call overhead. The main improvements. 1. **Short-circuit the most common valid cases**: Fast path for numeric, non-negative, non-NaN, non-inf. 2. **Single isinstance check**: Use the C-API speed of `type(x) in (float, int)` when possible, rather than `isinstance` (although in some versions, `type(x) in ...` can be slightly faster). 3. **Minimize math module calls**: Only call `math.isinf`/`math.isnan` if the type is `float` (skip for `int`, since `isinf(int)`/`isnan(int)` are always False, so avoid the calls). 4. **Reduce string formatting on error**: Only format error strings on the (rare) error path. **Key optimization points:** - Avoid `math.isinf`/`math.isnan` on integers (can't ever be true). - Avoid unnecessary formatting except when raising an exception. - Use the fast type check path for the biggest classes (`int`, `float`). - Return directly when possible to reduce overhead. This should lead to a measurable speedup, especially for the typical case where the value is a valid `int` or `float`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
📄 20% (0.20x) speedup for
validate_float_arginkeras/src/regularizers/regularizers.py⏱️ Runtime :
1.74 millisecond→1.44 millisecond(best of164runs)📝 Explanation and details
Here’s an optimized version of your program, focusing on removing redundant computations, minimizing branching, and cutting function call overhead. The main improvements.
type(x) in (float, int)when possible, rather thanisinstance(although in some versions,type(x) in ...can be slightly faster).math.isinf/math.isnanif the type isfloat(skip forint, sinceisinf(int)/isnan(int)are always False, so avoid the calls).Key optimization points:
math.isinf/math.isnanon integers (can't ever be true).int,float).This should lead to a measurable speedup, especially for the typical case where the value is a valid
intorfloat.✅ Correctness verification report:
🌀 Generated Regression Tests Details
To edit these changes
git checkout codeflash/optimize-validate_float_arg-max3zw1hand push.