Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Dec 4, 2025

📄 12% (0.12x) speedup for wrap_in_list in inference/core/interfaces/stream/utils.py

⏱️ Runtime : 44.5 microseconds 39.8 microseconds (best of 44 runs)

📝 Explanation and details

The optimization replaces issubclass(type(element), list) with isinstance(element, list), resulting in an 11% speedup.

Key optimization: The original code uses issubclass(type(element), list) to check if an element is a list, which involves two function calls: type() to get the object's type, then issubclass() to check inheritance. The optimized version uses isinstance(element, list), which is a single, more direct type check operation.

Why it's faster: isinstance() is implemented more efficiently in Python's C code and avoids the overhead of the type() + issubclass() chain. The line profiler shows the condition check time reduced from 107,350ns to 84,656ns (21% faster on that line alone).

Function usage context: This function is called in hot paths within the inference pipeline - specifically in render_boxes(), send_predictions(), and active_learning_sink() methods that process video frames and predictions. Since these methods are called frequently during video processing, the 11% improvement compounds significantly.

Test case performance: The optimization shows consistent improvements across most test cases, with particularly strong gains for list inputs (24-41% faster) where the isinstance check immediately returns True. Non-list inputs see 7-17% improvements. A few edge cases with dict/bool inputs show slight regressions (1-14% slower), but these are likely within measurement noise and outweighed by the overall gains.

Behavioral preservation: Both approaches correctly handle list subclasses and maintain identical functionality - isinstance() properly recognizes subclasses of list just like the original issubclass(type(x), list) approach.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 8 Passed
🌀 Generated Regression Tests 86 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Existing Unit Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
inference/unit_tests/core/interfaces/stream/test_utils.py::test_wrap_in_list_when_list_provided 839ns 670ns 25.2%✅
inference/unit_tests/core/interfaces/stream/test_utils.py::test_wrap_in_list_when_single_element_provided 749ns 711ns 5.34%✅
🌀 Generated Regression Tests and Runtime
from typing import List, TypeVar, Union

# imports
import pytest  # used for our unit tests
from inference.core.interfaces.stream.utils import wrap_in_list

T = TypeVar("T")
from inference.core.interfaces.stream.utils import wrap_in_list

# unit tests

# -------------------------
# 1. Basic Test Cases
# -------------------------


def test_wrap_in_list_with_int():
    # Should wrap an integer into a list
    codeflash_output = wrap_in_list(42)  # 549ns -> 482ns (13.9% faster)


def test_wrap_in_list_with_str():
    # Should wrap a string into a list
    codeflash_output = wrap_in_list("hello")  # 507ns -> 435ns (16.6% faster)


def test_wrap_in_list_with_float():
    # Should wrap a float into a list
    codeflash_output = wrap_in_list(3.14)  # 501ns -> 426ns (17.6% faster)


def test_wrap_in_list_with_list_of_ints():
    # Should return the same list if input is already a list
    codeflash_output = wrap_in_list([1, 2, 3])  # 447ns -> 317ns (41.0% faster)


def test_wrap_in_list_with_list_of_strings():
    # Should return the same list if input is already a list
    codeflash_output = wrap_in_list(["a", "b"])  # 441ns -> 338ns (30.5% faster)


def test_wrap_in_list_with_empty_list():
    # Should return the same empty list if input is already a list
    codeflash_output = wrap_in_list([])  # 402ns -> 324ns (24.1% faster)


# -------------------------
# 2. Edge Test Cases
# -------------------------


def test_wrap_in_list_with_none():
    # Should wrap None into a list
    codeflash_output = wrap_in_list(None)  # 511ns -> 456ns (12.1% faster)


def test_wrap_in_list_with_tuple():
    # Should wrap a tuple into a list, not flatten it
    tup = (1, 2)
    codeflash_output = wrap_in_list(tup)  # 537ns -> 501ns (7.19% faster)


def test_wrap_in_list_with_dict():
    # Should wrap a dict into a list
    d = {"a": 1}
    codeflash_output = wrap_in_list(d)  # 507ns -> 596ns (14.9% slower)


def test_wrap_in_list_with_set():
    # Should wrap a set into a list
    s = {1, 2}
    codeflash_output = wrap_in_list(s)  # 501ns -> 358ns (39.9% faster)


def test_wrap_in_list_with_bool_true():
    # Should wrap a boolean into a list
    codeflash_output = wrap_in_list(True)  # 536ns -> 587ns (8.69% slower)


def test_wrap_in_list_with_bool_false():
    # Should wrap a boolean into a list
    codeflash_output = wrap_in_list(False)  # 507ns -> 553ns (8.32% slower)


def test_wrap_in_list_with_bytes():
    # Should wrap bytes into a list
    b = b"bytes"
    codeflash_output = wrap_in_list(b)  # 476ns -> 474ns (0.422% faster)


def test_wrap_in_list_with_list_of_lists():
    # Should return the list of lists unchanged
    input_val = [[1, 2], [3, 4]]
    codeflash_output = wrap_in_list(input_val)  # 444ns -> 337ns (31.8% faster)


def test_wrap_in_list_with_custom_object():
    # Should wrap a custom object into a list
    class Foo:
        pass

    obj = Foo()
    codeflash_output = wrap_in_list(obj)  # 581ns -> 590ns (1.53% slower)


def test_wrap_in_list_with_list_of_custom_objects():
    # Should return the same list of custom objects
    class Bar:
        pass

    objs = [Bar(), Bar()]
    codeflash_output = wrap_in_list(objs)  # 460ns -> 346ns (32.9% faster)


def test_wrap_in_list_with_list_subclass():
    # Should not wrap a subclass of list, should return as is
    class MyList(list):
        pass

    ml = MyList([1, 2])
    codeflash_output = wrap_in_list(ml)  # 428ns -> 373ns (14.7% faster)


def test_wrap_in_list_with_list_of_none():
    # Should return the same list
    codeflash_output = wrap_in_list([None])  # 427ns -> 354ns (20.6% faster)


# -------------------------
# 3. Large Scale Test Cases
# -------------------------


def test_wrap_in_list_large_list():
    # Should return the same large list unchanged
    large_list = list(range(1000))
    codeflash_output = wrap_in_list(large_list)  # 456ns -> 363ns (25.6% faster)


def test_wrap_in_list_large_string():
    # Should wrap a large string into a list
    large_str = "x" * 1000
    codeflash_output = wrap_in_list(large_str)  # 529ns -> 452ns (17.0% faster)


def test_wrap_in_list_large_tuple():
    # Should wrap a large tuple into a list
    large_tuple = tuple(range(1000))
    codeflash_output = wrap_in_list(large_tuple)  # 589ns -> 555ns (6.13% faster)


def test_wrap_in_list_large_dict():
    # Should wrap a large dict into a list
    large_dict = {i: str(i) for i in range(1000)}
    codeflash_output = wrap_in_list(large_dict)  # 571ns -> 622ns (8.20% slower)


def test_wrap_in_list_large_custom_object_list():
    # Should return the same list of custom objects
    class Baz:
        pass

    objs = [Baz() for _ in range(1000)]
    codeflash_output = wrap_in_list(objs)  # 481ns -> 409ns (17.6% faster)


def test_wrap_in_list_large_set():
    # Should wrap a large set into a list
    large_set = set(range(1000))
    codeflash_output = wrap_in_list(large_set)  # 550ns -> 476ns (15.5% faster)


# -------------------------
# Additional Robustness Checks
# -------------------------


@pytest.mark.parametrize(
    "input_val",
    [
        0,
        "",
        [],
        {},
        (),
        set(),
        False,
        True,
        None,
    ],
)
def test_wrap_in_list_various_empty_like_values(input_val):
    # Should wrap non-list values, and return list as is
    if isinstance(input_val, list):
        codeflash_output = wrap_in_list(input_val)  # 4.79μs -> 4.04μs (18.5% faster)
    else:
        codeflash_output = wrap_in_list(input_val)


@pytest.mark.parametrize(
    "input_val",
    [
        [0],
        ["a"],
        [{}],
        [()],
        [set()],
        [False],
        [True],
        [None],
    ],
)
def test_wrap_in_list_various_singleton_lists(input_val):
    # Should return the same singleton list
    codeflash_output = wrap_in_list(input_val)  # 3.59μs -> 2.91μs (23.5% faster)


def test_wrap_in_list_mutation_does_not_affect_original():
    # Should not mutate the input if it's not a list
    x = 5
    codeflash_output = wrap_in_list(x)
    result = codeflash_output  # 542ns -> 537ns (0.931% faster)
    # Should not mutate the input if it's a list
    y = [6]
    codeflash_output = wrap_in_list(y)
    result2 = codeflash_output  # 267ns -> 247ns (8.10% faster)


def test_wrap_in_list_with_nested_list():
    # Should return the same nested list
    nested = [[1, 2], [3, 4], [5, [6, 7]]]
    codeflash_output = wrap_in_list(nested)  # 430ns -> 341ns (26.1% faster)


# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from typing import List, TypeVar, Union

# imports
import pytest  # used for our unit tests
from inference.core.interfaces.stream.utils import wrap_in_list

T = TypeVar("T")
from inference.core.interfaces.stream.utils import wrap_in_list

# unit tests

# -------------------------
# 1. Basic Test Cases
# -------------------------


def test_wrap_single_int():
    # Test wrapping a single integer
    codeflash_output = wrap_in_list(5)  # 697ns -> 594ns (17.3% faster)


def test_wrap_single_str():
    # Test wrapping a single string
    codeflash_output = wrap_in_list("hello")  # 533ns -> 483ns (10.4% faster)


def test_wrap_single_float():
    # Test wrapping a single float
    codeflash_output = wrap_in_list(3.14)  # 545ns -> 568ns (4.05% slower)


def test_wrap_list_of_ints():
    # Test passing a list of integers (should return as is)
    codeflash_output = wrap_in_list([1, 2, 3])  # 447ns -> 349ns (28.1% faster)


def test_wrap_list_of_strings():
    # Test passing a list of strings (should return as is)
    codeflash_output = wrap_in_list(["a", "b", "c"])  # 409ns -> 320ns (27.8% faster)


def test_wrap_empty_list():
    # Test passing an empty list (should return as is)
    codeflash_output = wrap_in_list([])  # 387ns -> 290ns (33.4% faster)


# -------------------------
# 2. Edge Test Cases
# -------------------------


def test_wrap_none():
    # Test passing None (should wrap in a list)
    codeflash_output = wrap_in_list(None)  # 499ns -> 438ns (13.9% faster)


def test_wrap_tuple():
    # Test passing a tuple (should wrap as a single element)
    codeflash_output = wrap_in_list((1, 2))  # 483ns -> 464ns (4.09% faster)


def test_wrap_dict():
    # Test passing a dictionary (should wrap as a single element)
    d = {"a": 1}
    codeflash_output = wrap_in_list(d)  # 495ns -> 547ns (9.51% slower)


def test_wrap_nested_list():
    # Test passing a nested list (should not flatten)
    codeflash_output = wrap_in_list([[1, 2], [3, 4]])  # 408ns -> 316ns (29.1% faster)


def test_wrap_list_within_list():
    # Test passing a list containing a list (should return as is)
    codeflash_output = wrap_in_list([["nested"]])  # 393ns -> 304ns (29.3% faster)


def test_wrap_bool_true():
    # Test passing True (should wrap in a list)
    codeflash_output = wrap_in_list(True)  # 522ns -> 595ns (12.3% slower)


def test_wrap_bool_false():
    # Test passing False (should wrap in a list)
    codeflash_output = wrap_in_list(False)  # 508ns -> 529ns (3.97% slower)


def test_wrap_bytes():
    # Test passing bytes (should wrap in a list)
    b = b"bytes"
    codeflash_output = wrap_in_list(b)  # 529ns -> 550ns (3.82% slower)


def test_wrap_set():
    # Test passing a set (should wrap in a list)
    s = {1, 2, 3}
    codeflash_output = wrap_in_list(s)  # 489ns -> 507ns (3.55% slower)


def test_wrap_custom_object():
    # Test passing a custom object (should wrap in a list)
    class Foo:
        pass

    foo = Foo()
    codeflash_output = wrap_in_list(foo)  # 522ns -> 593ns (12.0% slower)


def test_wrap_subclass_of_list():
    # Test passing a subclass of list (should return as is)
    class MyList(list):
        pass

    ml = MyList([1, 2, 3])
    codeflash_output = wrap_in_list(ml)  # 463ns -> 384ns (20.6% faster)


def test_wrap_list_is_not_copied():
    # Ensure the returned list is the same object if input is a list
    lst = [1, 2, 3]
    codeflash_output = wrap_in_list(lst)
    result = codeflash_output  # 437ns -> 340ns (28.5% faster)


def test_wrap_frozenset():
    # Test passing a frozenset (should wrap in a list)
    fs = frozenset([1, 2])
    codeflash_output = wrap_in_list(fs)  # 529ns -> 693ns (23.7% slower)


# -------------------------
# 3. Large Scale Test Cases
# -------------------------


def test_wrap_large_list():
    # Test passing a large list (should return as is)
    large_list = list(range(1000))
    codeflash_output = wrap_in_list(large_list)  # 462ns -> 382ns (20.9% faster)


def test_wrap_large_string():
    # Test passing a large string (should wrap in a list)
    large_str = "x" * 1000
    codeflash_output = wrap_in_list(large_str)  # 565ns -> 446ns (26.7% faster)


def test_wrap_large_dict():
    # Test passing a large dictionary (should wrap in a list)
    large_dict = {str(i): i for i in range(1000)}
    codeflash_output = wrap_in_list(large_dict)  # 518ns -> 532ns (2.63% slower)


def test_wrap_large_tuple():
    # Test passing a large tuple (should wrap in a list)
    large_tuple = tuple(range(1000))
    codeflash_output = wrap_in_list(large_tuple)  # 564ns -> 544ns (3.68% faster)


def test_wrap_large_nested_list():
    # Test passing a large nested list (should not flatten)
    nested = [[i] for i in range(1000)]
    codeflash_output = wrap_in_list(nested)  # 465ns -> 390ns (19.2% faster)


def test_wrap_many_small_lists():
    # Test passing a list of many small lists (should return as is)
    many_lists = [[i] for i in range(1000)]
    codeflash_output = wrap_in_list(many_lists)  # 452ns -> 388ns (16.5% faster)


# -------------------------
# 4. Type Robustness & Mutation Protection
# -------------------------


@pytest.mark.parametrize(
    "input_val,expected",
    [
        (42, [42]),
        ([42], [42]),
        ("foo", ["foo"]),
        (["foo"], ["foo"]),
        ([], []),
        (None, [None]),
        ([None], [None]),
        (0, [0]),
        ([0], [0]),
        (False, [False]),
        ([False], [False]),
    ],
)
def test_various_types_and_idempotency(input_val, expected):
    # Test various types and that wrap_in_list is idempotent for lists
    codeflash_output = wrap_in_list(input_val)  # 5.32μs -> 4.78μs (11.3% faster)


def test_mutation_of_returned_list_does_not_affect_input():
    # Test that wrapping a non-list returns a new list, and mutating it does not affect the original
    val = 123
    codeflash_output = wrap_in_list(val)
    result = codeflash_output  # 519ns -> 480ns (8.12% faster)
    result.append(456)


def test_mutation_of_input_list_affects_output():
    # Test that if input is a list, the returned value is the same object (not a copy)
    lst = [1]
    codeflash_output = wrap_in_list(lst)
    result = codeflash_output  # 430ns -> 342ns (25.7% faster)
    lst.append(2)


# -------------------------
# 5. Error Handling / Unusual Types
# -------------------------


def test_wrap_in_list_with_object_that_raises_on_type():
    # Test with an object whose type raises an exception on issubclass
    class Weird:
        def __class_getitem__(cls, item):
            raise Exception("Should not be called")

    w = Weird()
    # Should not raise
    codeflash_output = wrap_in_list(w)
    result = codeflash_output  # 550ns -> 535ns (2.80% faster)


def test_wrap_in_list_with_list_subclass_instance():
    # Test with an instance of a list subclass
    class MyList(list):
        pass

    ml = MyList([1, 2, 3])
    codeflash_output = wrap_in_list(ml)  # 464ns -> 386ns (20.2% faster)
    codeflash_output = wrap_in_list(ml)  # 189ns -> 169ns (11.8% faster)


def test_wrap_in_list_with_list_type_object():
    # Test with the list type itself (not an instance)
    codeflash_output = wrap_in_list(list)  # 531ns -> 455ns (16.7% faster)


# 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-wrap_in_list-miqsx5lr and push.

Codeflash Static Badge

The optimization replaces `issubclass(type(element), list)` with `isinstance(element, list)`, resulting in an 11% speedup.

**Key optimization**: The original code uses `issubclass(type(element), list)` to check if an element is a list, which involves two function calls: `type()` to get the object's type, then `issubclass()` to check inheritance. The optimized version uses `isinstance(element, list)`, which is a single, more direct type check operation.

**Why it's faster**: `isinstance()` is implemented more efficiently in Python's C code and avoids the overhead of the `type()` + `issubclass()` chain. The line profiler shows the condition check time reduced from 107,350ns to 84,656ns (21% faster on that line alone).

**Function usage context**: This function is called in hot paths within the inference pipeline - specifically in `render_boxes()`, `send_predictions()`, and `active_learning_sink()` methods that process video frames and predictions. Since these methods are called frequently during video processing, the 11% improvement compounds significantly.

**Test case performance**: The optimization shows consistent improvements across most test cases, with particularly strong gains for list inputs (24-41% faster) where the isinstance check immediately returns True. Non-list inputs see 7-17% improvements. A few edge cases with dict/bool inputs show slight regressions (1-14% slower), but these are likely within measurement noise and outweighed by the overall gains.

**Behavioral preservation**: Both approaches correctly handle list subclasses and maintain identical functionality - `isinstance()` properly recognizes subclasses of `list` just like the original `issubclass(type(x), list)` approach.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 December 4, 2025 02:13
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Dec 4, 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