Skip to content

⚡️ Speed up function is_true by 12%#34

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

⚡️ Speed up function is_true by 12%#34
codeflash-ai[bot] wants to merge 1 commit intomainfrom
codeflash/optimize-is_true-mgu91wly

Conversation

@codeflash-ai
Copy link
Copy Markdown

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

📄 12% (0.12x) speedup for is_true in pr_agent/servers/github_action_runner.py

⏱️ Runtime : 1.51 milliseconds 1.35 milliseconds (best of 115 runs)

📝 Explanation and details

The key optimization is adding a fast-path ASCII check for the exact string 'true' before falling back to the more expensive casefold() operation.

What specific optimizations were applied:

  1. Added ASCII fast-path: Check if val == 'true' directly before calling casefold()
  2. Removed local type aliases: Eliminated bool_type = bool and str_type = str assignments, using single vtype = type(value) assignment instead
  3. Early return optimization: Return True immediately when exact match is found

Why this leads to speedup:

  • String comparison (==) is much faster than casefold() for ASCII strings. The casefold() method performs Unicode normalization which has significant overhead even for simple ASCII strings
  • Most common case optimization: The test results show that lowercase 'true' strings get a 38% speedup, indicating this is a frequent case worth optimizing
  • Reduced variable assignments: Eliminating the local type aliases saves two assignment operations per function call

Test case performance patterns:

  • Best for lowercase 'true': 38% faster (most common valid case)
  • Good for non-4-character strings: 10-25% faster due to early length check
  • Slight regression for non-ASCII 'true' variants: 4-10% slower for uppercase/mixed case since they still need casefold(), but now go through an extra comparison step
  • Excellent for str subclasses: Up to 77% faster, likely due to reduced type checking overhead

The optimization prioritizes the most common valid input ('true') while maintaining correctness for all edge cases.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 10070 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():
    # Basic: input is boolean True
    codeflash_output = is_true(True) # 431ns -> 418ns (3.11% faster)

def test_bool_false():
    # Basic: input is boolean False
    codeflash_output = is_true(False) # 430ns -> 408ns (5.39% faster)

def test_str_true_lower():
    # Basic: input is string 'true' in lowercase
    codeflash_output = is_true('true') # 942ns -> 682ns (38.1% faster)

def test_str_true_upper():
    # Basic: input is string 'TRUE' in uppercase
    codeflash_output = is_true('TRUE') # 825ns -> 896ns (7.92% slower)

def test_str_true_mixed_case():
    # Basic: input is string 'TrUe' in mixed case
    codeflash_output = is_true('TrUe') # 807ns -> 842ns (4.16% slower)

def test_str_false_lower():
    # Basic: input is string 'false'
    codeflash_output = is_true('false') # 560ns -> 541ns (3.51% faster)

def test_str_other_word():
    # Basic: input is string 'yes'
    codeflash_output = is_true('yes') # 593ns -> 509ns (16.5% faster)

def test_str_empty():
    # Basic: input is empty string
    codeflash_output = is_true('') # 537ns -> 447ns (20.1% faster)

def test_str_true_with_spaces():
    # Basic: input is ' true ' (with spaces)
    codeflash_output = is_true(' true ') # 617ns -> 548ns (12.6% faster)

def test_str_true_with_leading_space():
    # Basic: input is ' true' (leading space)
    codeflash_output = is_true(' true') # 576ns -> 550ns (4.73% faster)

def test_str_true_with_trailing_space():
    # Basic: input is 'true ' (trailing space)
    codeflash_output = is_true('true ') # 569ns -> 525ns (8.38% faster)

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

def test_str_true_unicode():
    # Edge: input is 'true' with Unicode normalization
    codeflash_output = is_true('t\u0072\u0075\u0065') # 880ns -> 664ns (32.5% faster)

def test_str_true_casefold_equivalent():
    # Edge: input is 'truE' with Turkish dotted I
    codeflash_output = is_true('truE') # 825ns -> 879ns (6.14% slower)

def test_str_true_non_ascii():
    # Edge: input is 'trué' (accented character)
    codeflash_output = is_true('trué') # 1.23μs -> 1.27μs (3.61% slower)

def test_str_true_subclass():
    # Edge: input is a subclass of str
    class MyStr(str): pass
    codeflash_output = is_true(MyStr('true')) # 1.26μs -> 789ns (59.6% faster)
    codeflash_output = is_true(MyStr('TRUE')) # 436ns -> 939ns (53.6% slower)
    codeflash_output = is_true(MyStr('truE')) # 211ns -> 306ns (31.0% slower)
    codeflash_output = is_true(MyStr('false')) # 225ns -> 192ns (17.2% faster)


def test_str_true_length_variations():
    # Edge: input is 'true' with extra characters
    codeflash_output = is_true('true!') # 593ns -> 558ns (6.27% faster)
    codeflash_output = is_true('tru') # 485ns -> 440ns (10.2% faster)
    codeflash_output = is_true('truess') # 205ns -> 164ns (25.0% faster)
    codeflash_output = is_true('tr ue') # 244ns -> 206ns (18.4% faster)

def test_int_input():
    # Edge: input is integer
    codeflash_output = is_true(1) # 575ns -> 667ns (13.8% slower)
    codeflash_output = is_true(0) # 216ns -> 233ns (7.30% slower)

def test_float_input():
    # Edge: input is float
    codeflash_output = is_true(1.0) # 684ns -> 668ns (2.40% faster)
    codeflash_output = is_true(0.0) # 219ns -> 229ns (4.37% slower)

def test_none_input():
    # Edge: input is None
    codeflash_output = is_true(None) # 533ns -> 607ns (12.2% slower)

def test_list_input():
    # Edge: input is a list
    codeflash_output = is_true(['true']) # 510ns -> 473ns (7.82% faster)

def test_dict_input():
    # Edge: input is a dict
    codeflash_output = is_true({'true': True}) # 718ns -> 579ns (24.0% faster)

def test_bytes_input():
    # Edge: input is bytes
    codeflash_output = is_true(b'true') # 503ns -> 488ns (3.07% faster)

def test_str_true_with_newline():
    # Edge: input is 'true\n'
    codeflash_output = is_true('true\n') # 526ns -> 508ns (3.54% faster)

def test_str_true_with_tab():
    # Edge: input is 'true\t'
    codeflash_output = is_true('true\t') # 599ns -> 583ns (2.74% faster)

def test_str_true_with_nonbreaking_space():
    # Edge: input is 'true' plus non-breaking space
    codeflash_output = is_true('true\u00A0') # 568ns -> 503ns (12.9% faster)

def test_str_true_with_zero_width_space():
    # Edge: input is 'true' plus zero-width space
    codeflash_output = is_true('true\u200B') # 548ns -> 493ns (11.2% faster)

def test_str_true_with_combining_char():
    # Edge: input is 'tru' + 'e' + combining acute accent
    codeflash_output = is_true('true\u0301') # 524ns -> 532ns (1.50% slower)

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

def test_large_list_of_trues():
    # Large scale: a list of 1000 'true' strings
    results = [is_true('true') for _ in range(1000)] # 903ns -> 654ns (38.1% faster)

def test_large_list_of_mixed():
    # Large scale: 500 'true', 500 'false'
    results = [is_true('true') for _ in range(500)] + [is_true('false') for _ in range(500)] # 844ns -> 622ns (35.7% faster)

def test_large_list_of_booleans():
    # Large scale: 500 True, 500 False
    results = [is_true(True) for _ in range(500)] + [is_true(False) for _ in range(500)] # 419ns -> 399ns (5.01% faster)




def test_large_list_of_true_subclasses():
    # Large scale: 1000 instances of str subclass with 'true'
    class MyStr(str): pass
    results = [is_true(MyStr('true')) for _ in range(1000)] # 1.37μs -> 771ns (77.4% faster)

def test_large_list_of_false_subclasses():
    # Large scale: 1000 instances of str subclass with 'false'
    class MyStr(str): pass
    results = [is_true(MyStr('false')) for _ in range(1000)] # 701ns -> 610ns (14.9% 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 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_true_bool():
    # Basic boolean True
    codeflash_output = is_true(True) # 448ns -> 407ns (10.1% faster)

def test_false_bool():
    # Basic boolean False
    codeflash_output = is_true(False) # 440ns -> 410ns (7.32% faster)

def test_true_string_lowercase():
    # Basic string "true" lowercase
    codeflash_output = is_true("true") # 869ns -> 658ns (32.1% faster)

def test_true_string_uppercase():
    # Basic string "TRUE" uppercase
    codeflash_output = is_true("TRUE") # 814ns -> 903ns (9.86% slower)

def test_true_string_mixedcase():
    # Basic string "TrUe" mixed case
    codeflash_output = is_true("TrUe") # 763ns -> 849ns (10.1% slower)

def test_false_string():
    # Basic string "false"
    codeflash_output = is_true("false") # 585ns -> 534ns (9.55% faster)

def test_random_string():
    # Basic random string
    codeflash_output = is_true("hello") # 621ns -> 584ns (6.34% faster)

def test_empty_string():
    # Basic empty string
    codeflash_output = is_true("") # 565ns -> 542ns (4.24% faster)

# --- Edge Test Cases ---

def test_true_string_with_whitespace():
    # String " true" with leading whitespace
    codeflash_output = is_true(" true") # 602ns -> 567ns (6.17% faster)
    # String "true " with trailing whitespace
    codeflash_output = is_true("true ") # 281ns -> 245ns (14.7% faster)
    # String "t rue" with internal whitespace
    codeflash_output = is_true("t rue") # 149ns -> 138ns (7.97% faster)

def test_true_string_with_special_characters():
    # String "true!" with a special character
    codeflash_output = is_true("true!") # 533ns -> 477ns (11.7% faster)
    # String "tru3" with a digit
    codeflash_output = is_true("tru3") # 748ns -> 784ns (4.59% slower)

def test_true_string_unicode_equivalent():
    # String "true" with fullwidth 'e' (not ASCII 'e')
    codeflash_output = is_true("true") # 1.18μs -> 1.17μs (0.170% faster)

def test_true_string_casefold_edge():
    # String "TrUẞ" where 'ẞ'.casefold() is 'ss', so should be False
    codeflash_output = is_true("TrUẞ") # 972ns -> 950ns (2.32% faster)

def test_true_string_length_edge():
    # String "tru" (length 3)
    codeflash_output = is_true("tru") # 545ns -> 571ns (4.55% slower)
    # String "truee" (length 5)
    codeflash_output = is_true("truee") # 282ns -> 233ns (21.0% faster)

def test_none_input():
    # None input should return False
    codeflash_output = is_true(None) # 529ns -> 690ns (23.3% slower)

def test_integer_input():
    # Integer input should return False
    codeflash_output = is_true(1) # 519ns -> 506ns (2.57% faster)
    codeflash_output = is_true(0) # 270ns -> 238ns (13.4% faster)

def test_float_input():
    # Float input should return False
    codeflash_output = is_true(1.0) # 709ns -> 654ns (8.41% faster)
    codeflash_output = is_true(0.0) # 280ns -> 263ns (6.46% faster)

def test_list_input():
    # List input should return False
    codeflash_output = is_true(["true"]) # 476ns -> 464ns (2.59% faster)

def test_bytes_input():
    # Bytes input should return False
    codeflash_output = is_true(b"true") # 434ns -> 487ns (10.9% slower)


def test_str_subclass():
    # Subclass of str should behave like str
    class MyStr(str):
        pass
    codeflash_output = is_true(MyStr("true")) # 1.44μs -> 843ns (71.3% faster)
    codeflash_output = is_true(MyStr("TRUE")) # 438ns -> 1.03μs (57.6% slower)
    codeflash_output = is_true(MyStr("false")) # 240ns -> 221ns (8.60% faster)

def test_str_with_newline():
    # String "true\n" with trailing newline
    codeflash_output = is_true("true\n") # 565ns -> 564ns (0.177% faster)

def test_str_with_tab():
    # String "\ttrue" with leading tab
    codeflash_output = is_true("\ttrue") # 598ns -> 547ns (9.32% faster)

def test_str_with_null_char():
    # String "tru\x00e" with embedded null character
    codeflash_output = is_true("tru\x00e") # 557ns -> 512ns (8.79% faster)

# --- Large Scale Test Cases ---

def test_large_list_of_true_strings():
    # List of 1000 "true" strings, all should return True
    for s in ["true"] * 1000:
        codeflash_output = is_true(s) # 172μs -> 137μs (25.3% faster)

def test_large_list_of_false_strings():
    # List of 1000 "false" strings, all should return False
    for s in ["false"] * 1000:
        codeflash_output = is_true(s) # 142μs -> 131μs (8.22% faster)

def test_large_list_of_mixed_strings():
    # List of 1000 strings, alternating "true" and "false"
    for i in range(1000):
        if i % 2 == 0:
            codeflash_output = is_true("true")
        else:
            codeflash_output = is_true("false")


def test_large_list_of_true_bools():
    # List of 1000 True bools, all should return True
    for b in [True] * 1000:
        codeflash_output = is_true(b) # 115μs -> 108μs (6.37% faster)

def test_large_list_of_false_bools():
    # List of 1000 False bools, all should return False
    for b in [False] * 1000:
        codeflash_output = is_true(b) # 115μs -> 108μs (6.15% faster)

def test_large_list_of_mixed_bools():
    # List of 1000 bools, alternating True and False
    for i in range(1000):
        if i % 2 == 0:
            codeflash_output = is_true(True)
        else:
            codeflash_output = is_true(False)

def test_large_list_of_non_str_non_bool():
    # List of 1000 None values, all should return False
    for n in [None] * 1000:
        codeflash_output = is_true(n) # 147μs -> 139μs (5.87% faster)
    # List of 1000 integers, all should return False
    for i in range(1000):
        codeflash_output = is_true(i) # 146μs -> 139μs (5.37% faster)

def test_large_list_of_str_subclasses():
    # List of 1000 str subclass instances with "true"
    class MyStr(str):
        pass
    for s in [MyStr("true")] * 1000:
        codeflash_output = is_true(s) # 205μs -> 163μs (25.8% faster)

To edit these changes git checkout codeflash/optimize-is_true-mgu91wly and push.

Codeflash

The key optimization is adding a **fast-path ASCII check** for the exact string `'true'` before falling back to the more expensive `casefold()` operation.

**What specific optimizations were applied:**
1. **Added ASCII fast-path**: Check `if val == 'true'` directly before calling `casefold()`
2. **Removed local type aliases**: Eliminated `bool_type = bool` and `str_type = str` assignments, using single `vtype = type(value)` assignment instead
3. **Early return optimization**: Return `True` immediately when exact match is found

**Why this leads to speedup:**
- **String comparison (`==`) is much faster than `casefold()`** for ASCII strings. The `casefold()` method performs Unicode normalization which has significant overhead even for simple ASCII strings
- **Most common case optimization**: The test results show that lowercase `'true'` strings get a 38% speedup, indicating this is a frequent case worth optimizing
- **Reduced variable assignments**: Eliminating the local type aliases saves two assignment operations per function call

**Test case performance patterns:**
- **Best for lowercase 'true'**: 38% faster (most common valid case)
- **Good for non-4-character strings**: 10-25% faster due to early length check
- **Slight regression for non-ASCII 'true' variants**: 4-10% slower for uppercase/mixed case since they still need `casefold()`, but now go through an extra comparison step
- **Excellent for str subclasses**: Up to 77% faster, likely due to reduced type checking overhead

The optimization prioritizes the most common valid input (`'true'`) while maintaining correctness for all edge cases.
@codeflash-ai codeflash-ai bot requested a review from mashraf-222 October 17, 2025 02:48
@codeflash-ai codeflash-ai bot added the ⚡️ codeflash Optimization PR opened by Codeflash AI label Oct 17, 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