⚡️ Speed up function bit_or by 7%
#110
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.
📄 7% (0.07x) speedup for
bit_orinaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
307 microseconds→287 microseconds(best of5runs)📝 Explanation and details
The optimized code achieves a 6% runtime improvement by caching the constant
aerospike.OP_BIT_ORas a module-level variable_OP_BIT_OR. This eliminates repeated attribute lookups on theaerospikemodule object during each function call.Key Change:
_OP_BIT_OR = aerospike.OP_BIT_ORat module levelOP_KEY: aerospike.OP_BIT_ORtoOP_KEY: _OP_BIT_ORin the returned dictionaryWhy This Improves Performance:
In Python, attribute access (like
aerospike.OP_BIT_OR) requires a dictionary lookup in the module's__dict__at runtime. By caching this value once at module load time, each invocation ofbit_or()avoids the attribute lookup overhead. The line profiler shows this optimization reduces time spent on theOP_KEYline from 315,754ns to 302,361ns (4% faster for that line alone).Test Results Analysis:
The optimization provides consistent improvements across all test cases:
test_basic_with_bytes: 52% faster)bit_or()is called repeatedlyImpact:
This optimization is particularly valuable when
bit_or()is called frequently in hot paths or batch operations. The 6% overall speedup compounds significantly in scenarios involving hundreds or thousands of bitwise operations, as demonstrated by the large-scale test case showing consistent gains at scale.✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_ortest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_bad_argtest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_bad_bin_nametest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_bit_offset_out_of_rangetest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_bit_size_larger_than_valuetest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_bit_size_too_largetest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_multiple_bytestest_bitwise_operations.py::TestBitwiseOperations.test_bit_or_multiple_bytes_value_unchanged🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_or-ml0k9u1rand push.