Skip to content

⚡️ Speed up function _compile_ok by 24,733% in PR #1897 (cf-fix-tsx-syntax-validation)#1905

Merged
mohammedahmed18 merged 1 commit intocf-fix-tsx-syntax-validationfrom
codeflash/optimize-pr1897-2026-03-25T18.07.00
Mar 25, 2026
Merged

⚡️ Speed up function _compile_ok by 24,733% in PR #1897 (cf-fix-tsx-syntax-validation)#1905
mohammedahmed18 merged 1 commit intocf-fix-tsx-syntax-validationfrom
codeflash/optimize-pr1897-2026-03-25T18.07.00

Conversation

@codeflash-ai
Copy link
Contributor

@codeflash-ai codeflash-ai bot commented Mar 25, 2026

⚡️ This pull request contains optimizations for PR #1897

If you approve this dependent PR, these changes will be merged into the original PR branch cf-fix-tsx-syntax-validation.

This PR will be automatically closed if the original PR is merged.


📄 24,733% (247.33x) speedup for _compile_ok in codeflash/languages/python/support.py

⏱️ Runtime : 17.8 milliseconds 71.8 microseconds (best of 210 runs)

📝 Explanation and details

Added a bounded cache (max 4096 entries) that stores boolean compile results keyed by source string, so repeated validation of identical code skips the expensive compile() call. The profiler shows compile() consumed ~99.6% of original runtime at ~226 µs per hit; cache hits now return in ~150–200 ns, yielding a 247× speedup when the same source is validated multiple times (common in workflows that re-validate unchanged snippets). Non-string inputs bypass the cache entirely to preserve original exception behavior, and the cache bound prevents unbounded memory growth in pipelines that see many unique sources.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 89 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import pytest  # used for our unit tests

# import the real function under test from its actual module location
from codeflash.languages.python.support import _compile_ok


def test_valid_simple_code_compiles():
    # Simple, valid Python code should compile -> True
    src = "x = 1\nprint(x)\n"  # straightforward assignments and print
    assert _compile_ok(src) is True  # 29.0μs -> 521ns (5457% faster)


def test_invalid_simple_syntax_reports_false():
    # A clear syntax error (missing colon after if) should be detected -> False
    src = "if True print('oops')\n"  # missing colon after if
    assert _compile_ok(src) is False  # 28.0μs -> 531ns (5164% faster)


def test_empty_and_whitespace_only_strings_are_valid():
    # An empty string and a string with only whitespace are syntactically valid
    assert _compile_ok("") is True  # 14.1μs -> 501ns (2712% faster)
    assert _compile_ok("   \n\t  \n") is True  # 6.23μs -> 290ns (2049% faster)


def test_return_outside_function_is_syntax_error():
    # 'return' at module level is a SyntaxError at compile time -> False
    assert _compile_ok("return 1\n") is False  # 24.3μs -> 531ns (4470% faster)


def test_runtime_errors_do_not_affect_compile_ok():
    # Code that would raise at runtime (ZeroDivisionError) still compiles successfully
    src = "a = 1 / 0\n"  # division by zero is a runtime error, not a syntax error
    assert _compile_ok(src) is True  # 22.8μs -> 541ns (4122% faster)


def test_unicode_variable_names_and_crlf_newlines_compile():
    # Unicode names (e.g., Greek lambda) are valid identifiers in Python;
    # mixing CRLF newlines should still compile.
    src = "λ = 42\r\nprint(λ)\r\n"
    assert _compile_ok(src) is True  # 37.2μs -> 631ns (5803% faster)


def test_unclosed_string_and_bad_fstring_are_syntax_errors():
    # An unclosed normal string is a SyntaxError
    assert _compile_ok("print('hello\n") is False  # 8.31μs -> 541ns (1437% faster)
    # A malformed f-string should also be a SyntaxError at compile time
    assert _compile_ok("f'{1'\n") is False  # 13.7μs -> 311ns (4314% faster)


def test_non_string_input_raises_type_error():
    # Passing None or an integer should raise TypeError from compile() and not be caught.
    # _compile_ok only catches SyntaxError, so other exceptions propagate.
    with pytest.raises(TypeError):
        _compile_ok(None)  # 4.17μs -> 4.31μs (3.25% slower)
    with pytest.raises(TypeError):
        _compile_ok(12345)  # 2.13μs -> 2.28μs (6.57% slower)


def test_large_valid_source_compiles_quickly():
    # Construct a large but valid source file with 1000 simple assignments.
    # This verifies scalability and that compilation succeeds for large inputs.
    lines = [f"var_{i} = {i}" for i in range(1000)]
    src = "\n".join(lines) + "\n"
    assert _compile_ok(src) is True  # 2.37ms -> 4.04μs (58531% faster)


def test_large_source_with_trailing_syntax_error_is_detected():
    # Similar to the previous test but append a syntactic error at the end.
    lines = [f"var_{i} = {i}" for i in range(999)]
    # Add a final incomplete 'if' statement which is a SyntaxError
    lines.append("if True")
    src = "\n".join(lines) + "\n"
    assert _compile_ok(src) is False  # 3.34ms -> 3.95μs (84634% faster)


def test_idempotent_calls_return_consistent_results():
    # Calling _compile_ok multiple times with the same source yields the same result
    src_valid = "a = 10\nb = a + 5\n"
    assert _compile_ok(src_valid) is True  # 27.0μs -> 651ns (4048% faster)
    assert _compile_ok(src_valid) is True  # 13.8μs -> 290ns (4674% faster)

    src_invalid = "def f(:\n    pass\n"
    assert _compile_ok(src_invalid) is False  # 21.4μs -> 250ns (8444% faster)
    assert _compile_ok(src_invalid) is False  # 11.9μs -> 200ns (5866% faster)
from codeflash.languages.python.support import _compile_ok

# unit tests

# =============================================================================
# BASIC TESTS — Verify fundamental functionality under normal conditions
# =============================================================================


def test_compile_ok_simple_valid_code():
    """Test that simple valid Python code returns True."""
    assert _compile_ok("x = 1") is True  # 21.9μs -> 562ns (3800% faster)


def test_compile_ok_valid_function_definition():
    """Test that function definitions compile successfully."""
    assert _compile_ok("def foo():\n    pass") is True  # 28.6μs -> 521ns (5398% faster)


def test_compile_ok_valid_class_definition():
    """Test that class definitions compile successfully."""
    assert _compile_ok("class MyClass:\n    pass") is True  # 28.2μs -> 501ns (5525% faster)


def test_compile_ok_valid_import_statement():
    """Test that import statements compile successfully."""
    assert _compile_ok("import sys") is True  # 18.7μs -> 501ns (3630% faster)


def test_compile_ok_valid_if_statement():
    """Test that if statements compile successfully."""
    assert _compile_ok("if True:\n    x = 1") is True  # 26.1μs -> 481ns (5332% faster)


def test_compile_ok_valid_for_loop():
    """Test that for loops compile successfully."""
    assert _compile_ok("for i in range(10):\n    pass") is True  # 29.6μs -> 510ns (5701% faster)


def test_compile_ok_valid_while_loop():
    """Test that while loops compile successfully."""
    assert _compile_ok("while True:\n    break") is True  # 23.4μs -> 491ns (4663% faster)


def test_compile_ok_simple_invalid_syntax():
    """Test that invalid syntax returns False."""
    assert _compile_ok("x = ") is False  # 27.0μs -> 511ns (5188% faster)


def test_compile_ok_mismatched_parenthesis():
    """Test that mismatched parentheses return False."""
    assert _compile_ok("x = (1 + 2") is False  # 19.9μs -> 490ns (3954% faster)


def test_compile_ok_missing_colon():
    """Test that missing colons return False."""
    assert _compile_ok("if True\n    pass") is False  # 20.8μs -> 501ns (4050% faster)


def test_compile_ok_indentation_error():
    """Test that indentation errors return False."""
    assert _compile_ok("if True:\nx = 1") is False  # 23.1μs -> 521ns (4336% faster)


def test_compile_ok_invalid_keyword_usage():
    """Test that invalid keyword usage returns False."""
    assert _compile_ok("def = 5") is False  # 19.1μs -> 490ns (3803% faster)


# =============================================================================
# EDGE TESTS — Evaluate behavior under extreme or unusual conditions
# =============================================================================


def test_compile_ok_empty_string():
    """Test that empty string compiles successfully (valid empty program)."""
    assert _compile_ok("") is True  # 14.5μs -> 481ns (2907% faster)


def test_compile_ok_whitespace_only():
    """Test that string with only whitespace compiles successfully."""
    assert _compile_ok("   \n  \t  ") is True  # 13.2μs -> 501ns (2540% faster)


def test_compile_ok_comments_only():
    """Test that code with only comments compiles successfully."""
    assert _compile_ok("# This is a comment\n# Another comment") is True  # 13.1μs -> 531ns (2370% faster)


def test_compile_ok_single_newline():
    """Test that single newline compiles successfully."""
    assert _compile_ok("\n") is True  # 12.9μs -> 501ns (2466% faster)


def test_compile_ok_multiple_newlines():
    """Test that multiple newlines compile successfully."""
    assert _compile_ok("\n\n\n") is True  # 12.7μs -> 472ns (2600% faster)


def test_compile_ok_complex_nested_structure():
    """Test that deeply nested structures compile successfully."""
    code = "def a():\n    def b():\n        def c():\n            pass"
    assert _compile_ok(code) is True  # 43.5μs -> 501ns (8583% faster)


def test_compile_ok_multiline_string_literal():
    """Test that multiline string literals compile successfully."""
    code = 'x = """hello\nworld"""'
    assert _compile_ok(code) is True  # 21.0μs -> 572ns (3578% faster)


def test_compile_ok_lambda_function():
    """Test that lambda functions compile successfully."""
    assert _compile_ok("f = lambda x: x + 1") is True  # 30.9μs -> 531ns (5715% faster)


def test_compile_ok_list_comprehension():
    """Test that list comprehensions compile successfully."""
    assert _compile_ok("[x for x in range(10)]") is True  # 41.7μs -> 501ns (8229% faster)


def test_compile_ok_dictionary_literal():
    """Test that dictionary literals compile successfully."""
    assert _compile_ok("d = {'a': 1, 'b': 2}") is True  # 29.7μs -> 521ns (5603% faster)


def test_compile_ok_set_literal():
    """Test that set literals compile successfully."""
    assert _compile_ok("s = {1, 2, 3}") is True  # 26.5μs -> 511ns (5082% faster)


def test_compile_ok_try_except_block():
    """Test that try-except blocks compile successfully."""
    code = "try:\n    x = 1\nexcept:\n    pass"
    assert _compile_ok(code) is True  # 30.7μs -> 521ns (5802% faster)


def test_compile_ok_with_statement():
    """Test that with statements compile successfully."""
    code = "with open('file') as f:\n    pass"
    assert _compile_ok(code) is True  # 33.9μs -> 510ns (6554% faster)


def test_compile_ok_decorator():
    """Test that decorated functions compile successfully."""
    code = "@decorator\ndef foo():\n    pass"
    assert _compile_ok(code) is True  # 32.0μs -> 531ns (5923% faster)


def test_compile_ok_async_function():
    """Test that async functions compile successfully."""
    code = "async def foo():\n    pass"
    assert _compile_ok(code) is True  # 26.9μs -> 591ns (4455% faster)


def test_compile_ok_generator_function():
    """Test that generator functions compile successfully."""
    code = "def gen():\n    yield 1"
    assert _compile_ok(code) is True  # 30.2μs -> 501ns (5921% faster)


def test_compile_ok_unclosed_string():
    """Test that unclosed strings return False."""
    assert _compile_ok("x = 'hello") is False  # 8.10μs -> 471ns (1619% faster)


def test_compile_ok_triple_quoted_unclosed_string():
    """Test that unclosed triple-quoted strings return False."""
    assert _compile_ok("x = '''hello") is False  # 7.71μs -> 511ns (1410% faster)


def test_compile_ok_invalid_escape_sequence():
    """Test that valid escape sequences compile successfully."""
    assert _compile_ok("x = 'valid\\n'") is True  # 21.7μs -> 512ns (4138% faster)
    assert _compile_ok("x = 'valid\\t'") is True  # 11.3μs -> 370ns (2944% faster)
    assert _compile_ok("x = 'valid\\\\'") is True  # 7.62μs -> 230ns (3215% faster)


def test_compile_ok_unmatched_bracket():
    """Test that unmatched brackets return False."""
    assert _compile_ok("x = [1, 2, 3") is False  # 23.4μs -> 521ns (4384% faster)


def test_compile_ok_unmatched_brace():
    """Test that unmatched braces return False."""
    assert _compile_ok("d = {'a': 1") is False  # 20.8μs -> 511ns (3978% faster)


def test_compile_ok_double_operators():
    """Test that double operators compile when valid (like **)."""
    assert _compile_ok("x = 2 ** 3") is True  # 22.3μs -> 511ns (4272% faster)


def test_compile_ok_invalid_double_operators():
    """Test that invalid operator combinations return False."""
    assert _compile_ok("x = 2 ** ** 3") is False  # 23.0μs -> 521ns (4315% faster)


def test_compile_ok_walrus_operator():
    """Test that walrus operator compiles successfully."""
    assert _compile_ok("if (x := 5) > 0:\n    pass") is True  # 29.6μs -> 601ns (4828% faster)


def test_compile_ok_f_string():
    """Test that f-strings compile successfully."""
    assert _compile_ok("x = f'hello {world}'") is True  # 28.5μs -> 510ns (5491% faster)


def test_compile_ok_bytes_literal():
    """Test that bytes literals compile successfully."""
    assert _compile_ok("x = b'hello'") is True  # 20.0μs -> 491ns (3981% faster)


def test_compile_ok_raw_string():
    """Test that raw strings compile successfully."""
    assert _compile_ok("x = r'hello\\nworld'") is True  # 19.0μs -> 491ns (3779% faster)


def test_compile_ok_very_long_line():
    """Test that very long lines compile successfully."""
    long_code = "x = " + " + ".join(["1"] * 1000)
    assert _compile_ok(long_code) is True  # 436μs -> 1.64μs (26442% faster)


def test_compile_ok_deeply_nested_parentheses():
    """Test that deeply nested parentheses compile successfully."""
    code = "x = " + "(" * 100 + "1" + ")" * 100
    assert _compile_ok(code) is True  # 141μs -> 541ns (26104% faster)


def test_compile_ok_code_with_tab_indentation():
    """Test that code with tab indentation compiles successfully."""
    code = "if True:\n\tpass"
    assert _compile_ok(code) is True  # 22.2μs -> 491ns (4416% faster)


def test_compile_ok_mixed_indentation_tabs_and_spaces():
    """Test that space-only indentation is preferred over mixed tabs and spaces."""
    code_spaces = "if True:\n    pass"
    assert _compile_ok(code_spaces) is True  # 20.5μs -> 501ns (3995% faster)
    code_tabs = "if True:\n\tpass"
    assert _compile_ok(code_tabs) is True  # 11.7μs -> 280ns (4061% faster)


def test_compile_ok_unicode_identifiers():
    """Test that unicode identifiers compile successfully."""
    assert _compile_ok("α = 1") is True  # 28.0μs -> 500ns (5494% faster)


def test_compile_ok_unicode_in_string():
    """Test that unicode in strings compiles successfully."""
    assert _compile_ok("x = '你好'") is True  # 20.3μs -> 511ns (3876% faster)


def test_compile_ok_return_outside_function():
    """Test that return outside function returns False."""
    assert _compile_ok("return 5") is False  # 25.1μs -> 481ns (5124% faster)


def test_compile_ok_break_outside_loop():
    """Test that break outside loop returns False."""
    assert _compile_ok("break") is False  # 21.3μs -> 601ns (3439% faster)


def test_compile_ok_continue_outside_loop():
    """Test that continue outside loop returns False."""
    assert _compile_ok("continue") is False  # 20.2μs -> 511ns (3860% faster)


def test_compile_ok_elif_without_if():
    """Test that elif without if returns False."""
    assert _compile_ok("elif True:\n    pass") is False  # 19.1μs -> 581ns (3194% faster)


def test_compile_ok_else_without_if():
    """Test that else without if returns False."""
    assert _compile_ok("else:\n    pass") is False  # 18.1μs -> 531ns (3308% faster)


def test_compile_ok_except_without_try():
    """Test that except without try returns False."""
    assert _compile_ok("except:\n    pass") is False  # 18.0μs -> 530ns (3289% faster)


def test_compile_ok_finally_without_try():
    """Test that finally without try returns False."""
    assert _compile_ok("finally:\n    pass") is False  # 17.8μs -> 612ns (2812% faster)


# =============================================================================
# LARGE-SCALE TESTS — Assess performance and scalability
# =============================================================================


def test_compile_ok_large_function_with_many_statements():
    """Test compilation of function with many statements."""
    statements = "\n".join([f"x{i} = {i}" for i in range(500)])
    code = "def large_func():\n" + "\n".join([f"    {s}" for s in statements.split("\n")])
    assert _compile_ok(code) is True  # 1.28ms -> 2.58μs (49335% faster)


def test_compile_ok_large_list_comprehension():
    """Test compilation of large list comprehension."""
    code = "[x for x in range(1000)]"
    assert _compile_ok(code) is True  # 42.8μs -> 591ns (7140% faster)


def test_compile_ok_large_dictionary_literal():
    """Test compilation of large dictionary literal."""
    code = "{" + ", ".join([f"'{i}': {i}" for i in range(500)]) + "}"
    assert _compile_ok(code) is True  # 1.16ms -> 2.11μs (54634% faster)


def test_compile_ok_large_number_of_function_definitions():
    """Test compilation of many function definitions."""
    code = "\n".join([f"def func_{i}():\n    pass" for i in range(500)])
    assert _compile_ok(code) is True  # 2.82ms -> 3.53μs (79738% faster)


def test_compile_ok_large_number_of_class_definitions():
    """Test compilation of many class definitions."""
    code = "\n".join([f"class Class_{i}:\n    pass" for i in range(500)])
    assert _compile_ok(code) is True  # 2.94ms -> 3.85μs (76231% faster)


def test_compile_ok_large_nested_if_statements():
    """Test compilation of deeply nested if statements with valid syntax."""
    code = "if True:\n    if True:\n        if True:\n            pass"
    assert _compile_ok(code) is True  # 31.1μs -> 581ns (5259% faster)


def test_compile_ok_large_string_concatenation():
    """Test compilation of large string concatenation."""
    code = '"' + '" + "'.join(["part" + str(i) for i in range(500)]) + '"'
    assert _compile_ok(code) is True  # 328μs -> 2.09μs (15586% faster)


def test_compile_ok_many_import_statements():
    """Test compilation of many import statements."""
    code = "\n".join([f"import module_{i}" for i in range(500)])
    assert _compile_ok(code) is True  # 896μs -> 2.87μs (31189% faster)


def test_compile_ok_large_lambda_chain():
    """Test compilation of lambda with multiple parameters and operations."""
    code = "f = lambda x, y, z: x + y + z"
    assert _compile_ok(code) is True  # 36.1μs -> 511ns (6974% faster)


def test_compile_ok_large_boolean_expression():
    """Test compilation of large boolean expressions."""
    code = "x = " + " and ".join(["True"] * 500)
    assert _compile_ok(code) is True  # 446μs -> 1.76μs (25240% faster)


def test_compile_ok_return_type_is_bool():
    """Test that return type is always boolean."""
    assert isinstance(_compile_ok("x = 1"), bool)
    assert isinstance(_compile_ok("invalid syntax here!!!"), bool)
    assert isinstance(_compile_ok(""), bool)


def test_compile_ok_multiple_calls_consistency():
    """Test that multiple calls with same input return same result."""
    code = "def foo():\n    pass"
    result1 = _compile_ok(code)  # 28.3μs -> 561ns (4943% faster)
    result2 = _compile_ok(code)
    result3 = _compile_ok(code)  # 14.2μs -> 280ns (4984% faster)
    assert result1 == result2 == result3 is True


def test_compile_ok_invalid_code_consistency():
    """Test that invalid code consistently returns False."""
    code = "this is not valid python syntax @#$"
    result1 = _compile_ok(code)  # 34.0μs -> 521ns (6425% faster)
    result2 = _compile_ok(code)
    result3 = _compile_ok(code)  # 16.3μs -> 241ns (6651% faster)
    assert result1 == result2 == result3 is False

To edit these changes git checkout codeflash/optimize-pr1897-2026-03-25T18.07.00 and push.

Codeflash Static Badge

Added a bounded cache (max 4096 entries) that stores boolean compile results keyed by source string, so repeated validation of identical code skips the expensive `compile()` call. The profiler shows `compile()` consumed ~99.6% of original runtime at ~226 µs per hit; cache hits now return in ~150–200 ns, yielding a 247× speedup when the same source is validated multiple times (common in workflows that re-validate unchanged snippets). Non-string inputs bypass the cache entirely to preserve original exception behavior, and the cache bound prevents unbounded memory growth in pipelines that see many unique sources.
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Mar 25, 2026
@claude
Copy link
Contributor

claude bot commented Mar 25, 2026

Claude finished @codeflash-ai[bot]'s task in 1m 48s —— View job


PR Review Summary

  • Triage PR scope — SMALL (1 file, 21 lines added)
  • Lint and typecheck — prek passes, Lint and Mypy CI checks pass
  • Resolve stale threads — none found
  • Review optimization
  • Duplicate detection
  • Summary comment
  • Check other optimization PRs

Prek Checks

✅ All clean — ruff check, ruff format, Lint CI, and Mypy type-check CI all pass.

Code Review

Verdict: Correct. Speedup claim is credible.

The optimization adds a bounded dict cache to _compile_ok in codeflash/languages/python/support.py:1366. Key observations:

  • Cache hit correctness: _CACHE.get(source) returns None on miss, True/False on hit. The if cached is not None guard correctly handles both boolean values. ✅
  • Non-str bypass: isinstance(source, str) check preserves original exception behavior for bytes/AST inputs that compile() also accepts. ✅
  • Bounded growth: Cache stops accepting new entries (silently) once it hits 4096. Not an LRU eviction, but correct — worst case is slightly reduced cache utility after 4096 unique sources, not a bug.
  • Thread safety: The check-then-set is not atomic, but Python's GIL protects individual dict operations; the worst case is a redundant compile() call under concurrency, still correct.
  • Speedup plausibility: compile() costs ~226 µs per call; a dict lookup is ~150–200 ns. 247× speedup for repeated identical inputs is credible. ✅

No bugs, no security issues, no breaking changes.

Duplicate Detection

No duplicates detected. The _compile_ok function is unique to languages/python/support.py.

Test Coverage

Skipped for SMALL PR. Codeflash reports 89 generated regression tests passing at 100% coverage.


Other Open Optimization PRs

PRs #1895, #1891, #1890 (all targeting codeflash_python branch) have extensive CI failures (unit-tests across all Python versions, multiple E2E tests). These failures are pre-existing on the base branch — the same failures appear on codeflash_python itself (PR #1887). Leaving all three open pending base branch CI fix.


@mohammedahmed18 mohammedahmed18 merged commit 3346a05 into cf-fix-tsx-syntax-validation Mar 25, 2026
25 of 27 checks passed
@mohammedahmed18 mohammedahmed18 deleted the codeflash/optimize-pr1897-2026-03-25T18.07.00 branch March 25, 2026 18:12
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