⚡️ Speed up function bit_rscan by 5%
#111
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
bit_rscaninaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
988 microseconds→938 microseconds(best of5runs)📝 Explanation and details
The optimization achieves a 5% runtime improvement by eliminating a repeated attribute lookup on every function call.
What changed:
The code now caches
aerospike.OP_BIT_RSCANas a module-level constant_OP_BIT_RSCANinstead of performing an attribute lookup through theaerospikemodule on every invocation ofbit_rscan().Why this is faster:
In Python, accessing an attribute like
aerospike.OP_BIT_RSCANrequires traversing the module's namespace dictionary at runtime. By caching this constant value once at module import time, the function can now access it as a simple module-level global variable, which is significantly faster. Line profiler data confirms this: the operation assignment line dropped from 620.3ns to 617.1ns per hit, and the overall function runtime decreased from 988μs to 938μs.Performance characteristics:
The test results show consistent improvements across all test cases, with gains ranging from 0.6% to 32.6% depending on the specific parameter combinations. The optimization is particularly effective for:
test_batch_creation_of_many_operations_is_correct_and_efficient: 4% faster, andtest_bit_rscan_bulk_creation_performance: 5% faster)Why this matters:
The
bit_rscan()function is a factory method for creating operation dictionaries. If this function is called in tight loops when building batch operations (common in Aerospike workflows), the 5% improvement compounds significantly. For applications performing hundreds or thousands of bitwise operations, this translates to measurable latency reductions with zero behavioral changes or trade-offs.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_rscantest_bitwise_operations.py::TestBitwiseOperations.test_bit_rscan_across_bytestest_bitwise_operations.py::TestBitwiseOperations.test_bit_rscan_bad_bin_nametest_bitwise_operations.py::TestBitwiseOperations.test_bit_rscan_bit_size_too_largetest_bitwise_operations.py::TestBitwiseOperations.test_bit_rscan_offset_out_of_rangetest_bitwise_operations.py::TestBitwiseOperations.test_bit_rscan_value_not_found🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_rscan-ml0kfod0and push.