Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Jan 30, 2026

📄 15% (0.15x) speedup for bit_set_int in aerospike_helpers/operations/bitwise_operations.py

⏱️ Runtime : 397 microseconds 346 microseconds (best of 5 runs)

📝 Explanation and details

The optimization achieves a 14% runtime improvement (397μs → 346μs) by eliminating repeated attribute lookups on every function call.

What changed:
The code now caches aerospike.OP_BIT_SET_INT as a module-level constant _OP_BIT_SET_INT instead of looking it up from the aerospike module each time bit_set_int() is called.

Why this is faster:
In Python, attribute access (like aerospike.OP_BIT_SET_INT) involves dictionary lookups in the module's namespace on every access. By pre-computing this value once at module import time, the function uses a simple local variable lookup instead—a significantly cheaper operation. The line profiler confirms this: the dictionary construction line that previously showed 474522ns for the OP_KEY assignment now shows only 449181ns, a measurable reduction in overhead.

Performance characteristics:
The test results show consistent improvements across all test cases:

  • Simple calls: 17-32% faster (e.g., test_bin_name_is_stored_correctly: 1.91μs → 1.44μs)
  • Sequential calls benefit from the optimization on every iteration (e.g., 100 sequential calls: 45.4μs → 41.4μs, 9.7% faster)
  • The improvement scales well with call frequency—functions called in tight loops or hot paths see compounding benefits

Impact on workloads:
Since bit_set_int() is a helper that constructs operation dictionaries for Aerospike database operations, it's likely called frequently when building batch operations or in data processing pipelines. The 14% speedup means less overhead when constructing large numbers of bitwise operations, making it particularly valuable in high-throughput scenarios where operations are assembled in bulk.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 143 Passed
🌀 Generated Regression Tests 684 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Tests Coverage 100.0%
⚙️ Click to see Existing Unit Tests
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_bitwise_operations.py::TestBitwiseOperations.test_bit_set_int 12.1μs 9.43μs 27.9%✅
🌀 Click to see Generated Regression Tests
import random
import sys
import types

# function to test
import aerospike
# imports
import pytest  # used for our unit tests
from aerospike_helpers.operations.bitwise_operations import bit_set_int

# Create a minimal "aerospike" module in sys.modules so that the function's import aerospike works
# This preserves the original import semantics while avoiding a dependency on the real aerospike package.
_fake_aerospike = types.ModuleType("aerospike")

BIN_KEY = "bin"
BIT_OFFSET_KEY = "bit_offset"
BIT_SIZE_KEY = "bit_size"
VALUE_KEY = "value"
OP_KEY = "op"
POLICY_KEY = "policy"

def test_basic_return_structure_and_values():
    # Basic test: ensure the returned dict contains the expected keys and values for common inputs.
    bin_name = "my_bin"
    bit_offset = 10
    bit_size = 16
    value = 0x1234
    # No policy passed -> should default to None in return
    codeflash_output = bit_set_int(bin_name, bit_offset, bit_size, value); op = codeflash_output # 1.83μs -> 1.49μs (22.8% faster)

    # The dict must contain exactly the keys defined by the implementation
    expected_keys = {OP_KEY, BIN_KEY, POLICY_KEY, BIT_OFFSET_KEY, BIT_SIZE_KEY, VALUE_KEY}

def test_policy_reference_preserved_and_mutable_after_call():
    # Edge: verify that when we pass a policy dict, the returned dict stores the same object reference.
    policy = {"some": "option"}
    codeflash_output = bit_set_int("b", 0, 8, 1, policy); op = codeflash_output # 1.76μs -> 1.36μs (29.4% faster)

    # Mutating the original policy after calling function should be visible through the returned dict.
    policy["new"] = "value"

def test_various_bin_name_types_and_preservation():
    # Although the annotation suggests bin_name is a string, the function does no validation.
    # Ensure the function preserves whatever is passed through BIN_KEY.
    for bin_name in ("", "含有unicode", 123, None, b"bytes"):
        codeflash_output = bit_set_int(bin_name, 0, 1, 0); op = codeflash_output # 4.20μs -> 3.62μs (16.0% faster)

def test_edge_bit_size_and_offsets_do_not_get_modified():
    # Edge cases: bit_size of 0, negative bit_size, bit_size > 64, negative offsets.
    test_cases = [
        {"bit_offset": 0, "bit_size": 0},
        {"bit_offset": -1, "bit_size": 8},
        {"bit_offset": 5, "bit_size": 128},
        {"bit_offset": -1024, "bit_size": -64},
    ]
    for case in test_cases:
        codeflash_output = bit_set_int("bin", case["bit_offset"], case["bit_size"], 0); op = codeflash_output # 3.60μs -> 3.05μs (17.9% faster)

def test_value_signed_64_bit_boundary_values_preserved():
    # Edge: test minimum and maximum signed 64-bit integer values and a value outside the range.
    min_signed_64 = -(2 ** 63)
    max_signed_64 = 2 ** 63 - 1
    outside_positive = 2 ** 63  # one beyond signed 64-bit
    outside_negative = -(2 ** 63) - 1  # one beyond negative signed 64-bit

    for val in (min_signed_64, max_signed_64, outside_positive, outside_negative):
        codeflash_output = bit_set_int("bin", 0, 64, val); op = codeflash_output # 3.18μs -> 2.82μs (12.8% faster)

def test_returned_dict_is_fresh_each_call_and_immutable_identity_of_constant():
    # Ensure that each call returns a new dict instance (no accidental reuse).
    args = ("bin", 1, 8, 255, {"a": 1})
    codeflash_output = bit_set_int(*args); op1 = codeflash_output # 1.79μs -> 1.39μs (28.8% faster)
    codeflash_output = bit_set_int(*args); op2 = codeflash_output # 766ns -> 652ns (17.5% faster)

def test_large_scale_many_calls_deterministic_and_complete():
    # Large scale: perform many calls with varied parameters to ensure stability and deterministic behavior.
    # Use a fixed seed to keep the test deterministic.
    rng = random.Random(0)
    num_calls = 500  # under the 1000-step guideline
    results = []

    # Create varied inputs but keep the total number under limits.
    for i in range(num_calls):
        bin_name = f"bin_{rng.randint(0, 100)}"
        bit_offset = rng.randint(-1000, 1000)
        # vary bit_size including 0..128 to exercise boundaries
        bit_size = rng.randint(0, 128)
        # use a mix of small and large integers, including negatives
        value = rng.randint(-(2 ** 63), 2 ** 63 - 1)
        policy = {"iteration": i} if (i % 10 == 0) else None
        codeflash_output = bit_set_int(bin_name, bit_offset, bit_size, value, policy); op = codeflash_output # 236μs -> 208μs (13.2% faster)
        results.append((bin_name, bit_offset, bit_size, value, policy, op))
    for bin_name, bit_offset, bit_size, value, policy, op in results:
        pass
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import aerospike
# imports
import pytest
from aerospike_helpers.operations.bitwise_operations import bit_set_int

class TestBasicFunctionality:
    """Tests for basic functionality of bit_set_int function"""

    def test_basic_operation_with_simple_parameters(self):
        """Test that bit_set_int returns a properly structured dictionary with basic inputs"""
        codeflash_output = bit_set_int("my_bin", 0, 8, 255); result = codeflash_output # 1.89μs -> 1.51μs (25.5% faster)

    def test_bin_name_is_stored_correctly(self):
        """Test that the bin name is correctly stored in the returned dictionary"""
        bin_name = "test_bin"
        codeflash_output = bit_set_int(bin_name, 0, 8, 100); result = codeflash_output # 1.91μs -> 1.44μs (32.2% faster)

    def test_bit_offset_is_stored_correctly(self):
        """Test that the bit offset is correctly stored"""
        bit_offset = 16
        codeflash_output = bit_set_int("bin", bit_offset, 8, 100); result = codeflash_output # 1.77μs -> 1.44μs (23.0% faster)

    def test_bit_size_is_stored_correctly(self):
        """Test that the bit size is correctly stored"""
        bit_size = 32
        codeflash_output = bit_set_int("bin", 0, bit_size, 100); result = codeflash_output # 1.78μs -> 1.40μs (27.3% faster)

    def test_value_is_stored_correctly(self):
        """Test that the value is correctly stored"""
        value = 12345
        codeflash_output = bit_set_int("bin", 0, 8, value); result = codeflash_output # 1.73μs -> 1.37μs (26.1% faster)

    def test_operation_type_is_correct(self):
        """Test that the operation type is set to OP_BIT_SET_INT"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result = codeflash_output # 1.74μs -> 1.39μs (24.5% faster)

    def test_policy_is_none_by_default(self):
        """Test that policy defaults to None when not provided"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result = codeflash_output # 1.57μs -> 1.34μs (17.4% faster)

    def test_policy_is_stored_when_provided(self):
        """Test that policy is correctly stored when provided"""
        policy = {"write_flags": 3}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=policy); result = codeflash_output # 2.14μs -> 1.93μs (10.8% faster)

class TestEdgeCases:
    """Tests for edge cases and boundary conditions"""

    def test_zero_bit_offset(self):
        """Test with bit offset of 0 (minimum valid offset)"""
        codeflash_output = bit_set_int("bin", 0, 8, 255); result = codeflash_output # 1.76μs -> 1.43μs (22.7% faster)

    def test_large_bit_offset(self):
        """Test with a large bit offset value"""
        large_offset = 1000000
        codeflash_output = bit_set_int("bin", large_offset, 8, 100); result = codeflash_output # 1.75μs -> 1.36μs (28.3% faster)

    def test_single_bit_size(self):
        """Test with minimum bit size of 1"""
        codeflash_output = bit_set_int("bin", 0, 1, 1); result = codeflash_output # 1.66μs -> 1.31μs (26.7% faster)

    def test_maximum_bit_size_64(self):
        """Test with maximum bit size of 64 bits"""
        codeflash_output = bit_set_int("bin", 0, 64, 9223372036854775807); result = codeflash_output # 1.67μs -> 1.37μs (22.0% faster)

    def test_value_zero(self):
        """Test with value of 0"""
        codeflash_output = bit_set_int("bin", 0, 8, 0); result = codeflash_output # 1.63μs -> 1.34μs (21.2% faster)

    def test_positive_value(self):
        """Test with a positive value"""
        value = 1000000
        codeflash_output = bit_set_int("bin", 0, 32, value); result = codeflash_output # 1.64μs -> 1.28μs (28.5% faster)

    def test_negative_value(self):
        """Test with a negative signed integer value"""
        value = -100
        codeflash_output = bit_set_int("bin", 0, 32, value); result = codeflash_output # 1.59μs -> 1.32μs (20.4% faster)

    def test_maximum_signed_64bit_value(self):
        """Test with maximum 64-bit signed integer"""
        max_value = 9223372036854775807  # 2^63 - 1
        codeflash_output = bit_set_int("bin", 0, 64, max_value); result = codeflash_output # 1.67μs -> 1.31μs (27.4% faster)

    def test_minimum_signed_64bit_value(self):
        """Test with minimum 64-bit signed integer"""
        min_value = -9223372036854775808  # -2^63
        codeflash_output = bit_set_int("bin", 0, 64, min_value); result = codeflash_output # 1.60μs -> 1.37μs (17.1% faster)

    def test_empty_bin_name(self):
        """Test with an empty string as bin name"""
        codeflash_output = bit_set_int("", 0, 8, 100); result = codeflash_output # 1.69μs -> 1.39μs (21.7% faster)

    def test_bin_name_with_special_characters(self):
        """Test with bin name containing special characters"""
        bin_name = "bin_@#$%_test"
        codeflash_output = bit_set_int(bin_name, 0, 8, 100); result = codeflash_output # 1.58μs -> 1.33μs (18.7% faster)

    def test_very_long_bin_name(self):
        """Test with an extremely long bin name"""
        bin_name = "a" * 1000
        codeflash_output = bit_set_int(bin_name, 0, 8, 100); result = codeflash_output # 1.68μs -> 1.37μs (22.9% faster)

    def test_empty_policy_dict(self):
        """Test with an empty policy dictionary"""
        policy = {}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=policy); result = codeflash_output # 2.04μs -> 1.91μs (7.29% faster)

    def test_complex_policy_dict(self):
        """Test with a complex policy dictionary"""
        policy = {
            "write_flags": 3,
            "read_flags": 1,
            "timeout": 1000,
            "retry": 2
        }
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=policy); result = codeflash_output # 2.10μs -> 1.85μs (13.1% faster)

class TestReturnValueStructure:
    """Tests to verify the structure and integrity of returned dictionary"""

    def test_return_type_is_dict(self):
        """Test that the function always returns a dictionary"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result = codeflash_output # 1.58μs -> 1.33μs (18.5% faster)

    def test_all_required_keys_present(self):
        """Test that all required keys are present in the returned dictionary"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result = codeflash_output # 1.67μs -> 1.35μs (24.0% faster)
        
        required_keys = {"op", "bin", "bit_offset", "bit_size", "value", "policy"}

    def test_no_extra_keys_in_result(self):
        """Test that the result dictionary contains only expected keys"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result = codeflash_output # 1.72μs -> 1.32μs (30.1% faster)
        
        expected_keys = {"op", "bin", "bit_offset", "bit_size", "value", "policy"}

    def test_return_value_immutability_independence(self):
        """Test that multiple calls return independent dictionaries"""
        codeflash_output = bit_set_int("bin", 0, 8, 100); result1 = codeflash_output # 1.70μs -> 1.37μs (24.5% faster)
        codeflash_output = bit_set_int("bin", 0, 8, 100); result2 = codeflash_output # 778ns -> 674ns (15.4% faster)
        
        # Modify result1 and verify result2 is unaffected
        result1["bin"] = "modified"

    def test_operation_key_always_same(self):
        """Test that the operation key value is consistent across calls"""
        codeflash_output = bit_set_int("bin1", 0, 8, 100); result1 = codeflash_output # 1.68μs -> 1.31μs (28.2% faster)
        codeflash_output = bit_set_int("bin2", 100, 32, 500); result2 = codeflash_output # 960ns -> 834ns (15.1% faster)

class TestParameterConsistency:
    """Tests to verify parameter values are correctly preserved in output"""

    def test_all_parameters_match_input(self):
        """Test that all output values match the input parameters"""
        bin_name = "test_bin"
        bit_offset = 32
        bit_size = 16
        value = 12345
        policy = {"flag": 1}
        
        codeflash_output = bit_set_int(bin_name, bit_offset, bit_size, value, policy=policy); result = codeflash_output # 2.17μs -> 1.99μs (9.24% faster)

    def test_parameters_with_different_types_accepted(self):
        """Test that parameters of valid types are accepted"""
        # Test with integer values
        codeflash_output = bit_set_int("bin", 0, 8, 100); result1 = codeflash_output # 1.72μs -> 1.35μs (27.2% faster)
        
        # Test with string bin name
        codeflash_output = bit_set_int("my_bin_name", 0, 8, 100); result2 = codeflash_output # 753ns -> 644ns (16.9% faster)

    def test_operation_independent_of_parameters(self):
        """Test that operation type is independent of parameter values"""
        codeflash_output = bit_set_int("bin", 0, 1, 0); result1 = codeflash_output # 1.62μs -> 1.30μs (24.7% faster)
        codeflash_output = bit_set_int("bin", 1000, 64, -1); result2 = codeflash_output # 857ns -> 863ns (0.695% slower)
        codeflash_output = bit_set_int("bin", 500, 32, 999999); result3 = codeflash_output # 812ns -> 732ns (10.9% faster)

class TestLargeScale:
    """Tests for performance and scalability with large values"""

    def test_large_scale_offset_values(self):
        """Test with very large bit offset values"""
        large_offsets = [1000000, 10000000, 100000000, 1000000000]
        
        for offset in large_offsets:
            codeflash_output = bit_set_int("bin", offset, 8, 100); result = codeflash_output # 3.61μs -> 3.03μs (19.1% faster)

    def test_large_scale_bit_size_values(self):
        """Test with various large bit size values up to maximum"""
        bit_sizes = [1, 8, 16, 32, 48, 56, 64]
        
        for size in bit_sizes:
            codeflash_output = bit_set_int("bin", 0, size, 100); result = codeflash_output # 4.91μs -> 4.21μs (16.6% faster)

    def test_large_scale_value_range(self):
        """Test with various large integer values"""
        values = [
            0,
            256,
            65536,
            16777216,
            4294967296,
            1099511627776,
            281474976710656,
            72057594037927936,
            -1,
            -256,
            -65536,
            -16777216,
            -4294967296,
        ]
        
        for val in values:
            codeflash_output = bit_set_int("bin", 0, 64, val); result = codeflash_output # 7.75μs -> 6.99μs (11.0% faster)

    def test_multiple_sequential_calls(self):
        """Test function stability with multiple sequential calls"""
        # Make 100 sequential calls with different parameters
        for i in range(100):
            codeflash_output = bit_set_int(f"bin_{i}", i * 10, (i % 64) + 1, i * 1000); result = codeflash_output # 45.4μs -> 41.4μs (9.71% faster)

    def test_various_policy_sizes(self):
        """Test with policies of varying complexity and size"""
        # Small policy
        small_policy = {"flag": 1}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=small_policy); result1 = codeflash_output # 2.19μs -> 1.95μs (12.5% faster)
        
        # Medium policy with multiple keys
        medium_policy = {f"key_{i}": i for i in range(10)}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=medium_policy); result2 = codeflash_output # 916ns -> 834ns (9.83% faster)
        
        # Large policy with many keys
        large_policy = {f"key_{i}": f"value_{i}" for i in range(50)}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=large_policy); result3 = codeflash_output # 837ns -> 775ns (8.00% faster)

    def test_consistent_behavior_with_extreme_combinations(self):
        """Test function behavior with extreme parameter combinations"""
        extreme_cases = [
            ("", 0, 1, 0),
            ("x" * 500, 999999999, 64, 9223372036854775807),
            ("test", 1000000, 32, -9223372036854775808),
            ("bin_name", 0, 64, 1099511627776),
        ]
        
        for bin_name, offset, size, value in extreme_cases:
            codeflash_output = bit_set_int(bin_name, offset, size, value); result = codeflash_output # 4.11μs -> 3.60μs (14.2% faster)

    def test_return_dict_keys_with_large_policy(self):
        """Test that dictionary keys remain correct even with large policies"""
        large_policy = {f"nested_{i}": {"sub": i} for i in range(20)}
        codeflash_output = bit_set_int("bin", 0, 8, 100, policy=large_policy); result = codeflash_output # 2.00μs -> 1.86μs (7.80% faster)
        
        expected_keys = {"op", "bin", "bit_offset", "bit_size", "value", "policy"}
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from aerospike_helpers.operations.bitwise_operations import bit_set_int

def test_bit_set_int():
    bit_set_int('', 0, 0, 0, policy={})
🔎 Click to see Concolic Coverage Tests
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_hw2hs1n8/tmp97fqfe67/test_concolic_coverage.py::test_bit_set_int 2.70μs 2.12μs 27.2%✅

To edit these changes git checkout codeflash/optimize-bit_set_int-ml0ijb72 and push.

Codeflash Static Badge

The optimization achieves a **14% runtime improvement** (397μs → 346μs) by eliminating repeated attribute lookups on every function call. 

**What changed:**
The code now caches `aerospike.OP_BIT_SET_INT` as a module-level constant `_OP_BIT_SET_INT` instead of looking it up from the `aerospike` module each time `bit_set_int()` is called.

**Why this is faster:**
In Python, attribute access (like `aerospike.OP_BIT_SET_INT`) involves dictionary lookups in the module's namespace on every access. By pre-computing this value once at module import time, the function uses a simple local variable lookup instead—a significantly cheaper operation. The line profiler confirms this: the dictionary construction line that previously showed 474522ns for the `OP_KEY` assignment now shows only 449181ns, a measurable reduction in overhead.

**Performance characteristics:**
The test results show consistent improvements across all test cases:
- Simple calls: 17-32% faster (e.g., `test_bin_name_is_stored_correctly`: 1.91μs → 1.44μs)
- Sequential calls benefit from the optimization on every iteration (e.g., 100 sequential calls: 45.4μs → 41.4μs, 9.7% faster)
- The improvement scales well with call frequency—functions called in tight loops or hot paths see compounding benefits

**Impact on workloads:**
Since `bit_set_int()` is a helper that constructs operation dictionaries for Aerospike database operations, it's likely called frequently when building batch operations or in data processing pipelines. The 14% speedup means less overhead when constructing large numbers of bitwise operations, making it particularly valuable in high-throughput scenarios where operations are assembled in bulk.
@codeflash-ai codeflash-ai bot requested a review from aseembits93 January 30, 2026 06:39
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants