⚡️ Speed up function __getattr__
by 7%
#13
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.
📄 7% (0.07x) speedup for
__getattr__
inquantecon/filter.py
⏱️ Runtime :
677 microseconds
→635 microseconds
(best of178
runs)📝 Explanation and details
The optimization introduces message caching to eliminate redundant string formatting operations in the deprecation warning system.
Key changes:
_warn_msg_cache
dictionary to store pre-formatted warning messages_warn_msg_cache.get(name)
Why this improves performance:
The original code performed expensive f-string formatting on every
__getattr__
call (line taking 77.8% of execution time). The optimization moves this cost to first-access only - subsequent calls for the same attribute name retrieve the pre-formatted message from the cache in O(1) time.Performance characteristics:
test_getattr_performance_many_calls
(6.79% faster) where the same attribute is accessed 500 timesThis optimization is most effective for codebases that trigger the same deprecated attribute warnings multiple times, which is common in real-world usage patterns where deprecated APIs are called repeatedly.
✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-__getattr__-mggty2bv
and push.