⚡️ Speed up function bit_get_int by 17%
#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.
📄 17% (0.17x) speedup for
bit_get_intinaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
608 microseconds→520 microseconds(best of5runs)📝 Explanation and details
The optimization achieves a 17% runtime improvement by eliminating repeated attribute lookups of
aerospike.OP_BIT_GET_INT.Key Change:
The constant
aerospike.OP_BIT_GET_INTis cached at module-level as_OP_BIT_GET_INT, transforming what was previously an attribute lookup on every function call into a simple local variable reference.Why This Works:
In Python, attribute lookups (like
aerospike.OP_BIT_GET_INT) involve dictionary searches in the module's__dict__at runtime. By caching this constant value once at module import time, each call tobit_get_int()avoids this lookup overhead. While a single attribute lookup is fast, when a function is called repeatedly (as evidenced by the 1,314 hits in the profiler and the bulk operation tests with 100-500 iterations), these microseconds accumulate significantly.Performance Impact:
The line profiler shows the dictionary construction line improved from 844,072 ns to 799,935 ns (5% faster on that line alone). More importantly, the test results demonstrate consistent 10-40% improvements per call, with the most dramatic gains in:
Workload Suitability:
This optimization is particularly effective for:
bit_get_int()is called hundreds of timesThe optimization maintains identical behavior—all dictionary values, types, and structure are preserved—while providing measurable runtime reduction through a simple constant caching strategy.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_get_inttest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_accross_bytestest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_bad_argument_typetest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_bad_bin_nametest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_bit_offset_out_of_rangetest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_bit_size_too_largetest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_fraction_of_bytetest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_multiple_bytestest_bitwise_operations.py::TestBitwiseOperations.test_bit_get_int_signed🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_get_int-ml0ja6yland push.