⚡️ Speed up function _ensure_term by 5%
#105
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
_ensure_terminpandas/io/pytables.py⏱️ Runtime :
20.7 milliseconds→19.7 milliseconds(best of57runs)📝 Explanation and details
The optimization implements lazy initialization with module-level caching for the operations tuple in
maybe_expression().Key Changes:
_operationsvariable that's initialized toNonemaybe_expression()to check if_operationsisNoneand only create the operations tuple on first callWhy This is Faster:
The original code reconstructed the operations tuple (
PyTablesExprVisitor.binary_ops + PyTablesExprVisitor.unary_ops + ("=",)) on every call tomaybe_expression(). This tuple concatenation involves memory allocation and copying. The optimization eliminates this overhead by computing it once and reusing the result.Performance Impact:
maybe_expression()The 5% overall speedup comes from eliminating redundant tuple creation overhead across the many calls to
maybe_expression()during pytables query processing.✅ Correctness verification report:
🌀 Generated Regression Tests and Runtime
To edit these changes
git checkout codeflash/optimize-_ensure_term-mhc076v5and push.