Skip to content

⚡️ Speed up function _add_timing_instrumentation by 27% in PR #1199 (omni-java)#1296

Closed
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
codeflash/optimize-pr1199-2026-02-03T09.30.59
Closed

⚡️ Speed up function _add_timing_instrumentation by 27% in PR #1199 (omni-java)#1296
codeflash-ai[bot] wants to merge 1 commit intoomni-javafrom
codeflash/optimize-pr1199-2026-02-03T09.30.59

Conversation

@codeflash-ai
Copy link
Copy Markdown
Contributor

@codeflash-ai codeflash-ai bot commented Feb 3, 2026

⚡️ This pull request contains optimizations for PR #1199

If you approve this dependent PR, these changes will be merged into the original PR branch omni-java.

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


📄 27% (0.27x) speedup for _add_timing_instrumentation in codeflash/languages/java/instrumentation.py

⏱️ Runtime : 2.67 milliseconds 2.10 milliseconds (best of 166 runs)

📝 Explanation and details

This optimization achieves a 26% runtime improvement (2.67ms → 2.10ms) by eliminating unnecessary character-by-character scanning in the brace-counting logic, which was the primary performance bottleneck.

Key Optimizations:

  1. Fast-path brace detection: Added an upfront check (if "{" not in body_line and "}" not in body_line) before character scanning. Lines without braces (the majority) now skip the expensive character iteration entirely, reducing hot-path time from ~37% to ~11% of total execution.

  2. Precomputed constants: Caching len(lines) as n_lines and the indentation prefix body_prefix = " " * 8 eliminates redundant computations in tight loops.

  3. Short-circuit optimization: Added early exit (if brace_depth == 0: break) within the character loop to stop processing once the method's closing brace is found mid-line.

  4. Batch operations: Replaced individual append calls with result.extend(method_lines) for method signature lines, reducing function call overhead.

Performance Impact:

The line profiler shows the character-scanning loop time dropped from 4.68M + 4.52M + 4.78M = ~14M ns (37% of total) to ~1.4M ns (11% of total) - a 10x reduction in the hottest code path. This benefit scales with input size, as evidenced by test results:

  • Small files (single test): 7-11% faster
  • Medium files (multiple tests): 6-9% faster
  • Large files (200+ test methods, 500+ statements): 15-33% faster (some cases showing 312% speedup for very large method bodies)

The optimization is particularly effective for typical Java test files where most lines contain code statements rather than braces, making the fast-path check highly beneficial. The character-by-character scan now only runs on the small subset of lines that actually contain braces, while preserving exact output behavior including proper handling of nested blocks and mid-line brace detection.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 58 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
import textwrap

# imports
import pytest  # used for our unit tests
from codeflash.languages.java.instrumentation import \
    _add_timing_instrumentation

def test_basic_instrumentation_single_test():
    # Basic scenario: a simple Java class with one @Test method.
    src = textwrap.dedent(
        """\
        public class ExampleTest {
            @Test
            public void testSomething() {
                int x = 1;
            }
        }"""
    )
    # Instrument using provided class and function names.
    codeflash_output = _add_timing_instrumentation(src, "ExampleTest", "testSomething"); out = codeflash_output # 11.3μs -> 10.5μs (7.36% faster)

def test_multiple_tests_and_annotation_handling():
    # Multiple @Test methods, and an additional annotation after @Test (common in some styles).
    src = textwrap.dedent(
        """\
        public class MultiTest {
            @Test
            @DisplayName("first")
            public void firstTest() {
                // first body
            }

            @Test
            public void secondTest() {
                // second body
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "MultiTest", "ignoredFunc"); out = codeflash_output # 18.7μs -> 17.7μs (6.13% faster)

def test_method_signature_multiline_and_brace_on_new_line():
    # Method signature spanning multiple lines, with the opening brace on its own line.
    src = textwrap.dedent(
        """\
        public class MultiLineSig {
            @Test
            public void multiLine(
                int a,
                int b
            ) {
                doSomething();
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "MultiLineSig", "multiLine"); out = codeflash_output # 12.1μs -> 11.0μs (9.45% faster)

def test_nested_braces_in_body_are_handled_correctly():
    # Body contains nested blocks; instrumentation should consume the correct block until matching closing brace.
    src = textwrap.dedent(
        """\
        public class NestedBraces {
            @Test
            public void nested() {
                if (true) {
                    int a = 1;
                }
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "NestedBraces", "nested"); out = codeflash_output # 12.7μs -> 12.5μs (1.20% faster)

def test_no_test_annotations_returns_unchanged_structure():
    # If there are no @Test annotations, the source should be returned with no instrumentation injected.
    src = textwrap.dedent(
        """\
        public class NoTests {
            public void helper() {
                // nothing to do
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "NoTests", "helper"); out = codeflash_output # 3.18μs -> 3.08μs (3.25% faster)

def test_opening_brace_on_same_line_and_on_next_line_variants():
    # Test both variants: opening brace on same line and on next line.
    src = textwrap.dedent(
        """\
        public class BraceVariants {
            @Test
            public void sameLine() {
                a();
            }

            @Test
            public void nextLine()
            {
                b();
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "BraceVariants", "testFunc"); out = codeflash_output # 18.9μs -> 17.6μs (7.54% faster)

def test_large_scale_many_tests_produces_expected_counts():
    # Large scale test: create many @Test methods to test scalability (but keep under 1000).
    num_methods = 200  # well under 1000 to be safe and fast
    methods = []
    for i in range(1, num_methods + 1):
        methods.append(textwrap.dedent(
            f"""\
            @Test
            public void test{i}() {{
                // body {i}
            }}
            """
        ))
    src = "public class ManyTests {\n" + "\n".join(methods) + "\n}"
    codeflash_output = _add_timing_instrumentation(src, "ManyTests", "bulk"); out = codeflash_output # 1.04ms -> 907μs (15.0% faster)
    mid = num_methods // 2

def test_unique_variable_suffixes_and_correct_indexing():
    # Ensure unique suffixes appear per test and indexing starts at 1 and increments.
    src = textwrap.dedent(
        """\
        public class Indexing {
            @Test
            public void first() {
                x();
            }

            @Test
            public void second() {
                y();
            }

            @Test
            public void third() {
                z();
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "Indexing", "fn"); out = codeflash_output # 24.8μs -> 23.4μs (5.86% faster)

    # Check for each id suffixed variables up to 3.
    for idx in (1, 2, 3):
        pass

def test_preserves_other_non_test_annotations_and_order():
    # Ensure annotations that appear before @Test remain in place and order unchanged.
    src = textwrap.dedent(
        """\
        public class PreserveAnnotations {
            @CustomAnnotation(value = "keep")
            @Test
            @AnotherAnn
            public void annotated() {
                work();
            }
        }"""
    )
    codeflash_output = _add_timing_instrumentation(src, "PreserveAnnotations", "annotated"); out = codeflash_output # 12.0μs -> 11.4μs (4.82% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
import pytest
from codeflash.languages.java.instrumentation import \
    _add_timing_instrumentation

def test_basic_single_test_method():
    """Test that a single @Test method gets proper timing instrumentation."""
    source = """public class TestExample {
    @Test
    public void testSimple() {
        int x = 5;
        assertEquals(5, x);
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestExample", "testSimple"); result = codeflash_output # 13.3μs -> 12.0μs (11.4% faster)

def test_multiple_test_methods():
    """Test that multiple @Test methods are each instrumented independently."""
    source = """public class TestMultiple {
    @Test
    public void testFirst() {
        int x = 1;
    }
    
    @Test
    public void testSecond() {
        int y = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestMultiple", "testFirst"); result = codeflash_output # 19.5μs -> 18.4μs (6.10% faster)

def test_test_method_with_parameters():
    """Test instrumentation of @Test methods with parameters/annotations."""
    source = """public class TestParams {
    @Test(timeout = 5000)
    public void testWithTimeout() {
        assert true;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestParams", "testWithTimeout"); result = codeflash_output # 11.5μs -> 10.8μs (6.68% faster)

def test_test_method_with_multiple_annotations():
    """Test instrumentation when method has multiple annotations."""
    source = """public class TestAnnotations {
    @Test
    @Override
    public void testMultiAnnotated() {
        int z = 10;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestAnnotations", "testMultiAnnotated"); result = codeflash_output # 12.0μs -> 11.1μs (8.31% faster)

def test_preserves_non_test_methods():
    """Test that non-@Test methods are not instrumented."""
    source = """public class TestMixed {
    public void helperMethod() {
        int x = 5;
    }
    
    @Test
    public void testMethod() {
        helperMethod();
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestMixed", "testMethod"); result = codeflash_output # 13.0μs -> 12.1μs (7.28% faster)

def test_method_with_nested_braces():
    """Test that method with nested braces (if/for/while) is instrumented correctly."""
    source = """public class TestNested {
    @Test
    public void testNested() {
        for (int i = 0; i < 10; i++) {
            if (i > 5) {
                System.out.println(i);
            }
        }
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestNested", "testNested"); result = codeflash_output # 15.7μs -> 16.1μs (2.36% slower)

def test_correct_class_name_in_markers():
    """Test that class name is correctly inserted into timing markers."""
    source = """public class MyTestClass {
    @Test
    public void myTest() {
        assertTrue(true);
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "MyTestClass", "myTest"); result = codeflash_output # 11.4μs -> 10.6μs (8.07% faster)

def test_correct_function_name_in_markers():
    """Test that function name is correctly inserted into timing markers."""
    source = """public class TestClass {
    @Test
    public void mySpecialTest() {
        assertTrue(true);
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestClass", "mySpecialTest"); result = codeflash_output # 11.4μs -> 10.6μs (7.64% faster)

def test_start_marker_format():
    """Test that start marker has correct format."""
    source = """public class Test {
    @Test
    public void testMethod() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "testMethod"); result = codeflash_output # 11.2μs -> 10.4μs (7.89% faster)

def test_end_marker_format():
    """Test that end marker has correct format with duration."""
    source = """public class Test {
    @Test
    public void testMethod() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "testMethod"); result = codeflash_output # 10.7μs -> 10.4μs (2.01% faster)

def test_environment_variable_parsing():
    """Test that environment variables are correctly read."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 10.8μs -> 10.3μs (5.18% faster)

def test_timing_measurements_capture():
    """Test that start and end nanosecond measurements are captured."""
    source = """public class Test {
    @Test
    public void test() {
        boolean check = true;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.3μs -> 10.3μs (9.85% faster)

def test_indentation_preserved():
    """Test that indentation levels are maintained correctly."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 5;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.0μs -> 10.4μs (5.99% faster)
    
    # Split lines and check indentation patterns
    lines = result.split("\n")
    
    # Find the for loop line and verify proper indentation
    for_loop_found = False
    for line in lines:
        if "for (int _cf_i1" in line:
            for_loop_found = True
            break

def test_try_finally_structure():
    """Test that try/finally block is properly structured."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 10;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.0μs -> 10.5μs (4.08% faster)

def test_empty_method_body():
    """Test instrumentation of test method with empty body."""
    source = """public class Test {
    @Test
    public void emptyTest() {
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "emptyTest"); result = codeflash_output # 10.2μs -> 10.0μs (1.90% faster)

def test_method_with_single_line_body():
    """Test instrumentation of method with single statement."""
    source = """public class Test {
    @Test
    public void singleLine() { assert true; }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "singleLine"); result = codeflash_output # 9.33μs -> 9.26μs (0.756% faster)

def test_method_with_try_catch():
    """Test that method with its own try/catch is instrumented."""
    source = """public class Test {
    @Test
    public void testWithTry() {
        try {
            int x = 5;
        } catch (Exception e) {
            fail("Error");
        }
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "testWithTry"); result = codeflash_output # 15.3μs -> 14.8μs (3.05% faster)

def test_method_with_long_body():
    """Test instrumentation of method with substantial code."""
    source = """public class Test {
    @Test
    public void longTest() {
        int x = 1;
        int y = 2;
        int z = 3;
        assertEquals(x + y, z);
        assertTrue(x < y);
        assertFalse(y > z);
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "longTest"); result = codeflash_output # 15.8μs -> 11.9μs (33.2% faster)

def test_iteration_counter_increments():
    """Test that each method gets unique iteration counter."""
    source = """public class Test {
    @Test
    public void test1() {
        int x = 1;
    }
    
    @Test
    public void test2() {
        int y = 2;
    }
    
    @Test
    public void test3() {
        int z = 3;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test1"); result = codeflash_output # 25.7μs -> 23.4μs (9.84% faster)

def test_output_println_calls():
    """Test that System.out.println is called for timing markers."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 5;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.2μs -> 10.4μs (7.43% faster)
    
    # Should have two println calls: start and end markers
    println_count = result.count("System.out.println(")

def test_console_output_contains_markers():
    """Test that console output contains proper timing marker format."""
    source = """public class Test {
    @Test
    public void test() {
        assert true;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.0μs -> 10.4μs (5.77% faster)

def test_large_number_of_test_methods():
    """Test instrumentation with many test methods to ensure scalability."""
    # Create a class with 100 test methods
    source = "public class TestLarge {\n"
    for i in range(100):
        source += f"""    @Test
    public void test{i}() {{
        int x = {i};
    }}

"""
    source += "}"
    
    codeflash_output = _add_timing_instrumentation(source, "TestLarge", "test0"); result = codeflash_output # 508μs -> 437μs (16.3% faster)
    
    # Verify all 100 methods are instrumented
    for i in range(1, 101):
        pass

def test_large_method_body_with_many_statements():
    """Test instrumentation of method with many statements."""
    # Create a method with many statements
    statements = "\n        ".join([f"int x{i} = {i};" for i in range(500)])
    source = f"""public class Test {{
    @Test
    public void largeTest() {{
        {statements}
    }}
}}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "largeTest"); result = codeflash_output # 411μs -> 100.0μs (312% faster)

def test_method_with_deeply_nested_blocks():
    """Test instrumentation of method with deep nesting."""
    source = """public class Test {
    @Test
    public void deeplyNested() {
        if (true) {
            for (int i = 0; i < 5; i++) {
                while (i > 0) {
                    try {
                        int x = 1;
                    } catch (Exception e) {
                        System.out.println("error");
                    }
                }
            }
        }
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "deeplyNested"); result = codeflash_output # 22.5μs -> 23.0μs (2.17% slower)

def test_method_body_indentation_correctness():
    """Test that method body is indented correctly within try block."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
        int y = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 12.3μs -> 11.1μs (10.5% faster)
    lines = result.split("\n")
    
    # Find method body lines and verify they have increased indentation
    found_body = False
    for i, line in enumerate(lines):
        if "int x = 1;" in line:
            found_body = True
            # Should be indented more than original due to try/for nesting
            # Original was 8 spaces (2 levels), now should be 16+ spaces (4+ levels)
            leading_spaces = len(line) - len(line.lstrip())
            break

def test_timing_variables_unique_per_method():
    """Test that timing variables use unique names for each method."""
    source = """public class Test {
    @Test
    public void test1() {
        int a = 1;
    }
    
    @Test
    public void test2() {
        int b = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test1"); result = codeflash_output # 18.5μs -> 17.2μs (7.68% faster)

def test_non_test_methods_between_test_methods():
    """Test that regular methods between @Test methods are not instrumented."""
    source = """public class Test {
    @Test
    public void test1() {
        int x = 1;
    }
    
    public void helper() {
        int y = 2;
    }
    
    @Test
    public void test2() {
        int z = 3;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test1"); result = codeflash_output # 19.5μs -> 18.3μs (6.35% faster)

def test_string_concatenation_for_markers():
    """Test that marker strings are properly concatenated."""
    source = """public class Test {
    @Test
    public void test() {
        assertTrue(true);
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.4μs -> 10.5μs (8.80% faster)

def test_method_with_throws_declaration():
    """Test instrumentation of method that throws exceptions."""
    source = """public class Test {
    @Test
    public void testThrows() throws Exception {
        throw new Exception("test");
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "testThrows"); result = codeflash_output # 11.7μs -> 10.3μs (13.3% faster)

def test_source_code_lines_preserved():
    """Test that all original source code lines are preserved."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
        int y = 2;
        int z = x + y;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 13.0μs -> 11.0μs (18.3% faster)

def test_closing_brace_handling():
    """Test that method closing brace is properly handled."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
    
    public void otherMethod() {
        int y = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 12.5μs -> 11.8μs (5.70% faster)
    
    # Verify both methods still have closing braces
    # Count braces - should be balanced
    open_braces = result.count("{")
    close_braces = result.count("}")

def test_output_string_format_with_class_and_function():
    """Test that output string includes correct class and function names."""
    source = """public class MyClass {
    @Test
    public void myFunction() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "MyClass", "myFunction"); result = codeflash_output # 11.1μs -> 10.5μs (5.24% faster)

def test_integer_parsing_for_loop_index():
    """Test that CODEFLASH_LOOP_INDEX is parsed as integer."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.1μs -> 10.3μs (8.00% faster)

def test_inner_iterations_default_value():
    """Test that CODEFLASH_INNER_ITERATIONS defaults to 100."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.0μs -> 10.2μs (7.64% faster)

def test_loop_variable_initialization():
    """Test that for loop variable is properly initialized."""
    source = """public class Test {
    @Test
    public void test() {
        assert true;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 10.8μs -> 10.2μs (5.51% faster)

def test_multiple_markers_per_iteration():
    """Test that each iteration produces both start and end markers."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.2μs -> 10.4μs (8.10% faster)
    
    # Count println calls - should be at least 2 per method (start and end markers)
    # Each iteration prints start marker, then end marker in finally
    println_calls = result.count("System.out.println(")

def test_code_after_test_methods():
    """Test that code after test methods is preserved."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
    
    @Override
    public String toString() {
        return "Test";
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 13.0μs -> 12.1μs (7.48% faster)

def test_special_characters_in_class_name():
    """Test handling of special characters in class/function names."""
    source = """public class Test_Case {
    @Test
    public void test_method_name() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test_Case", "test_method_name"); result = codeflash_output # 11.4μs -> 10.7μs (6.67% faster)

def test_class_name_parameter_used_correctly():
    """Test that class_name parameter is used in instrumentation."""
    source = """public class TestA {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "TestB", "test"); result = codeflash_output # 11.0μs -> 10.2μs (7.44% faster)

def test_func_name_parameter_used_correctly():
    """Test that func_name parameter is used in instrumentation."""
    source = """public class Test {
    @Test
    public void originalName() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "providedName"); result = codeflash_output # 11.1μs -> 10.4μs (6.05% faster)

def test_duration_calculation_in_finally():
    """Test that duration is calculated in finally block."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 10.8μs -> 10.3μs (4.66% faster)

def test_return_type_is_string():
    """Test that function returns a string."""
    source = """public class Test {
    @Test
    public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 11.0μs -> 10.3μs (6.60% faster)

def test_newlines_preserved_between_methods():
    """Test that newlines between methods are preserved."""
    source = """public class Test {
    @Test
    public void test1() {
        int x = 1;
    }
    
    
    @Test
    public void test2() {
        int y = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test1"); result = codeflash_output # 18.9μs -> 17.4μs (8.59% faster)

def test_method_signature_on_single_line():
    """Test handling when method signature and opening brace are on same line."""
    source = """public class Test {
    @Test public void test() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 8.21μs -> 7.68μs (6.79% faster)

def test_method_signature_split_across_lines():
    """Test handling when method signature spans multiple lines."""
    source = """public class Test {
    @Test
    public void test(
        int param1,
        int param2) {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 12.4μs -> 11.7μs (5.93% faster)

def test_empty_source():
    """Test handling of empty source code."""
    source = ""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 1.79μs -> 1.75μs (2.28% faster)

def test_source_without_test_methods():
    """Test handling of source code without any @Test methods."""
    source = """public class Test {
    public void regularMethod() {
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 3.47μs -> 3.42μs (1.46% faster)

def test_comment_lines_preserved():
    """Test that comment lines are preserved during instrumentation."""
    source = """public class Test {
    @Test
    public void test() {
        // This is a comment
        int x = 1;
        /* Multi-line
           comment */
        int y = 2;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 15.6μs -> 12.2μs (27.9% faster)

def test_string_literals_with_braces():
    """Test that string literals containing braces don't affect brace counting."""
    source = """public class Test {
    @Test
    public void test() {
        String s = "{test}";
        int x = 1;
    }
}"""
    
    codeflash_output = _add_timing_instrumentation(source, "Test", "test"); result = codeflash_output # 12.7μs -> 12.5μs (1.36% 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-pr1199-2026-02-03T09.30.59 and push.

Codeflash Static Badge

This optimization achieves a **26% runtime improvement** (2.67ms → 2.10ms) by eliminating unnecessary character-by-character scanning in the brace-counting logic, which was the primary performance bottleneck.

**Key Optimizations:**

1. **Fast-path brace detection**: Added an upfront check (`if "{" not in body_line and "}" not in body_line`) before character scanning. Lines without braces (the majority) now skip the expensive character iteration entirely, reducing hot-path time from ~37% to ~11% of total execution.

2. **Precomputed constants**: Caching `len(lines)` as `n_lines` and the indentation prefix `body_prefix = " " * 8` eliminates redundant computations in tight loops.

3. **Short-circuit optimization**: Added early exit (`if brace_depth == 0: break`) within the character loop to stop processing once the method's closing brace is found mid-line.

4. **Batch operations**: Replaced individual `append` calls with `result.extend(method_lines)` for method signature lines, reducing function call overhead.

**Performance Impact:**

The line profiler shows the character-scanning loop time dropped from **4.68M + 4.52M + 4.78M = ~14M ns** (37% of total) to **~1.4M ns** (11% of total) - a **10x reduction** in the hottest code path. This benefit scales with input size, as evidenced by test results:

- Small files (single test): 7-11% faster
- Medium files (multiple tests): 6-9% faster  
- Large files (200+ test methods, 500+ statements): **15-33% faster** (some cases showing 312% speedup for very large method bodies)

The optimization is particularly effective for typical Java test files where most lines contain code statements rather than braces, making the fast-path check highly beneficial. The character-by-character scan now only runs on the small subset of lines that actually contain braces, while preserving exact output behavior including proper handling of nested blocks and mid-line brace detection.
@codeflash-ai codeflash-ai bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Feb 3, 2026
@codeflash-ai codeflash-ai bot mentioned this pull request Feb 3, 2026
@KRRT7
Copy link
Copy Markdown
Collaborator

KRRT7 commented Feb 19, 2026

Closing stale bot PR.

@KRRT7 KRRT7 closed this Feb 19, 2026
@KRRT7 KRRT7 deleted the codeflash/optimize-pr1199-2026-02-03T09.30.59 branch February 19, 2026 13:02
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