⚡️ Speed up function alias by 153%
#87
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.
📄 153% (1.53x) speedup for
aliasinxarray/core/utils.py⏱️ Runtime :
113 microseconds→44.9 microseconds(best of22runs)📝 Explanation and details
The optimized version achieves a 152% speedup through two key optimizations:
1. String Memoization with LRU Cache
The optimized code introduces
_cached_alias_message()with@functools.lru_cache(maxsize=None)to cache the deprecation message strings. Since thealias()function is likely called repeatedly with the sameold_name/new_namecombinations during application startup or module loading, this eliminates redundant string formatting operations. The f-stringf"{old_name} has been deprecated. Use {new_name} instead."only gets computed once per unique parameter combination.2. Lazy Import of
alias_warningThe import of
alias_warningis moved inside the wrapper function, converting it from a module-level import to a lazy import. This reduces the initial overhead when thealias()function is created, as the import only happens when the deprecated function is actually called.Performance Impact Analysis:
The test results show consistent 160-240% speedups across all test cases, with the most significant gains in basic wrapper creation scenarios. This suggests the optimization primarily benefits the function creation phase rather than execution. The caching is particularly effective when multiple aliases are created for the same function names, as shown in
test_alias_different_old_nameswhere the second call shows a 248% speedup.Workload Suitability:
This optimization is most beneficial for:
The optimization maintains full backward compatibility while significantly reducing the overhead of creating deprecated function wrappers.
✅ Correctness verification report:
⚙️ Existing Unit Tests and Runtime
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-alias-mj9tcnbyand push.