Skip to content

⚡️ Speed up function is_true by 6%#51

Open
codeflash-ai[bot] wants to merge 1 commit intomainfrom
codeflash/optimize-is_true-mgzngj7c
Open

⚡️ Speed up function is_true by 6%#51
codeflash-ai[bot] wants to merge 1 commit intomainfrom
codeflash/optimize-is_true-mgzngj7c

Conversation

@codeflash-ai
Copy link
Copy Markdown

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

📄 6% (0.06x) speedup for is_true in pr_agent/servers/github_action_runner.py

⏱️ Runtime : 36.7 milliseconds 34.5 milliseconds (best of 92 runs)

📝 Explanation and details

The optimized code achieves a 6% speedup by eliminating redundant type checking and simplifying the string handling logic.

Key Optimizations:

  1. Consolidated Type Checking: Replaced the original's separate type(value) is str and isinstance(value, str) checks with a single isinstance(value, str) call. This handles both exact strings and string subclasses in one operation, eliminating unnecessary branching and variable assignments.

  2. Removed Redundant Variable Assignment: The original code assigned val = value twice in different branches, then used val for subsequent operations. The optimized version directly uses value, reducing stack operations and namespace lookups.

  3. Streamlined Control Flow: Simplified from multiple conditional branches to a cleaner linear flow - check bool type, then string type with early length validation, then case-insensitive comparison.

Why This is Faster:

  • Fewer Python bytecode instructions due to eliminated redundant checks and assignments
  • Single isinstance() call is more efficient than the original's dual type checking approach
  • Direct use of value instead of val reduces variable lookup overhead
  • Early return on length check remains optimal for non-4-character strings

Test Case Performance:
The optimization particularly excels with:

  • String subclasses (5-15% faster): Single isinstance() check handles inheritance efficiently
  • Large-scale processing (7-8% faster): Reduced per-iteration overhead compounds significantly
  • Mixed case 'true' variants (2-4% faster): Streamlined path to lower() comparison
  • Non-string types remain comparable, with bool operations showing minimal overhead

The optimization maintains all original functionality while reducing computational complexity through cleaner type handling and elimination of redundant operations.

Correctness verification report:

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

# imports
import pytest  # used for our unit tests
from pr_agent.servers.github_action_runner import is_true

# unit tests

# --- Basic Test Cases ---

def test_bool_true():
    # Should return True for boolean True
    codeflash_output = is_true(True) # 14.7μs -> 14.8μs (0.830% slower)

def test_bool_false():
    # Should return False for boolean False
    codeflash_output = is_true(False) # 12.0μs -> 12.4μs (2.82% slower)

def test_string_true_lowercase():
    # Should return True for string 'true'
    codeflash_output = is_true('true') # 14.5μs -> 14.0μs (3.75% faster)

def test_string_true_uppercase():
    # Should return True for string 'TRUE'
    codeflash_output = is_true('TRUE') # 13.8μs -> 13.5μs (2.38% faster)

def test_string_true_mixedcase():
    # Should return True for string 'TrUe'
    codeflash_output = is_true('TrUe') # 14.4μs -> 13.8μs (3.85% faster)

def test_string_false():
    # Should return False for string 'false'
    codeflash_output = is_true('false') # 13.6μs -> 13.1μs (3.58% faster)

def test_string_yes():
    # Should return False for string 'yes'
    codeflash_output = is_true('yes') # 13.3μs -> 13.4μs (0.766% slower)

def test_string_no():
    # Should return False for string 'no'
    codeflash_output = is_true('no') # 13.5μs -> 13.4μs (0.568% faster)

def test_string_empty():
    # Should return False for empty string
    codeflash_output = is_true('') # 12.6μs -> 12.8μs (1.23% slower)

def test_string_true_with_space():
    # Should return False for string 'true ' (with trailing space)
    codeflash_output = is_true('true ') # 13.1μs -> 13.1μs (0.276% faster)

def test_string_true_with_leading_space():
    # Should return False for string ' true' (with leading space)
    codeflash_output = is_true(' true') # 13.4μs -> 12.9μs (3.53% faster)

def test_string_true_with_internal_space():
    # Should return False for string 'tr ue'
    codeflash_output = is_true('tr ue') # 13.2μs -> 12.9μs (2.11% faster)

def test_string_true_substring():
    # Should return False for string 'tru'
    codeflash_output = is_true('tru') # 13.6μs -> 13.3μs (2.29% faster)

def test_string_true_longer():
    # Should return False for string 'truely'
    codeflash_output = is_true('truely') # 13.5μs -> 13.3μs (1.49% faster)

# --- Edge Test Cases ---

def test_non_string_non_bool_int():
    # Should return False for integer input
    codeflash_output = is_true(1) # 12.8μs -> 12.8μs (0.102% faster)

def test_non_string_non_bool_float():
    # Should return False for float input
    codeflash_output = is_true(1.0) # 12.7μs -> 12.8μs (1.17% slower)

def test_non_string_non_bool_none():
    # Should return False for None
    codeflash_output = is_true(None) # 12.8μs -> 12.7μs (0.954% faster)

def test_non_string_non_bool_list():
    # Should return False for list input
    codeflash_output = is_true(['true']) # 12.9μs -> 12.8μs (0.924% faster)

def test_non_string_non_bool_dict():
    # Should return False for dict input
    codeflash_output = is_true({'true': True}) # 13.1μs -> 13.0μs (0.361% faster)

def test_string_true_unicode():
    # Should return False for unicode string with similar letters but different codepoints
    codeflash_output = is_true('tru\u00e9') # 14.7μs -> 15.0μs (1.94% slower)

def test_string_true_subclass():
    # Should return True for subclass of str containing 'true'
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('true')) # 15.2μs -> 14.5μs (5.44% faster)

def test_string_true_subclass_mixedcase():
    # Should return True for subclass of str containing 'TrUe'
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('TrUe')) # 15.2μs -> 14.8μs (2.43% faster)

def test_string_true_subclass_wrong():
    # Should return False for subclass of str containing 'truely'
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('truely')) # 14.4μs -> 13.5μs (6.14% faster)

def test_bytes_input():
    # Should return False for bytes input
    codeflash_output = is_true(b'true') # 13.3μs -> 13.1μs (1.58% faster)

def test_string_true_with_newline():
    # Should return False for 'true\n'
    codeflash_output = is_true('true\n') # 13.2μs -> 13.4μs (1.15% slower)

def test_string_true_with_tab():
    # Should return False for 'true\t'
    codeflash_output = is_true('true\t') # 13.6μs -> 13.1μs (3.54% faster)

def test_string_true_with_special_char():
    # Should return False for 'tru€'
    codeflash_output = is_true('tru€') # 14.2μs -> 14.3μs (0.609% slower)

def test_string_true_case_variants():
    # Should return True for all 16 case variants of 'true'
    from itertools import product
    base = 'true'
    for bits in product(*[(c.lower(), c.upper()) for c in base]):
        variant = ''.join(bits)
        codeflash_output = is_true(variant) # 98.2μs -> 91.8μs (6.89% faster)

def test_string_true_wrong_length():
    # Should return False for strings of length not 4
    codeflash_output = is_true('t') # 13.1μs -> 12.7μs (3.10% faster)
    codeflash_output = is_true('tr') # 7.12μs -> 6.84μs (4.03% faster)
    codeflash_output = is_true('tru') # 5.65μs -> 5.21μs (8.46% faster)
    codeflash_output = is_true('truee') # 5.36μs -> 5.15μs (4.04% faster)

# --- Large Scale Test Cases ---

def test_large_list_of_true_strings():
    # Should return True for all 'true' variants in a large list
    variants = ['true', 'TRUE', 'TrUe', 'tRuE', 'truE', 'TRue', 'trUE', 'TruE']
    large_list = variants * (1000 // len(variants))
    for s in large_list:
        codeflash_output = is_true(s) # 5.35ms -> 4.96ms (7.97% faster)

def test_large_list_of_false_strings():
    # Should return False for all non-'true' strings in a large list
    non_true = ['false', 'yes', 'no', '', 'tru', 'truely', 't rue', 'true ', ' true']
    large_list = non_true * (1000 // len(non_true))
    for s in large_list:
        codeflash_output = is_true(s) # 5.34ms -> 4.95ms (7.85% faster)

def test_large_list_of_booleans():
    # Should return True for True and False for False in a large list
    bools = [True, False] * 500
    for b in bools:
        codeflash_output = is_true(b) # 4.04ms -> 4.12ms (1.92% slower)

def test_large_list_of_mixed_types():
    # Should return correct values for a large list of mixed types
    mixed = ['true', 'TRUE', 'false', '', True, False, None, 0, 1, [], {}, 'truely', 'TrUe']
    large_list = mixed * (1000 // len(mixed))
    for v in large_list:
        expected = (
            True if (type(v) is bool and v is True)
            else False if (type(v) is bool and v is False)
            else True if (isinstance(v, str) and len(v) == 4 and v.lower() == 'true')
            else False
        )
        codeflash_output = is_true(v) # 4.96ms -> 4.65ms (6.63% faster)


#------------------------------------------------
from typing import Union

# imports
import pytest  # used for our unit tests
from pr_agent.servers.github_action_runner import is_true

# unit tests

# 1. Basic Test Cases

def test_true_bool():
    # Should return True for boolean True
    codeflash_output = is_true(True) # 21.3μs -> 22.5μs (5.42% slower)

def test_false_bool():
    # Should return False for boolean False
    codeflash_output = is_true(False) # 13.5μs -> 13.8μs (2.71% slower)

def test_true_str_lowercase():
    # Should return True for string 'true'
    codeflash_output = is_true('true') # 15.3μs -> 15.2μs (0.770% faster)

def test_true_str_uppercase():
    # Should return True for string 'TRUE'
    codeflash_output = is_true('TRUE') # 14.3μs -> 14.2μs (0.554% faster)

def test_true_str_mixedcase():
    # Should return True for string 'TrUe'
    codeflash_output = is_true('TrUe') # 13.9μs -> 13.7μs (1.54% faster)

def test_false_str():
    # Should return False for string 'false'
    codeflash_output = is_true('false') # 14.0μs -> 13.7μs (2.39% faster)

def test_false_str_upper():
    # Should return False for string 'FALSE'
    codeflash_output = is_true('FALSE') # 13.4μs -> 13.3μs (0.791% faster)

def test_false_str_mixedcase():
    # Should return False for string 'FaLsE'
    codeflash_output = is_true('FaLsE') # 13.3μs -> 13.0μs (2.01% faster)

def test_true_str_extra_space():
    # Should return False for ' true' (leading space)
    codeflash_output = is_true(' true') # 13.4μs -> 13.7μs (1.65% slower)
    # Should return False for 'true ' (trailing space)
    codeflash_output = is_true('true ') # 7.47μs -> 7.33μs (1.79% faster)
    # Should return False for ' true ' (both sides)
    codeflash_output = is_true(' true ') # 5.79μs -> 5.59μs (3.54% faster)

def test_true_str_wrong_length():
    # Should return False for 'tru'
    codeflash_output = is_true('tru') # 12.9μs -> 12.5μs (3.01% faster)
    # Should return False for 'truue'
    codeflash_output = is_true('truue') # 7.09μs -> 6.83μs (3.73% faster)
    # Should return False for empty string
    codeflash_output = is_true('') # 5.64μs -> 5.43μs (3.81% faster)

def test_true_str_non_ascii():
    # Should return False for 'trüe' (non-ascii character)
    codeflash_output = is_true('trüe') # 13.4μs -> 13.2μs (1.28% faster)

# 2. Edge Test Cases

def test_true_str_subclass():
    # Should return True for str subclass containing 'TRUE'
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('TRUE')) # 15.2μs -> 14.0μs (8.19% faster)

def test_false_str_subclass():
    # Should return False for str subclass containing 'FALSE'
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('FALSE')) # 14.0μs -> 13.1μs (7.30% faster)

def test_non_str_non_bool():
    # Should return False for integer
    codeflash_output = is_true(1) # 12.6μs -> 12.9μs (2.51% slower)
    # Should return False for float
    codeflash_output = is_true(0.0) # 7.31μs -> 7.21μs (1.50% faster)
    # Should return False for None
    codeflash_output = is_true(None) # 5.55μs -> 5.24μs (6.03% faster)
    # Should return False for list
    codeflash_output = is_true(['true']) # 5.24μs -> 4.92μs (6.57% faster)
    # Should return False for dict
    codeflash_output = is_true({'true': True}) # 5.01μs -> 4.93μs (1.58% faster)

def test_str_with_special_characters():
    # Should return False for 'tru!' (special character)
    codeflash_output = is_true('tru!') # 12.9μs -> 12.4μs (3.95% faster)
    # Should return False for 'tr ue' (space in middle)
    codeflash_output = is_true('tr ue') # 7.17μs -> 6.86μs (4.55% faster)

def test_str_with_unicode_true():
    # Should return False for string '𝚝𝚛𝚞𝚎' (unicode bold)
    codeflash_output = is_true('𝚝𝚛𝚞𝚎') # 13.3μs -> 12.8μs (3.66% faster)

def test_str_with_newline():
    # Should return False for 'true\n'
    codeflash_output = is_true('true\n') # 12.7μs -> 12.2μs (4.07% faster)

def test_str_with_tab():
    # Should return False for 'true\t'
    codeflash_output = is_true('true\t') # 13.2μs -> 12.6μs (4.83% faster)


def test_str_with_numeric():
    # Should return False for '1234'
    codeflash_output = is_true('1234') # 23.9μs -> 24.3μs (1.60% slower)

def test_str_with_partial_true():
    # Should return False for 'truE'
    codeflash_output = is_true('truE') # 15.5μs -> 15.2μs (1.48% faster)
    # Should return False for 'tRue'
    codeflash_output = is_true('tRue') # 7.49μs -> 7.46μs (0.416% faster)

# 3. Large Scale Test Cases

def test_large_list_of_true_strings():
    # Should return all True for a list of 'true' strings in various cases
    cases = ['true', 'TRUE', 'TrUe', 'tRuE', 'truE', 'tRue', 'TRue', 'trUE']
    for s in cases:
        codeflash_output = is_true(s) # 54.4μs -> 51.3μs (6.10% faster)

def test_large_list_of_false_strings():
    # Should return all False for a list of strings not 'true'
    cases = ['false', 'nope', 'yes', 'tru', 'truee', '', 'ture', 'tr ue', 'trüe', 'tru!', 'true ', ' true']
    for s in cases:
        codeflash_output = is_true(s) # 76.3μs -> 72.1μs (5.82% faster)

def test_large_list_of_bools():
    # Should return True for all True, False for all False
    bools = [True, False] * 500  # 1000 elements
    for i, b in enumerate(bools):
        expected = True if i % 2 == 0 else False
        codeflash_output = is_true(b) # 4.04ms -> 4.12ms (1.97% slower)

def test_large_mixed_types():
    # Should return False for all non-str, non-bool types in a large list
    mixed = [None, 1, 0.0, [], {}, set(), object()] * 100
    for item in mixed:
        codeflash_output = is_true(item) # 3.45ms -> 3.22ms (7.16% faster)

def test_large_str_subclass():
    # Should return True for large list of str subclass instances with 'true'
    class MyStr(str): pass
    lst = [MyStr('TrUe')] * 500
    for s in lst:
        codeflash_output = is_true(s) # 2.91ms -> 2.52ms (15.5% faster)

def test_large_str_subclass_false():
    # Should return False for large list of str subclass instances with 'false'
    class MyStr(str): pass
    lst = [MyStr('FaLsE')] * 500
    for s in lst:
        codeflash_output = is_true(s) # 2.87ms -> 2.48ms (15.7% faster)

def test_large_random_strings():
    # Should return False for random 4-letter strings except 'true' in any case
    import random
    import string
    valid = set(['true', 'TRUE', 'TrUe', 'tRuE', 'truE', 'tRue', 'TRue', 'trUE'])
    for _ in range(500):
        s = ''.join(random.choices(string.ascii_letters, k=4))
        if s.lower() == 'true':
            codeflash_output = is_true(s)
        else:
            codeflash_output = is_true(s)
# 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-is_true-mgzngj7c and push.

Codeflash

The optimized code achieves a **6% speedup** by eliminating redundant type checking and simplifying the string handling logic.

**Key Optimizations:**

1. **Consolidated Type Checking**: Replaced the original's separate `type(value) is str` and `isinstance(value, str)` checks with a single `isinstance(value, str)` call. This handles both exact strings and string subclasses in one operation, eliminating unnecessary branching and variable assignments.

2. **Removed Redundant Variable Assignment**: The original code assigned `val = value` twice in different branches, then used `val` for subsequent operations. The optimized version directly uses `value`, reducing stack operations and namespace lookups.

3. **Streamlined Control Flow**: Simplified from multiple conditional branches to a cleaner linear flow - check bool type, then string type with early length validation, then case-insensitive comparison.

**Why This is Faster:**
- Fewer Python bytecode instructions due to eliminated redundant checks and assignments
- Single `isinstance()` call is more efficient than the original's dual type checking approach
- Direct use of `value` instead of `val` reduces variable lookup overhead
- Early return on length check remains optimal for non-4-character strings

**Test Case Performance:**
The optimization particularly excels with:
- **String subclasses** (5-15% faster): Single `isinstance()` check handles inheritance efficiently
- **Large-scale processing** (7-8% faster): Reduced per-iteration overhead compounds significantly
- **Mixed case 'true' variants** (2-4% faster): Streamlined path to `lower()` comparison
- **Non-string types** remain comparable, with bool operations showing minimal overhead

The optimization maintains all original functionality while reducing computational complexity through cleaner type handling and elimination of redundant operations.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 20, 2025 21:30
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 20, 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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

0 participants