⚡️ Speed up function bit_lshift by 13%
#108
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.
📄 13% (0.13x) speedup for
bit_lshiftinaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
235 microseconds→208 microseconds(best of5runs)📝 Explanation and details
The optimization achieves a 13% runtime improvement by eliminating a repeated attribute lookup on each function call.
Key Change:
The optimized code caches the constant
aerospike.OP_BIT_LSHIFTinto a module-level variable_OP_BIT_LSHIFTat import time, rather than looking it up from theaerospikemodule on every function invocation.Why This Works:
In Python, attribute lookups (like
aerospike.OP_BIT_LSHIFT) require dictionary lookups in the module's namespace on each access. While individual lookups are fast, they add measurable overhead when the function is called repeatedly. By caching the constant value once at module load time, we convert what was previously anLOAD_ATTRbytecode operation into a simplerLOAD_GLOBALoperation on each call.The line profiler data confirms this: the line with
OP_KEY: aerospike.OP_BIT_LSHIFTtook 270,949ns in the original vs 248,493ns in the optimized version - a 22,456ns (8.3%) improvement on that single line alone.Performance Characteristics:
Based on the annotated tests, this optimization provides consistent speedups across all test cases:
Impact Assessment:
Since
bit_lshiftis a helper function that creates operation dictionaries for Aerospike bitwise operations, it's likely called in data processing pipelines or batch operations where the cumulative effect of this micro-optimization becomes significant. Even modest 13% runtime improvements can meaningfully reduce latency in hot paths involving multiple bitwise operations.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_lshifttest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_across_bytestest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_bad_argtest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_bad_bin_nametest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_bit_size_too_largetest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_offset_out_of_rangetest_bitwise_operations.py::TestBitwiseOperations.test_bit_lshift_wrap🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_lshift-ml0jv7k9and push.