⚡️ Speed up function removesuffix by 5%
#201
Open
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.
📄 5% (0.05x) speedup for
removesuffixinkeras/src/utils/python_utils.py⏱️ Runtime :
69.5 microseconds→66.1 microseconds(best of224runs)📝 Explanation and details
The optimization eliminates a redundant
len(suffix)call by computing it once and storing the result. In the original code,len(suffix)was called twice when a suffix exists and matches: once in the conditional check and again when slicing the string. The optimized version calculatessuffix_len = len(suffix)upfront and reuses this value.The restructured control flow also avoids calling
x.endswith(suffix)when the suffix is empty (length 0), since an empty suffix would never match anyway. This provides a small performance benefit for empty suffix cases.Based on the test results, the optimization shows consistent improvements across most scenarios:
len()call elimination has the most impactGiven the function references, this optimization is particularly valuable since
removesuffixis called within layer building operations in Keras, specifically for processing argument names with_shapesuffixes. In neural network frameworks, layer building can happen frequently during model construction, making even small optimizations worthwhile in the aggregate.The 5% overall speedup may seem modest, but for a utility function that's likely called many times during model setup and potentially in training loops, this optimization provides measurable performance benefits without any change to the API or behavior.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-removesuffix-mja6gyljand push.