⚡️ Speed up function bit_not by 12%
#109
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.
📄 12% (0.12x) speedup for
bit_notinaerospike_helpers/operations/bitwise_operations.py⏱️ Runtime :
1.43 milliseconds→1.28 milliseconds(best of5runs)📝 Explanation and details
The optimization achieves an 11% runtime improvement by caching the
aerospike.OP_BIT_NOTconstant at module level as_OP_BIT_NOT, eliminating repeated attribute lookups on every function call.What changed:
_OP_BIT_NOT = aerospike.OP_BIT_NOTthat caches the operation type_OP_BIT_NOTinstead ofaerospike.OP_BIT_NOTWhy this is faster:
In Python, attribute access (
aerospike.OP_BIT_NOT) requires traversing the module's namespace dictionary on each invocation. This lookup has measurable overhead, especially in tight loops or frequently called functions. The line profiler shows the attribute lookup line consuming 15.8% of the function's time (1.97ms out of 12.47ms total).By caching the constant at module import time and using a local name lookup instead, we eliminate the attribute access overhead. Python's LOAD_GLOBAL operation (for
_OP_BIT_NOT) is significantly faster than LOAD_ATTR (foraerospike.OP_BIT_NOT) because it avoids the extra level of indirection.Performance characteristics:
The optimization shows consistent improvements across all test cases:
Real-world impact:
This function is likely called in hot paths for constructing Aerospike bitwise operations. Since it's a dictionary-building helper that's designed to be called repeatedly when building operation lists, even modest per-call savings compound significantly. The optimization is particularly valuable when:
The change is purely internal (caching strategy) with no API or behavioral changes, making it safe and backward-compatible.
✅ Correctness verification report:
⚙️ Click to see Existing Unit Tests
test_bitwise_operations.py::TestBitwiseOperations.test_bit_nottest_bitwise_operations.py::TestBitwiseOperations.test_bit_not_bad_argtest_bitwise_operations.py::TestBitwiseOperations.test_bit_not_bad_bin_nametest_bitwise_operations.py::TestBitwiseOperations.test_bit_not_bit_size_too_largetest_bitwise_operations.py::TestBitwiseOperations.test_bit_not_offset_out_of_rangetest_bitwise_operations.py::TestBitwiseOperations.test_bit_not_small_bit_size🌀 Click to see Generated Regression Tests
To edit these changes
git checkout codeflash/optimize-bit_not-ml0k3tk2and push.