Skip to content

⚡️ Speed up function annotation_params_for_rect by 22% #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

codeflash-ai[bot]
Copy link

@codeflash-ai codeflash-ai bot commented May 24, 2025

📄 22% (0.22x) speedup for annotation_params_for_rect in plotly/shapeannotation.py

⏱️ Runtime : 1.91 milliseconds 1.57 milliseconds (best of 69 runs)

📝 Explanation and details

Here is an optimized version of your code. The main optimizations are.

  • Use tuple lookups instead of repeated set construction: Instead of repeatedly constructing sets, use a frozenset as dictionary keys to map directly to (xanchor, yanchor, x_func, y_func), allowing O(1) lookup and less code branching.
  • Remove extra list wrappings: Use min(x0, x1), max(y0, y1), etc., directly.
  • Avoid repeated string checks and set/add operations: Pre-parse the position string efficiently.
  • Hoist conditionals for 'vrect' and 'hrect'.

Result: Clean, faster, branchless table-driven variant.

Key improvements.

  • Fast lookup for most positions, fewer branches.
  • No list allocations for min/max/mean operations.
  • Positions and logic are handled with much less code and time branching.
  • frozenset is used as a hashable dict key for lookup.

Function signatures and outputs are unchanged.

Correctness verification report:

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

# unit tests

# ----------------- Basic Test Cases -----------------

@pytest.mark.parametrize(
    "shape_type, shape_args, position, expected",
    [
        # Inside top left
        (
            "rect",
            {"x0": 1, "x1": 3, "y0": 2, "y1": 4},
            "inside top left",
            {"xanchor": "left", "yanchor": "top", "x": 1, "y": 4, "showarrow": False},
        ),
        # Inside top right
        (
            "rect",
            {"x0": 1, "x1": 3, "y0": 2, "y1": 4},
            "inside top right",
            {"xanchor": "right", "yanchor": "top", "x": 3, "y": 4, "showarrow": False},
        ),
        # Inside top (center)
        (
            "rect",
            {"x0": 1, "x1": 5, "y0": 2, "y1": 6},
            "inside top",
            {"xanchor": "center", "yanchor": "top", "x": 3.0, "y": 6, "showarrow": False},
        ),
        # Inside bottom left
        (
            "rect",
            {"x0": 2, "x1": 8, "y0": 3, "y1": 7},
            "inside bottom left",
            {"xanchor": "left", "yanchor": "bottom", "x": 2, "y": 3, "showarrow": False},
        ),
        # Inside bottom right
        (
            "rect",
            {"x0": 2, "x1": 8, "y0": 3, "y1": 7},
            "inside bottom right",
            {"xanchor": "right", "yanchor": "bottom", "x": 8, "y": 3, "showarrow": False},
        ),
        # Inside bottom (center)
        (
            "rect",
            {"x0": 2, "x1": 8, "y0": 3, "y1": 7},
            "inside bottom",
            {"xanchor": "center", "yanchor": "bottom", "x": 5.0, "y": 3, "showarrow": False},
        ),
        # Inside left (middle)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 4, "y1": 8},
            "inside left",
            {"xanchor": "left", "yanchor": "middle", "x": 0, "y": 6.0, "showarrow": False},
        ),
        # Inside right (middle)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 4, "y1": 8},
            "inside right",
            {"xanchor": "right", "yanchor": "middle", "x": 10, "y": 6.0, "showarrow": False},
        ),
        # Inside (center middle)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 4, "y1": 8},
            "inside",
            {"xanchor": "center", "yanchor": "middle", "x": 5.0, "y": 6.0, "showarrow": False},
        ),
        # None position defaults to "top right" (inside top right)
        (
            "rect",
            {"x0": 1, "x1": 3, "y0": 2, "y1": 4},
            None,
            {"xanchor": "right", "yanchor": "top", "x": 3, "y": 4, "showarrow": False},
        ),
        # Position with extra spaces
        (
            "rect",
            {"x0": 1, "x1": 3, "y0": 2, "y1": 4},
            " inside   top   right ",
            {"xanchor": "right", "yanchor": "top", "x": 3, "y": 4, "showarrow": False},
        ),
    ]
)
def test_basic_inside_positions(shape_type, shape_args, position, expected):
    # Remove extra spaces for the test to match function's expected input
    if position is not None:
        position = " ".join(position.split())
    codeflash_output = annotation_params_for_rect(shape_type, shape_args, position); result = codeflash_output

@pytest.mark.parametrize(
    "shape_type, shape_args, position, expected",
    [
        # Outside top left (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside top left",
            {"xanchor": "left", "yanchor": "top", "x": 0, "y": 10, "showarrow": False},
        ),
        # Outside top right (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside top right",
            {"xanchor": "right", "yanchor": "top", "x": 10, "y": 10, "showarrow": False},
        ),
        # Outside top (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside top",
            {"xanchor": "center", "yanchor": "bottom", "x": 5.0, "y": 10, "showarrow": False},
        ),
        # Outside bottom left (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside bottom left",
            {"xanchor": "left", "yanchor": "bottom", "x": 0, "y": 0, "showarrow": False},
        ),
        # Outside bottom right (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside bottom right",
            {"xanchor": "right", "yanchor": "bottom", "x": 10, "y": 0, "showarrow": False},
        ),
        # Outside bottom (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside bottom",
            {"xanchor": "center", "yanchor": "top", "x": 5.0, "y": 0, "showarrow": False},
        ),
        # Outside left (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside left",
            {"xanchor": "right", "yanchor": "middle", "x": 0, "y": 5.0, "showarrow": False},
        ),
        # Outside right (rect)
        (
            "rect",
            {"x0": 0, "x1": 10, "y0": 0, "y1": 10},
            "outside right",
            {"xanchor": "left", "yanchor": "middle", "x": 10, "y": 5.0, "showarrow": False},
        ),
        # Outside top left (vrect)
        (
            "vrect",
            {"x0": 2, "x1": 8, "y0": 4, "y1": 12},
            "outside top left",
            {"xanchor": "right", "yanchor": "top", "x": 2, "y": 12, "showarrow": False},
        ),
        # Outside bottom right (hrect)
        (
            "hrect",
            {"x0": 2, "x1": 8, "y0": 4, "y1": 12},
            "outside bottom right",
            {"xanchor": "right", "yanchor": "top", "x": 8, "y": 4, "showarrow": False},
        ),
    ]
)
def test_basic_outside_positions(shape_type, shape_args, position, expected):
    codeflash_output = annotation_params_for_rect(shape_type, shape_args, position); result = codeflash_output

# ----------------- Edge Test Cases -----------------

def test_x0_equals_x1_y0_equals_y1():
    # Rectangle is a point
    shape_args = {"x0": 5, "x1": 5, "y0": 7, "y1": 7}
    # All positions should collapse to the same point
    for pos, expected in [
        ("inside top left", {"xanchor": "left", "yanchor": "top", "x": 5, "y": 7, "showarrow": False}),
        ("inside bottom right", {"xanchor": "right", "yanchor": "bottom", "x": 5, "y": 7, "showarrow": False}),
        ("inside", {"xanchor": "center", "yanchor": "middle", "x": 5.0, "y": 7.0, "showarrow": False}),
        ("outside left", {"xanchor": "right", "yanchor": "middle", "x": 5, "y": 7.0, "showarrow": False}),
    ]:
        codeflash_output = annotation_params_for_rect("rect", shape_args, pos); result = codeflash_output

def test_negative_coordinates():
    # Rectangle with negative coordinates
    shape_args = {"x0": -10, "x1": -2, "y0": -8, "y1": -4}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside top right"); result = codeflash_output

def test_reversed_coordinates():
    # x0 > x1 and y0 > y1
    shape_args = {"x0": 10, "x1": 2, "y0": 8, "y1": 4}
    # Should still select min/max correctly
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside bottom left"); result = codeflash_output

def test_float_coordinates():
    # Rectangle with float coordinates
    shape_args = {"x0": 1.5, "x1": 3.5, "y0": 2.5, "y1": 4.5}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside top"); result = codeflash_output

def test_position_case_insensitivity_and_order():
    # Position order and case should not matter (but function is case-sensitive, so test order only)
    shape_args = {"x0": 1, "x1": 2, "y0": 3, "y1": 4}
    # "top inside right" should be the same as "inside top right"
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside top right"); result1 = codeflash_output
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top right inside"); result2 = codeflash_output

def test_invalid_position_raises():
    # Unknown position should raise ValueError
    shape_args = {"x0": 1, "x1": 2, "y0": 3, "y1": 4}
    with pytest.raises(ValueError):
        annotation_params_for_rect("rect", shape_args, "foobar")

def test_missing_position_word_defaults_to_inside():
    # "top right" should default to inside
    shape_args = {"x0": 1, "x1": 2, "y0": 3, "y1": 4}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "top right"); result = codeflash_output
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside top right"); expected = codeflash_output


def test_large_float_coordinates():
    # Large float values
    shape_args = {"x0": 1e10, "x1": 1e11, "y0": -1e12, "y1": 1e12}
    codeflash_output = annotation_params_for_rect("rect", shape_args, "inside"); result = codeflash_output

# ----------------- Large Scale Test Cases -----------------

def test_many_rects_performance():
    # Test a large number of rectangles (but < 1000 for performance)
    for i in range(1, 1001, 100):
        shape_args = {"x0": i, "x1": i + 10, "y0": i * 2, "y1": i * 2 + 10}
        # Use a variety of positions
        for position, expected in [
            ("inside top left", {"xanchor": "left", "yanchor": "top", "x": i, "y": i * 2 + 10, "showarrow": False}),
            ("inside bottom right", {"xanchor": "right", "yanchor": "bottom", "x": i + 10, "y": i * 2, "showarrow": False}),
            ("inside", {"xanchor": "center", "yanchor": "middle", "x": i + 5.0, "y": i * 2 + 5.0, "showarrow": False}),
        ]:
            codeflash_output = annotation_params_for_rect("rect", shape_args, position); result = codeflash_output

def test_large_rect_coordinates():
    # Test rectangles with large coordinate ranges
    shape_args = {"x0": -1_000_000, "x1": 1_000_000, "y0": -2_000_000, "y1": 2_000_000}
    # Test all "inside" positions
    positions = [
        ("inside top left", {"xanchor": "left", "yanchor": "top", "x": -1_000_000, "y": 2_000_000, "showarrow": False}),
        ("inside top right", {"xanchor": "right", "yanchor": "top", "x": 1_000_000, "y": 2_000_000, "showarrow": False}),
        ("inside bottom left", {"xanchor": "left", "yanchor": "bottom", "x": -1_000_000, "y": -2_000_000, "showarrow": False}),
        ("inside bottom right", {"xanchor": "right", "yanchor": "bottom", "x": 1_000_000, "y": -2_000_000, "showarrow": False}),
        ("inside", {"xanchor": "center", "yanchor": "middle", "x": 0.0, "y": 0.0, "showarrow": False}),
    ]
    for position, expected in positions:
        codeflash_output = annotation_params_for_rect("rect", shape_args, position); result = codeflash_output

def test_performance_with_various_positions():
    # Test all valid positions for a single rectangle
    shape_args = {"x0": 0, "x1": 100, "y0": 0, "y1": 200}
    positions = [
        "inside top left", "inside top right", "inside top",
        "inside bottom left", "inside bottom right", "inside bottom",
        "inside left", "inside right", "inside",
        "outside top left", "outside top right", "outside top",
        "outside bottom left", "outside bottom right", "outside bottom",
        "outside left", "outside right",
    ]
    # Just check that all positions return a dict with required keys and correct types
    for position in positions:
        codeflash_output = annotation_params_for_rect("rect", shape_args, position); result = codeflash_output
        for key in ["xanchor", "yanchor", "x", "y", "showarrow"]:
            pass
# 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 -----------

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_top_left(shape_type):
    # Test inside top left for a normal rectangle
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 1,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_top_right(shape_type):
    # Test inside top right for a normal rectangle
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top right"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_top(shape_type):
    # Test inside top for a normal rectangle
    args = {"x0": 2, "x1": 6, "y0": 3, "y1": 8}
    expected = {
        "xanchor": "center",
        "yanchor": "top",
        "x": 4.0,
        "y": 8,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_bottom_left(shape_type):
    # Test inside bottom left for a normal rectangle
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "bottom",
        "x": 1,
        "y": 2,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside bottom left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_bottom_right(shape_type):
    # Test inside bottom right for a normal rectangle
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "bottom",
        "x": 3,
        "y": 2,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside bottom right"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_bottom(shape_type):
    # Test inside bottom for a normal rectangle
    args = {"x0": 2, "x1": 6, "y0": 3, "y1": 8}
    expected = {
        "xanchor": "center",
        "yanchor": "bottom",
        "x": 4.0,
        "y": 3,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside bottom"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_left(shape_type):
    # Test inside left for a normal rectangle
    args = {"x0": 2, "x1": 6, "y0": 3, "y1": 8}
    expected = {
        "xanchor": "left",
        "yanchor": "middle",
        "x": 2,
        "y": 5.5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_right(shape_type):
    # Test inside right for a normal rectangle
    args = {"x0": 2, "x1": 6, "y0": 3, "y1": 8}
    expected = {
        "xanchor": "right",
        "yanchor": "middle",
        "x": 6,
        "y": 5.5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside right"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_inside_center(shape_type):
    # Test inside (center) for a normal rectangle
    args = {"x0": 2, "x1": 6, "y0": 3, "y1": 8}
    expected = {
        "xanchor": "center",
        "yanchor": "middle",
        "x": 4.0,
        "y": 5.5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_none_position_defaults_to_top_right(shape_type):
    # Test that None position defaults to "top right"
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, None); result = codeflash_output

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

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_x0_greater_than_x1_and_y0_greater_than_y1(shape_type):
    # x0 > x1 and y0 > y1, should still work (min/max used)
    args = {"x0": 5, "x1": 2, "y0": 8, "y1": 3}
    # "inside top left" means min(x), max(y)
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 2,
        "y": 8,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_x0_equals_x1_and_y0_equals_y1(shape_type):
    # Degenerate rectangle (point)
    args = {"x0": 5, "x1": 5, "y0": 7, "y1": 7}
    # All positions should collapse to the same point
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 5,
        "y": 7,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_x0_equals_x1_vertical_line(shape_type):
    # Vertical line (x0 == x1)
    args = {"x0": 4, "x1": 4, "y0": 1, "y1": 9}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 4,
        "y": 9,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top right"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_y0_equals_y1_horizontal_line(shape_type):
    # Horizontal line (y0 == y1)
    args = {"x0": 4, "x1": 8, "y0": 5, "y1": 5}
    expected = {
        "xanchor": "center",
        "yanchor": "top",
        "x": 6.0,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_negative_coordinates(shape_type):
    # Negative coordinates
    args = {"x0": -5, "x1": -2, "y0": -8, "y1": -3}
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": -5,
        "y": -3,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_zero_coordinates(shape_type):
    # Zero coordinates
    args = {"x0": 0, "x1": 0, "y0": 0, "y1": 0}
    expected = {
        "xanchor": "center",
        "yanchor": "middle",
        "x": 0.0,
        "y": 0.0,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_position_with_extra_spaces(shape_type):
    # Position string with extra spaces should still work
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "  top   right "); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_invalid_position_raises(shape_type):
    # Invalid position string should raise ValueError
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    with pytest.raises(ValueError):
        annotation_params_for_rect(shape_type, args, "notarealposition")

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_missing_x0_raises(shape_type):
    # Missing x0 in shape_args should raise KeyError
    args = {"x1": 3, "y0": 2, "y1": 5}
    with pytest.raises(KeyError):
        annotation_params_for_rect(shape_type, args, "inside top left")

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_missing_y1_raises(shape_type):
    # Missing y1 in shape_args should raise KeyError
    args = {"x0": 1, "x1": 3, "y0": 2}
    with pytest.raises(KeyError):
        annotation_params_for_rect(shape_type, args, "inside top left")

# ----------- OUTSIDE POSITION TESTS -----------

def test_outside_top_left_rect():
    # For rect, outside top left
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 1,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("rect", args, "outside top left"); result = codeflash_output

def test_outside_top_left_vrect():
    # For vrect, outside top left
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 1,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("vrect", args, "outside top left"); result = codeflash_output

def test_outside_top_left_hrect():
    # For hrect, outside top left
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "bottom",
        "x": 1,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("hrect", args, "outside top left"); result = codeflash_output

def test_outside_top_right_vrect():
    # For vrect, outside top right
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("vrect", args, "outside top right"); result = codeflash_output

def test_outside_top_right_hrect():
    # For hrect, outside top right
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "bottom",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("hrect", args, "outside top right"); result = codeflash_output

def test_outside_top_right_rect():
    # For rect, outside top right
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect("rect", args, "outside top right"); result = codeflash_output

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

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_large_coordinates(shape_type):
    # Test with very large coordinate values
    args = {"x0": 1_000_000, "x1": 2_000_000, "y0": 3_000_000, "y1": 4_000_000}
    expected = {
        "xanchor": "center",
        "yanchor": "top",
        "x": 1_500_000.0,
        "y": 4_000_000,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside top"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_many_rects_performance(shape_type):
    # Test function performance by calling it 500 times with unique coordinates
    for i in range(500):
        args = {"x0": i, "x1": i+10, "y0": i+5, "y1": i+15}
        # Just check that it doesn't raise and returns expected keys
        codeflash_output = annotation_params_for_rect(shape_type, args, "inside top right"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_large_negative_coordinates(shape_type):
    # Test with large negative coordinate values
    args = {"x0": -1_000_000, "x1": -2_000_000, "y0": -3_000_000, "y1": -4_000_000}
    expected = {
        "xanchor": "center",
        "yanchor": "bottom",
        "x": -1_500_000.0,
        "y": -4_000_000,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside bottom"); result = codeflash_output

# ----------- ADDITIONAL EDGE CASES -----------

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_position_case_insensitivity(shape_type):
    # Position should be case sensitive, so "Inside Top Left" is invalid
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    with pytest.raises(ValueError):
        annotation_params_for_rect(shape_type, args, "Inside Top Left")

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_position_with_duplicate_keywords(shape_type):
    # "inside inside top left" should be treated as {"inside", "top", "left"}
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "left",
        "yanchor": "top",
        "x": 1,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "inside inside top left"); result = codeflash_output

@pytest.mark.parametrize("shape_type", ["rect", "hrect", "vrect"])
def test_position_with_trailing_leading_spaces(shape_type):
    # Position string with leading/trailing spaces
    args = {"x0": 1, "x1": 3, "y0": 2, "y1": 5}
    expected = {
        "xanchor": "right",
        "yanchor": "top",
        "x": 3,
        "y": 5,
        "showarrow": False,
    }
    codeflash_output = annotation_params_for_rect(shape_type, args, "   top right   "); result = codeflash_output
# 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-mb2cv0gc and push.

Codeflash

Here is an optimized version of your code. The main optimizations are.

- **Use tuple lookups instead of repeated set construction:** Instead of repeatedly constructing sets, use a **frozenset** as dictionary keys to map directly to (xanchor, yanchor, x_func, y_func), allowing O(1) lookup and less code branching.
- **Remove extra list wrappings:** Use `min(x0, x1)`, `max(y0, y1)`, etc., directly.
- **Avoid repeated string checks and set/add operations:** Pre-parse the position string efficiently.
- **Hoist conditionals for 'vrect' and 'hrect'.**

Result: Clean, faster, branchless table-driven variant.



**Key improvements**.
- Fast lookup for most positions, fewer branches.
- No list allocations for min/max/mean operations.
- Positions and logic are handled with much less code and time branching.
- `frozenset` is used as a hashable dict key for lookup.

**Function signatures and outputs are unchanged.**
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label May 24, 2025
@codeflash-ai codeflash-ai bot requested a review from misrasaurabh1 May 24, 2025 14:59
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
Projects
None yet
Development

Successfully merging this pull request may close these issues.

0 participants