Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Nov 1, 2025

📄 46% (0.46x) speedup for annotation_params_for_rect in plotly/shapeannotation.py

⏱️ Runtime : 2.15 milliseconds 1.47 milliseconds (best of 154 runs)

📝 Explanation and details

The optimized code achieves a 46% speedup through several key optimizations:

1. Precomputed frozenset constants for position matching

  • Replaced repeated set(["inside", "top", "left"]) constructions with precomputed frozenset constants like _INSIDE_TOP_LEFT
  • Eliminates the overhead of creating new sets on every comparison, as seen in the line profiler where position comparisons now run much faster

2. Optimized _mean function for the common two-element case

  • Added fast path: return (x[0] + x[1]) / 2 if n == 2 else float(sum(x)) / n
  • Since coordinates are typically pairs (x0,x1) or (y0,y1), this avoids sum() and float() conversion overhead for the most common case

3. Inlined position preprocessing logic

  • Replaced the _add_inside_to_position() function call with direct inline logic in _prepare_position
  • Eliminates function call overhead and import dependencies

4. Streamlined min/max operations

  • Changed min([x0, x1]) to min(x0, x1) and max([y0, y1]) to max(y0, y1)
  • Avoids creating temporary lists for simple two-argument min/max operations

Performance characteristics:

  • Inside positions show 23-58% improvements due to faster set comparisons and optimized _mean
  • Outside positions show 38-64% improvements, benefiting most from the precomputed frozensets
  • Large-scale operations (like the 1000-call test) see 43% improvement, demonstrating the cumulative effect
  • Error cases are 55-70% faster due to reduced overhead in position parsing

These optimizations are particularly effective for plotting libraries where annotation positioning is called frequently with predictable position patterns.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 1697 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
import pytest  # used for our unit tests
from plotly.shapeannotation import annotation_params_for_rect

# unit tests

# ---------------------- BASIC TEST CASES ----------------------

def test_inside_top_left_basic():
    # Test inside top left placement
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top left"); result = codeflash_output # 3.52μs -> 2.85μs (23.7% faster)

def test_inside_top_right_basic():
    # Test inside top right placement
    shape_args = {"x0": 1, "x1": 5, "y0": 2, "y1": 8}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top right"); result = codeflash_output # 3.25μs -> 2.62μs (24.2% faster)

def test_inside_bottom_left_basic():
    # Test inside bottom left placement
    shape_args = {"x0": 3, "x1": 7, "y0": 4, "y1": 9}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom left"); result = codeflash_output # 3.52μs -> 2.61μs (34.7% faster)

def test_inside_bottom_right_basic():
    # Test inside bottom right placement
    shape_args = {"x0": 2, "x1": 6, "y0": 1, "y1": 11}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom right"); result = codeflash_output # 3.55μs -> 2.60μs (36.6% faster)

def test_inside_top_basic():
    # Test inside top placement (centered horizontally)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top"); result = codeflash_output # 3.96μs -> 2.80μs (41.6% faster)

def test_inside_bottom_basic():
    # Test inside bottom placement (centered horizontally)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom"); result = codeflash_output # 3.91μs -> 2.79μs (39.9% faster)

def test_inside_left_basic():
    # Test inside left placement (centered vertically)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "left"); result = codeflash_output # 3.92μs -> 2.77μs (41.4% faster)

def test_inside_right_basic():
    # Test inside right placement (centered vertically)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "right"); result = codeflash_output # 4.02μs -> 2.69μs (49.3% faster)

def test_inside_center_basic():
    # Test inside center placement
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, None); result = codeflash_output # 3.30μs -> 2.56μs (29.1% faster)

def test_inside_center_explicit():
    # Test inside center placement with explicit "inside"
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside"); result = codeflash_output # 4.05μs -> 2.56μs (58.4% faster)

# ---------------------- EDGE TEST CASES ----------------------

def test_x0_equals_x1():
    # Test when x0 equals x1 (zero width)
    shape_args = {"x0": 5, "x1": 5, "y0": 2, "y1": 8}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top right"); result = codeflash_output # 3.18μs -> 2.48μs (28.5% faster)

def test_y0_equals_y1():
    # Test when y0 equals y1 (zero height)
    shape_args = {"x0": 2, "x1": 6, "y0": 7, "y1": 7}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom left"); result = codeflash_output # 3.46μs -> 2.47μs (39.8% faster)

def test_x0_greater_than_x1():
    # Test when x0 > x1 (reverse direction)
    shape_args = {"x0": 10, "x1": 0, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top left"); result = codeflash_output # 2.96μs -> 2.39μs (23.9% faster)

def test_y0_greater_than_y1():
    # Test when y0 > y1 (reverse direction)
    shape_args = {"x0": 0, "x1": 10, "y0": 20, "y1": 0}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom right"); result = codeflash_output # 3.54μs -> 2.61μs (35.3% faster)

def test_negative_coordinates():
    # Test with negative coordinates
    shape_args = {"x0": -5, "x1": 5, "y0": -10, "y1": 10}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top left"); result = codeflash_output # 2.89μs -> 2.52μs (15.1% faster)

def test_float_coordinates():
    # Test with float coordinates
    shape_args = {"x0": 1.5, "x1": 4.5, "y0": 2.25, "y1": 8.75}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top"); result = codeflash_output # 3.89μs -> 2.88μs (35.2% faster)


def test_invalid_position_raises():
    # Test invalid position raises ValueError
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    with pytest.raises(ValueError):
        annotation_params_for_rect("rect", shape_args, "foobar") # 5.63μs -> 3.62μs (55.4% faster)

def test_missing_shape_args_keys():
    # Test missing keys in shape_args raises KeyError
    shape_args = {"x0": 0, "x1": 10, "y0": 0}  # missing y1
    with pytest.raises(KeyError):
        annotation_params_for_rect("rect", shape_args, "top left") # 825ns -> 799ns (3.25% faster)

def test_outside_top_left_vrect():
    # Test outside top left for vrect shape_type
    shape_args = {"x0": 2, "x1": 8, "y0": 1, "y1": 9}
    codeflash_output = annotation_params_for_rect("vrect", shape_args, "outside top left"); result = codeflash_output # 5.55μs -> 4.01μs (38.6% faster)

def test_outside_top_right_hrect():
    # Test outside top right for hrect shape_type
    shape_args = {"x0": 2, "x1": 8, "y0": 1, "y1": 9}
    codeflash_output = annotation_params_for_rect("hrect", shape_args, "outside top right"); result = codeflash_output # 4.78μs -> 3.16μs (51.2% faster)

def test_outside_bottom_left_vrect():
    # Test outside bottom left for vrect shape_type
    shape_args = {"x0": 2, "x1": 8, "y0": 1, "y1": 9}
    codeflash_output = annotation_params_for_rect("vrect", shape_args, "outside bottom left"); result = codeflash_output # 4.80μs -> 3.20μs (49.9% faster)

def test_outside_bottom_right_hrect():
    # Test outside bottom right for hrect shape_type
    shape_args = {"x0": 2, "x1": 8, "y0": 1, "y1": 9}
    codeflash_output = annotation_params_for_rect("hrect", shape_args, "outside bottom right"); result = codeflash_output # 4.80μs -> 3.14μs (52.9% faster)

def test_outside_top_basic():
    # Test outside top placement (centered horizontally, bottom anchor)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "outside top"); result = codeflash_output # 5.26μs -> 3.31μs (59.0% faster)

def test_outside_bottom_basic():
    # Test outside bottom placement (centered horizontally, top anchor)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "outside bottom"); result = codeflash_output # 4.87μs -> 3.25μs (49.9% faster)

def test_outside_left_basic():
    # Test outside left placement (right anchor)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "outside left"); result = codeflash_output # 5.18μs -> 3.18μs (62.6% faster)

def test_outside_right_basic():
    # Test outside right placement (left anchor)
    shape_args = {"x0": 0, "x1": 10, "y0": 0, "y1": 20}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "outside right"); result = codeflash_output # 5.16μs -> 3.22μs (60.4% faster)

# ---------------------- LARGE SCALE TEST CASES ----------------------

def test_large_scale_inside_top_right():
    # Test with large values
    shape_args = {"x0": 1_000_000, "x1": 2_000_000, "y0": 500_000, "y1": 2_500_000}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top right"); result = codeflash_output # 3.12μs -> 2.49μs (25.4% faster)

def test_large_scale_inside_center():
    # Test with large values and center position
    shape_args = {"x0": 1_000_000, "x1": 2_000_000, "y0": 500_000, "y1": 2_500_000}
    codeflash_output = annotation_params_for_rect("rect", shape_args, None); result = codeflash_output # 3.05μs -> 2.38μs (28.2% faster)

def test_large_scale_outside_top_left_vrect():
    # Test large values for vrect shape_type
    shape_args = {"x0": 100, "x1": 900, "y0": 100_000, "y1": 900_000}
    codeflash_output = annotation_params_for_rect("vrect", shape_args, "outside top left"); result = codeflash_output # 4.40μs -> 2.89μs (52.1% faster)

def test_many_calls_performance():
    # Test performance and determinism with many calls (1000)
    shape_args = {"x0": 0, "x1": 100, "y0": 0, "y1": 200}
    for i in range(1000):
        # Use alternating positions and shape_types
        pos = "top right" if i % 2 == 0 else "bottom left"
        shape_type = "rect" if i % 2 == 0 else "vrect"
        codeflash_output = annotation_params_for_rect(shape_type, shape_args, pos); result = codeflash_output # 1.10ms -> 766μs (43.3% faster)
        if i % 2 == 0:
            pass
        else:
            pass

def test_large_range_float_coordinates():
    # Test with large float coordinates
    shape_args = {"x0": 1e5, "x1": 1e6, "y0": 2e5, "y1": 2e6}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top"); result = codeflash_output # 4.85μs -> 3.63μs (33.6% faster)

def test_large_scale_outside_right():
    # Test outside right with large values
    shape_args = {"x0": 0, "x1": 999, "y0": 0, "y1": 999}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "outside right"); result = codeflash_output # 5.66μs -> 3.53μs (60.1% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
import pytest  # used for our unit tests
from plotly.shapeannotation import annotation_params_for_rect

# unit tests

# --- Basic Test Cases ---

def test_inside_top_left_basic():
    # Should return left/top anchor at min(x), max(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "top left"
    ); result = codeflash_output # 2.90μs -> 2.31μs (25.6% faster)

def test_inside_top_right_basic():
    # Should return right/top anchor at max(x), max(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "top right"
    ); result = codeflash_output # 3.10μs -> 2.34μs (32.2% faster)

def test_inside_top_basic():
    # Should return center/top anchor at mean(x), max(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 2, "x1": 6, "y0": 1, "y1": 9}, "top"
    ); result = codeflash_output # 3.65μs -> 2.57μs (42.0% faster)

def test_inside_bottom_left_basic():
    # Should return left/bottom anchor at min(x), min(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "bottom left"
    ); result = codeflash_output # 3.38μs -> 2.56μs (32.1% faster)

def test_inside_bottom_right_basic():
    # Should return right/bottom anchor at max(x), min(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "bottom right"
    ); result = codeflash_output # 3.25μs -> 2.57μs (26.6% faster)

def test_inside_bottom_basic():
    # Should return center/bottom anchor at mean(x), min(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 2, "x1": 6, "y0": 1, "y1": 9}, "bottom"
    ); result = codeflash_output # 3.90μs -> 2.76μs (41.1% faster)

def test_inside_left_basic():
    # Should return left/middle anchor at min(x), mean(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "left"
    ); result = codeflash_output # 4.04μs -> 2.86μs (41.2% faster)

def test_inside_right_basic():
    # Should return right/middle anchor at max(x), mean(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "right"
    ); result = codeflash_output # 4.16μs -> 2.82μs (47.5% faster)

def test_inside_center_basic():
    # Should return center/middle anchor at mean(x), mean(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, None
    ); result = codeflash_output # 3.43μs -> 2.75μs (24.6% faster)

def test_inside_only_basic():
    # Should return center/middle anchor at mean(x), mean(y)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "inside"
    ); result = codeflash_output # 4.10μs -> 2.68μs (52.8% faster)

def test_outside_top_left_vrect():
    # Should swap anchor for vrect
    codeflash_output = annotation_params_for_rect(
        "vrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside top left"
    ); result = codeflash_output # 4.74μs -> 3.05μs (55.2% faster)

def test_outside_top_left_hrect():
    # Should swap anchor for hrect
    codeflash_output = annotation_params_for_rect(
        "hrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside top left"
    ); result = codeflash_output # 4.28μs -> 2.80μs (53.0% faster)

def test_outside_top_right_vrect():
    codeflash_output = annotation_params_for_rect(
        "vrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside top right"
    ); result = codeflash_output # 4.39μs -> 2.92μs (50.4% faster)

def test_outside_top_right_hrect():
    codeflash_output = annotation_params_for_rect(
        "hrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside top right"
    ); result = codeflash_output # 4.05μs -> 2.69μs (50.9% faster)

def test_outside_top_basic():
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 2, "x1": 6, "y0": 1, "y1": 9}, "outside top"
    ); result = codeflash_output # 4.92μs -> 3.06μs (60.9% faster)

def test_outside_bottom_left_vrect():
    codeflash_output = annotation_params_for_rect(
        "vrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside bottom left"
    ); result = codeflash_output # 4.49μs -> 3.02μs (48.6% faster)

def test_outside_bottom_left_hrect():
    codeflash_output = annotation_params_for_rect(
        "hrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside bottom left"
    ); result = codeflash_output # 4.40μs -> 2.69μs (63.5% faster)

def test_outside_bottom_right_vrect():
    codeflash_output = annotation_params_for_rect(
        "vrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside bottom right"
    ); result = codeflash_output # 4.68μs -> 2.99μs (56.3% faster)

def test_outside_bottom_right_hrect():
    codeflash_output = annotation_params_for_rect(
        "hrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside bottom right"
    ); result = codeflash_output # 4.35μs -> 2.82μs (54.3% faster)

def test_outside_bottom_basic():
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 2, "x1": 6, "y0": 1, "y1": 9}, "outside bottom"
    ); result = codeflash_output # 5.17μs -> 3.22μs (60.5% faster)

def test_outside_left_basic():
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside left"
    ); result = codeflash_output # 5.05μs -> 3.07μs (64.5% faster)

def test_outside_right_basic():
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside right"
    ); result = codeflash_output # 5.08μs -> 3.15μs (61.4% faster)

# --- Edge Test Cases ---

def test_x0_equals_x1():
    # Degenerate rectangle; x0 == x1
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 4, "x1": 4, "y0": 2, "y1": 8}, "top left"
    ); result = codeflash_output # 3.04μs -> 2.50μs (21.2% faster)

def test_y0_equals_y1():
    # Degenerate rectangle; y0 == y1
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 7, "y1": 7}, "bottom right"
    ); result = codeflash_output # 3.61μs -> 2.64μs (36.9% faster)

def test_x0_greater_than_x1():
    # x0 > x1, should still work
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 10, "x1": 2, "y0": 8, "y1": 2}, "top left"
    ); result = codeflash_output # 2.86μs -> 2.47μs (15.7% faster)

def test_y0_greater_than_y1():
    # y0 > y1, should still work
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 10, "y1": 2}, "bottom right"
    ); result = codeflash_output # 3.49μs -> 2.50μs (39.5% faster)

def test_negative_coordinates():
    # Negative coordinates should work
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": -3, "x1": -1, "y0": -5, "y1": -2}, "top right"
    ); result = codeflash_output # 3.04μs -> 2.48μs (22.5% faster)

def test_zero_coordinates():
    # Zero coordinates should work
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 0, "x1": 0, "y0": 0, "y1": 0}, "bottom left"
    ); result = codeflash_output # 3.34μs -> 2.47μs (35.3% faster)

def test_float_coordinates():
    # Float coordinates should work
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1.5, "x1": 2.5, "y0": 3.5, "y1": 4.5}, "right"
    ); result = codeflash_output # 4.53μs -> 3.07μs (47.6% faster)

def test_position_case_insensitivity():
    # Position should be split by space, not case; so "Top Left" is not valid
    with pytest.raises(ValueError):
        annotation_params_for_rect(
            "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "Top Left"
        ) # 4.59μs -> 2.98μs (54.4% faster)

def test_invalid_position():
    # Should raise error for unknown position
    with pytest.raises(ValueError):
        annotation_params_for_rect(
            "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "foobar"
        ) # 4.41μs -> 2.60μs (69.7% faster)

def test_missing_shape_args():
    # Should raise KeyError if shape_args missing keys
    with pytest.raises(KeyError):
        annotation_params_for_rect(
            "rect", {"x0": 1, "x1": 5, "y0": 2}, "top left"
        ) # 741ns -> 725ns (2.21% faster)

def test_empty_shape_args():
    # Should raise KeyError if shape_args is empty
    with pytest.raises(KeyError):
        annotation_params_for_rect(
            "rect", {}, "top left"
        ) # 665ns -> 711ns (6.47% slower)

def test_none_position_defaults():
    # None position defaults to "top right" (inside)
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, None
    ); result = codeflash_output # 3.98μs -> 3.26μs (22.1% faster)


def test_position_with_inside_explicit():
    # Should work if "inside" is explicitly in position
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "inside top left"
    ); result = codeflash_output # 4.53μs -> 3.86μs (17.2% faster)

def test_position_with_outside_explicit():
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "outside bottom right"
    ); result = codeflash_output # 5.62μs -> 3.62μs (55.2% faster)

def test_non_rect_shape_type():
    # Should work for vrect and hrect (already tested above), but fail for unknown shape_type only if position is outside
    codeflash_output = annotation_params_for_rect(
        "customrect", {"x0": 1, "x1": 5, "y0": 2, "y1": 8}, "top left"
    ); result = codeflash_output # 3.29μs -> 2.69μs (22.5% faster)

# --- Large Scale Test Cases ---

def test_large_scale_many_calls():
    # Test function over many rectangles with varying positions
    for i in range(1, 501):
        shape_args = {"x0": i, "x1": i+10, "y0": i*2, "y1": i*2+20}
        # Alternate position between top left and bottom right
        if i % 2 == 0:
            codeflash_output = annotation_params_for_rect("rect", shape_args, "top left"); result = codeflash_output
        else:
            codeflash_output = annotation_params_for_rect("rect", shape_args, "bottom right"); result = codeflash_output

def test_large_scale_extreme_values():
    # Very large/small values
    codeflash_output = annotation_params_for_rect(
        "rect", {"x0": -1e9, "x1": 1e9, "y0": -1e9, "y1": 1e9}, "top"
    ); result = codeflash_output # 4.72μs -> 3.24μs (45.7% faster)

def test_large_scale_float_precision():
    # Many rectangles with float values
    for i in range(1, 101):
        x0 = i * 0.1
        x1 = i * 0.2
        y0 = i * 0.3
        y1 = i * 0.4
        codeflash_output = annotation_params_for_rect(
            "rect",
            {"x0": x0, "x1": x1, "y0": y0, "y1": y1},
            "right"
        ); result = codeflash_output # 156μs -> 88.2μs (77.4% faster)

def test_large_scale_positions():
    # Test all positions for a large number of rectangles
    positions = [
        "top left", "top right", "top", "bottom left", "bottom right", "bottom",
        "left", "right", "inside", "outside top left", "outside top right",
        "outside top", "outside bottom left", "outside bottom right",
        "outside bottom", "outside left", "outside right"
    ]
    for i, pos in enumerate(positions):
        shape_args = {"x0": i, "x1": i+1, "y0": i+2, "y1": i+3}
        # Should not raise
        codeflash_output = annotation_params_for_rect("rect", shape_args, pos); result = codeflash_output # 36.4μs -> 23.3μs (56.4% faster)

def test_large_scale_invalid_positions():
    # Test that all invalid positions raise
    invalid_positions = ["", "foo", "left right", "top bottom", "middle", "center", "up down", "left up", "down", "side", "corner"]
    for pos in invalid_positions:
        with pytest.raises(ValueError):
            annotation_params_for_rect("rect", {"x0": 1, "x1": 2, "y0": 3, "y1": 4}, pos)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.

To edit these changes git checkout codeflash/optimize-annotation_params_for_rect-mhge3a6f and push.

Codeflash Static Badge

The optimized code achieves a **46% speedup** through several key optimizations:

**1. Precomputed frozenset constants for position matching**
- Replaced repeated `set(["inside", "top", "left"])` constructions with precomputed `frozenset` constants like `_INSIDE_TOP_LEFT`
- Eliminates the overhead of creating new sets on every comparison, as seen in the line profiler where position comparisons now run much faster

**2. Optimized `_mean` function for the common two-element case**
- Added fast path: `return (x[0] + x[1]) / 2 if n == 2 else float(sum(x)) / n`
- Since coordinates are typically pairs (x0,x1) or (y0,y1), this avoids `sum()` and `float()` conversion overhead for the most common case

**3. Inlined position preprocessing logic**
- Replaced the `_add_inside_to_position()` function call with direct inline logic in `_prepare_position`
- Eliminates function call overhead and import dependencies

**4. Streamlined min/max operations**
- Changed `min([x0, x1])` to `min(x0, x1)` and `max([y0, y1])` to `max(y0, y1)`
- Avoids creating temporary lists for simple two-argument min/max operations

**Performance characteristics:**
- **Inside positions** show 23-58% improvements due to faster set comparisons and optimized `_mean`
- **Outside positions** show 38-64% improvements, benefiting most from the precomputed frozensets
- **Large-scale operations** (like the 1000-call test) see 43% improvement, demonstrating the cumulative effect
- **Error cases** are 55-70% faster due to reduced overhead in position parsing

These optimizations are particularly effective for plotting libraries where annotation positioning is called frequently with predictable position patterns.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 November 1, 2025 14:40
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Nov 1, 2025
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: High Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant