Skip to content

Conversation

@codeflash-ai
Copy link

@codeflash-ai codeflash-ai bot commented Oct 30, 2025

📄 121% (1.21x) speedup for is_tokens_or_list_of_tokens in litellm/llms/openai/completion/utils.py

⏱️ Runtime : 3.55 milliseconds 1.60 milliseconds (best of 188 runs)

📝 Explanation and details

The optimized code achieves a 121% speedup by replacing expensive all() calls with explicit for-loops that provide better early exit behavior.

Key optimizations:

  1. Eliminated all() overhead: The original code uses all(isinstance(item, int) for item in value) and all(isinstance(item, list) and all(isinstance(i, int) for i in item) for item in value). These all() calls create generator expressions and incur Python function call overhead for each element check.

  2. Explicit loops with early exit: The optimized version uses manual for-loops with break statements that immediately exit when a non-matching type is found. This avoids the generator creation overhead and provides more efficient short-circuiting.

  3. Better loop control flow: Uses Python's for-else construct where the else clause executes only if the loop completes without hitting a break, providing cleaner early-exit logic.

Performance gains by test case type:

  • Small lists: 60-160% faster due to reduced function call overhead
  • Large lists with early failures: 140-280% faster due to immediate short-circuiting when non-int/non-list elements are found early
  • Edge cases with mixed types: 100-150% faster as the explicit loops exit immediately upon finding invalid elements

The optimization is particularly effective for cases where invalid elements appear early in the list, as the manual loops can exit immediately rather than continuing to process remaining elements through the all() generator pipeline.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 93 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 3 Passed
📊 Tests Coverage 100.0%
🌀 Generated Regression Tests and Runtime
from typing import List

# imports
import pytest  # used for our unit tests
from litellm.llms.openai.completion.utils import is_tokens_or_list_of_tokens

# unit tests

# 1. Basic Test Cases

def test_single_list_of_ints():
    # Basic case: list of ints
    codeflash_output = is_tokens_or_list_of_tokens([1, 2, 3, 4]) # 1.32μs -> 685ns (92.4% faster)

def test_single_list_of_ints_empty():
    # Edge case: empty list of ints
    codeflash_output = is_tokens_or_list_of_tokens([]) # 924ns -> 416ns (122% faster)

def test_list_of_lists_of_ints():
    # Basic case: list of lists of ints
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [3, 4]]) # 2.72μs -> 1.03μs (163% faster)

def test_list_of_lists_of_ints_empty_inner():
    # Edge case: list of lists, some empty
    codeflash_output = is_tokens_or_list_of_tokens([[], [1, 2], []]) # 2.64μs -> 1.03μs (157% faster)

def test_list_of_lists_of_ints_empty_outer():
    # Edge case: completely empty outer list
    codeflash_output = is_tokens_or_list_of_tokens([]) # 907ns -> 481ns (88.6% faster)

def test_single_list_of_non_ints():
    # Basic case: list of non-ints
    codeflash_output = is_tokens_or_list_of_tokens(['a', 'b', 'c']) # 1.96μs -> 788ns (149% faster)

def test_list_of_lists_of_non_ints():
    # Basic case: list of lists of non-ints
    codeflash_output = is_tokens_or_list_of_tokens([['a', 'b'], ['c', 'd']]) # 2.07μs -> 876ns (136% faster)

def test_list_of_mixed_types():
    # List with mixed types
    codeflash_output = is_tokens_or_list_of_tokens([1, 'a', 3]) # 1.90μs -> 967ns (96.8% faster)

def test_list_of_lists_mixed_types():
    # List of lists with mixed types
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], ['a', 3]]) # 2.54μs -> 1.07μs (136% faster)

def test_list_of_lists_non_list_element():
    # List of lists with a non-list element
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], 3]) # 2.23μs -> 1.07μs (109% faster)

def test_list_of_lists_with_empty_and_non_ints():
    # List of lists, some empty, some with non-int
    codeflash_output = is_tokens_or_list_of_tokens([[], ['a'], [1, 2]]) # 2.35μs -> 931ns (152% faster)

def test_single_list_of_floats():
    # List of floats
    codeflash_output = is_tokens_or_list_of_tokens([1.0, 2.0]) # 1.57μs -> 826ns (90.3% faster)

def test_list_of_lists_of_floats():
    # List of lists of floats
    codeflash_output = is_tokens_or_list_of_tokens([[1.0, 2.0], [3.0]]) # 1.91μs -> 916ns (108% faster)

def test_single_list_of_bools():
    # List of bools (bool is subclass of int, but should not be accepted)
    codeflash_output = is_tokens_or_list_of_tokens([True, False]) # 1.15μs -> 663ns (74.1% faster)

def test_list_of_lists_of_bools():
    # List of lists of bools
    codeflash_output = is_tokens_or_list_of_tokens([[True], [False]]) # 2.04μs -> 1.02μs (98.5% faster)

def test_single_list_of_int_and_bool():
    # List of ints and bools mixed
    codeflash_output = is_tokens_or_list_of_tokens([1, True, 3]) # 1.11μs -> 691ns (60.6% faster)

def test_list_of_lists_of_int_and_bool():
    # List of lists with ints and bools mixed
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [True, 4]]) # 2.35μs -> 1.09μs (116% faster)

def test_not_a_list():
    # Input is not a list
    codeflash_output = is_tokens_or_list_of_tokens(123) # 565ns -> 594ns (4.88% slower)
    codeflash_output = is_tokens_or_list_of_tokens('string') # 301ns -> 317ns (5.05% slower)
    codeflash_output = is_tokens_or_list_of_tokens(None) # 187ns -> 200ns (6.50% slower)
    codeflash_output = is_tokens_or_list_of_tokens({1: 2, 3: 4}) # 233ns -> 235ns (0.851% slower)
    codeflash_output = is_tokens_or_list_of_tokens((1, 2, 3)) # 245ns -> 256ns (4.30% slower)

# 2. Edge Test Cases

def test_list_with_nested_lists_deeper_than_two():
    # List with nested lists deeper than two levels
    codeflash_output = is_tokens_or_list_of_tokens([[[1, 2]], [[3]]]) # 2.15μs -> 886ns (143% faster)

def test_list_with_empty_inner_lists_only():
    # List of lists, all empty
    codeflash_output = is_tokens_or_list_of_tokens([[], [], []]) # 2.37μs -> 915ns (159% faster)

def test_list_with_empty_and_non_empty_inner_lists():
    # List of lists, some empty, some with ints
    codeflash_output = is_tokens_or_list_of_tokens([[], [1], [2, 3]]) # 2.63μs -> 1.12μs (135% faster)

def test_list_with_none_in_inner_list():
    # List of lists, one inner list contains None
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [None, 3]]) # 2.58μs -> 1.05μs (145% faster)

def test_list_with_inner_list_as_non_list():
    # List of lists, one inner item is not a list
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], 3, [4, 5]]) # 2.19μs -> 1.08μs (103% faster)

def test_list_with_inner_list_as_tuple():
    # List of lists, one inner item is a tuple
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], (3, 4)]) # 2.15μs -> 1.02μs (111% faster)

def test_list_with_inner_list_as_set():
    # List of lists, one inner item is a set
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], {3, 4}]) # 2.12μs -> 912ns (133% faster)

def test_list_with_inner_list_as_dict():
    # List of lists, one inner item is a dict
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], {3: 4}]) # 2.06μs -> 957ns (115% faster)

def test_list_with_inner_list_with_mixed_types():
    # List of lists, one inner list has mixed types
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [3, 'a']]) # 2.38μs -> 1.05μs (126% faster)

def test_list_with_inner_list_with_floats():
    # List of lists, one inner list has floats
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [3.0, 4]]) # 2.33μs -> 1.17μs (100% faster)

def test_list_with_inner_list_with_bools():
    # List of lists, one inner list has bools
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [True, 4]]) # 2.12μs -> 1.04μs (105% faster)

def test_list_of_lists_with_all_empty_lists():
    # List of lists, all empty lists
    codeflash_output = is_tokens_or_list_of_tokens([[], [], []]) # 2.20μs -> 931ns (136% faster)

def test_list_with_inner_list_empty_and_non_int():
    # List of lists, one empty, one non-int
    codeflash_output = is_tokens_or_list_of_tokens([[], ['a']]) # 2.23μs -> 925ns (141% faster)

def test_list_with_inner_list_empty_and_int():
    # List of lists, one empty, one int list
    codeflash_output = is_tokens_or_list_of_tokens([[], [1, 2]]) # 2.11μs -> 965ns (118% faster)

# 3. Large Scale Test Cases

def test_large_list_of_ints():
    # Large list of ints
    large_list = list(range(1000))
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 28.7μs -> 19.6μs (46.6% faster)

def test_large_list_of_lists_of_ints():
    # Large list of lists of ints
    large_list = [[i, i+1] for i in range(0, 1000, 2)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 95.1μs -> 36.7μs (159% faster)

def test_large_list_of_lists_with_empty_lists():
    # Large list of empty lists
    large_list = [[] for _ in range(1000)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 130μs -> 34.0μs (284% faster)

def test_large_list_of_lists_with_non_ints():
    # Large list of lists, one with non-int
    large_list = [[i, i+1] for i in range(0, 998, 2)] + [['a', 'b']]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 95.2μs -> 36.7μs (160% faster)

def test_large_list_of_ints_with_non_int():
    # Large list of ints, one non-int
    large_list = list(range(999)) + ['a']
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 29.7μs -> 20.0μs (48.1% faster)

def test_large_list_of_lists_with_bool():
    # Large list of lists, one with bool
    large_list = [[i, i+1] for i in range(0, 998, 2)] + [[True, 2]]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 95.4μs -> 36.6μs (161% faster)

def test_large_list_of_lists_deeper_nesting():
    # Large list of lists, with deeper nesting in one element
    large_list = [[i, i+1] for i in range(0, 998, 2)] + [[[1, 2]]]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 95.4μs -> 36.4μs (162% faster)

def test_large_list_of_lists_with_empty_and_non_int():
    # Large list of lists, some empty, one with non-int
    large_list = [[] for _ in range(999)] + [['a']]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 130μs -> 34.1μs (284% faster)

def test_large_list_of_lists_all_empty():
    # Large list of lists, all empty
    large_list = [[] for _ in range(1000)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 131μs -> 34.0μs (286% faster)

def test_large_list_of_lists_some_empty_some_ints():
    # Large list of lists, some empty, some with ints
    large_list = [[] for _ in range(500)] + [[i, i+1] for i in range(0, 500, 2)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 112μs -> 35.6μs (214% 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

# imports
import pytest  # used for our unit tests
from litellm.llms.openai.completion.utils import is_tokens_or_list_of_tokens

# unit tests

# 1. Basic Test Cases

def test_single_list_of_ints():
    # Basic: a list of tokens
    codeflash_output = is_tokens_or_list_of_tokens([1, 2, 3]) # 1.15μs -> 624ns (84.0% faster)

def test_single_list_of_ints_empty():
    # Basic: an empty list of tokens (should be valid)
    codeflash_output = is_tokens_or_list_of_tokens([]) # 899ns -> 405ns (122% faster)

def test_list_of_lists_of_ints():
    # Basic: a list of lists of tokens
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [3, 4]]) # 2.52μs -> 1.06μs (137% faster)

def test_list_of_lists_of_ints_empty_inner():
    # Basic: a list of lists with empty inner lists
    codeflash_output = is_tokens_or_list_of_tokens([[], []]) # 2.10μs -> 876ns (139% faster)

def test_list_of_lists_of_ints_empty_outer():
    # Basic: an empty list of lists
    codeflash_output = is_tokens_or_list_of_tokens([]) # 862ns -> 422ns (104% faster)

def test_mixed_list_of_ints_and_lists():
    # Basic: a list with both ints and lists (should be invalid)
    codeflash_output = is_tokens_or_list_of_tokens([1, [2, 3]]) # 2.09μs -> 936ns (124% faster)

def test_non_list_input():
    # Basic: input is not a list
    codeflash_output = is_tokens_or_list_of_tokens(123) # 548ns -> 572ns (4.20% slower)
    codeflash_output = is_tokens_or_list_of_tokens("string") # 319ns -> 323ns (1.24% slower)
    codeflash_output = is_tokens_or_list_of_tokens(None) # 188ns -> 203ns (7.39% slower)

def test_list_of_non_ints():
    # Basic: list of non-int types
    codeflash_output = is_tokens_or_list_of_tokens(["a", "b", "c"]) # 1.81μs -> 739ns (145% faster)
    codeflash_output = is_tokens_or_list_of_tokens([1.1, 2.2]) # 861ns -> 606ns (42.1% faster)

def test_list_of_lists_of_non_ints():
    # Basic: list of lists containing non-int types
    codeflash_output = is_tokens_or_list_of_tokens([["a", "b"], ["c", "d"]]) # 2.00μs -> 837ns (139% faster)
    codeflash_output = is_tokens_or_list_of_tokens([[1.1, 2.2], [3.3]]) # 992ns -> 540ns (83.7% faster)

# 2. Edge Test Cases

def test_list_with_bool_values():
    # Edge: bools are subclasses of int, but should be rejected
    codeflash_output = is_tokens_or_list_of_tokens([True, False]) # 1.17μs -> 672ns (74.6% faster)
    codeflash_output = is_tokens_or_list_of_tokens([[True, False], [1, 2]]) # 1.72μs -> 900ns (91.6% faster)

def test_list_with_mixed_int_and_bool():
    # Edge: list with both int and bool
    codeflash_output = is_tokens_or_list_of_tokens([1, True]) # 999ns -> 626ns (59.6% faster)

def test_list_of_lists_with_mixed_int_and_bool():
    # Edge: list of lists with both int and bool
    codeflash_output = is_tokens_or_list_of_tokens([[1, True], [2, False]]) # 2.17μs -> 1.09μs (98.0% faster)

def test_list_with_none():
    # Edge: list containing None
    codeflash_output = is_tokens_or_list_of_tokens([None, 1, 2]) # 1.70μs -> 723ns (136% faster)

def test_list_of_lists_with_none():
    # Edge: list of lists containing None
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], [None, 3]]) # 2.51μs -> 1.05μs (139% faster)

def test_list_with_empty_inner_and_non_list():
    # Edge: list with empty list and non-list element
    codeflash_output = is_tokens_or_list_of_tokens([[], 1]) # 2.23μs -> 985ns (126% faster)

def test_list_with_nested_lists_more_than_2_levels():
    # Edge: list of lists of lists (should be invalid)
    codeflash_output = is_tokens_or_list_of_tokens([[[1, 2]], [[3, 4]]]) # 1.94μs -> 816ns (138% faster)

def test_list_with_dicts():
    # Edge: list containing dicts
    codeflash_output = is_tokens_or_list_of_tokens([{}, {}]) # 1.56μs -> 747ns (109% faster)
    codeflash_output = is_tokens_or_list_of_tokens([[1, 2], {}]) # 1.44μs -> 717ns (101% faster)

def test_list_with_tuple():
    # Edge: list containing tuple
    codeflash_output = is_tokens_or_list_of_tokens([(1, 2), (3, 4)]) # 1.61μs -> 828ns (94.9% faster)

def test_list_with_set():
    # Edge: list containing set
    codeflash_output = is_tokens_or_list_of_tokens([{1, 2}, {3, 4}]) # 1.55μs -> 695ns (123% faster)

def test_list_with_float_and_int():
    # Edge: list containing both float and int
    codeflash_output = is_tokens_or_list_of_tokens([1, 2.0]) # 1.79μs -> 894ns (99.8% faster)

def test_list_with_str_and_int():
    # Edge: list containing both str and int
    codeflash_output = is_tokens_or_list_of_tokens([1, "2"]) # 1.75μs -> 842ns (108% faster)

def test_list_of_lists_with_empty_and_non_empty():
    # Edge: list of lists with empty and non-empty lists
    codeflash_output = is_tokens_or_list_of_tokens([[], [1, 2]]) # 2.31μs -> 948ns (143% faster)

def test_list_of_lists_with_empty_and_non_list():
    # Edge: list of lists with empty list and non-list element
    codeflash_output = is_tokens_or_list_of_tokens([[], 1]) # 2.07μs -> 923ns (124% faster)

def test_list_with_generator():
    # Edge: list containing a generator
    codeflash_output = is_tokens_or_list_of_tokens([i for i in range(3)]) # 1.10μs -> 617ns (78.8% faster)

def test_list_with_large_ints():
    # Edge: list with very large integers
    codeflash_output = is_tokens_or_list_of_tokens([2**64, 2**128]) # 1.08μs -> 584ns (85.8% faster)

def test_list_with_negative_ints():
    # Edge: list with negative integers
    codeflash_output = is_tokens_or_list_of_tokens([-1, -2, -3]) # 1.10μs -> 623ns (77.4% faster)

def test_list_of_lists_with_negative_ints():
    # Edge: list of lists with negative integers
    codeflash_output = is_tokens_or_list_of_tokens([[-1, -2], [-3, -4]]) # 2.51μs -> 1.10μs (127% faster)

def test_list_with_unicode_ints():
    # Edge: list with unicode digits as strings
    codeflash_output = is_tokens_or_list_of_tokens(["1", "2", "3"]) # 1.83μs -> 756ns (142% faster)

# 3. Large Scale Test Cases

def test_large_list_of_ints():
    # Large Scale: large list of ints
    large_list = list(range(1000))
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 28.7μs -> 19.6μs (46.2% faster)

def test_large_list_of_lists_of_ints():
    # Large Scale: large list of lists of ints
    large_list_of_lists = [list(range(10)) for _ in range(100)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 45.4μs -> 23.4μs (94.0% faster)

def test_large_list_of_lists_with_empty_lists():
    # Large Scale: large list of empty lists
    large_list_of_empty_lists = [[] for _ in range(1000)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_empty_lists) # 130μs -> 34.1μs (281% faster)

def test_large_list_of_lists_with_mixed_empty_and_non_empty():
    # Large Scale: large list of lists, half empty, half non-empty
    large_mixed = [[] for _ in range(500)] + [list(range(10)) for _ in range(500)]
    codeflash_output = is_tokens_or_list_of_tokens(large_mixed) # 280μs -> 129μs (117% faster)

def test_large_list_with_non_int_at_end():
    # Large Scale: large list of ints with a non-int at the end
    large_list = list(range(999)) + ["a"]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 29.5μs -> 20.0μs (47.6% faster)

def test_large_list_of_lists_with_non_int_inner():
    # Large Scale: large list of lists, one inner list contains a non-int
    large_list_of_lists = [list(range(10)) for _ in range(999)] + [["a", "b"]]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 428μs -> 224μs (91.2% faster)

def test_large_list_of_lists_with_non_list():
    # Large Scale: large list of lists, one element is not a list
    large_list_of_lists = [list(range(10)) for _ in range(999)] + [123]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 431μs -> 223μs (93.0% faster)

def test_large_list_of_lists_with_empty_and_non_list():
    # Large Scale: large list of lists, one element is not a list
    large_list_of_lists = [[] for _ in range(999)] + [123]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 130μs -> 34.2μs (282% faster)

def test_large_list_of_lists_with_bool_inner():
    # Large Scale: large list of lists, one inner list contains bool
    large_list_of_lists = [list(range(10)) for _ in range(999)] + [[True, False]]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 428μs -> 223μs (91.6% faster)

def test_large_list_of_lists_with_none_inner():
    # Large Scale: large list of lists, one inner list contains None
    large_list_of_lists = [list(range(10)) for _ in range(999)] + [[None, 1]]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 431μs -> 223μs (93.0% faster)

def test_large_list_of_lists_with_negative_ints():
    # Large Scale: large list of lists with negative ints
    large_list_of_lists = [[-i for i in range(10)] for _ in range(100)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 45.1μs -> 23.3μs (93.6% faster)

def test_large_list_with_negative_ints():
    # Large Scale: large list of negative ints
    large_list = [-i for i in range(1000)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list) # 28.8μs -> 19.5μs (47.9% faster)

def test_large_list_of_lists_with_large_ints():
    # Large Scale: large list of lists with large ints
    large_list_of_lists = [[2**64 + i for i in range(10)] for _ in range(100)]
    codeflash_output = is_tokens_or_list_of_tokens(large_list_of_lists) # 45.1μs -> 23.6μs (91.2% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
#------------------------------------------------
from litellm.llms.openai.completion.utils import is_tokens_or_list_of_tokens

def test_is_tokens_or_list_of_tokens():
    is_tokens_or_list_of_tokens([''])

def test_is_tokens_or_list_of_tokens_2():
    is_tokens_or_list_of_tokens([])

def test_is_tokens_or_list_of_tokens_3():
    is_tokens_or_list_of_tokens([[]])
🔎 Concolic Coverage Tests and Runtime
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
codeflash_concolic_kt42dg31/tmpv2flhdwh/test_concolic_coverage.py::test_is_tokens_or_list_of_tokens 1.69μs 795ns 112%✅
codeflash_concolic_kt42dg31/tmpv2flhdwh/test_concolic_coverage.py::test_is_tokens_or_list_of_tokens_2 896ns 490ns 82.9%✅
codeflash_concolic_kt42dg31/tmpv2flhdwh/test_concolic_coverage.py::test_is_tokens_or_list_of_tokens_3 1.87μs 850ns 120%✅

To edit these changes git checkout codeflash/optimize-is_tokens_or_list_of_tokens-mhddzgju and push.

Codeflash Static Badge

The optimized code achieves a **121% speedup** by replacing expensive `all()` calls with explicit for-loops that provide better early exit behavior.

**Key optimizations:**

1. **Eliminated `all()` overhead**: The original code uses `all(isinstance(item, int) for item in value)` and `all(isinstance(item, list) and all(isinstance(i, int) for i in item) for item in value)`. These `all()` calls create generator expressions and incur Python function call overhead for each element check.

2. **Explicit loops with early exit**: The optimized version uses manual for-loops with `break` statements that immediately exit when a non-matching type is found. This avoids the generator creation overhead and provides more efficient short-circuiting.

3. **Better loop control flow**: Uses Python's `for-else` construct where the `else` clause executes only if the loop completes without hitting a `break`, providing cleaner early-exit logic.

**Performance gains by test case type:**
- **Small lists**: 60-160% faster due to reduced function call overhead
- **Large lists with early failures**: 140-280% faster due to immediate short-circuiting when non-int/non-list elements are found early
- **Edge cases with mixed types**: 100-150% faster as the explicit loops exit immediately upon finding invalid elements

The optimization is particularly effective for cases where invalid elements appear early in the list, as the manual loops can exit immediately rather than continuing to process remaining elements through the `all()` generator pipeline.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 30, 2025 12:14
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: Medium Optimization Quality according to Codeflash labels Oct 30, 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: Medium Optimization Quality according to Codeflash

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant