⚡️ Speed up function _replace_locals by 10%
#93
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.
📄 10% (0.10x) speedup for
_replace_localsinpandas/core/computation/expr.py⏱️ Runtime :
21.7 microseconds→19.7 microseconds(best of132runs)📝 Explanation and details
The optimization introduces module-level constant caching by pre-binding
tokenize.OPandLOCAL_TAGto local variables_TOKENIZE_OPand_LOCAL_TAG. This eliminates repeated attribute lookups during function execution.Key changes:
_TOKENIZE_OP = tokenize.OPand_LOCAL_TAG = LOCAL_TAGtokenize.OPandLOCAL_TAGwithin the function with the cached versionsWhy this speeds up the code:
In Python, accessing module attributes like
tokenize.OPrequires dictionary lookups in the module's namespace on every access. By caching these values as module-level variables, we convert expensive attribute lookups into faster local variable access. This is particularly effective for frequently called functions where the same constants are accessed repeatedly.The line profiler shows the optimization reduces time spent on both the conditional check (line with
if toknum == _TOKENIZE_OP) and the return statement, resulting in a 10% overall speedup.Test case performance patterns:
@replacement path or involve non-OP token types, where both cached constants are accessed✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_replace_locals-mhbnght3and push.