⚡️ Speed up function bit_resize by 14%
#101
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.
📄 14% (0.14x) speedup for
bit_resizeinaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
1.25 milliseconds→1.10 milliseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 13% runtime improvement by reducing dictionary construction overhead through two key changes:
1. Pre-constructed Base Dictionary (
_BIT_RESIZE_BASE)The optimization creates a module-level base dictionary containing the static
"op"key at import time. On each function call, this base is copied and populated with the dynamic values. This is faster than constructing a new dictionary from scratch because:dict.copy()method is implemented in C and optimized for shallow copying, making it faster than building a dictionary with 5 key-value pairs from scratch"op"key doesn't need to be re-evaluated on every call2. Direct String Literals Instead of Module Constants
The original code uses module-level constants (
OP_KEY,BIN_KEY, etc.) as dictionary keys, requiring attribute lookups on each access. The optimized version uses string literals directly ("bin","policy", etc.), eliminating these lookups. While subtle, this saves a few nanoseconds per key access.Performance Characteristics Based on Test Results:
Why This Works:
Python's dictionary construction involves multiple steps: allocating memory, hashing keys, and inserting key-value pairs. By pre-allocating the base structure and using the optimized
copy()method, we reduce this overhead. The line profiler shows the original's dictionary literal construction took ~24% of total time just for the opening brace, while the optimized version'scopy()operation is faster and more predictable.This optimization is particularly valuable if
bit_resizeis called frequently in data processing pipelines or batch operations against Aerospike, where the cumulative savings across thousands of calls become significant.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_create_only_allows_createtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_create_only_prevents_updatetest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_default_allows_createtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_defaultstest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_from_fronttest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_grow_only_allows_growtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_grow_only_does_not_allow_shrinktest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_partial_no_failtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_shrink_from_fronttest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_shrink_only_allows_shrinktest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_shrink_only_does_not_allow_growtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_shrink_removes_from_endtest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_update_only_allows_updatetest_bitwise_operations.py::TestBitwiseOperations.test_bit_resize_update_only_prevents_create🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_resize-ml0i44b4and push.