Skip to content

⚡️ Speed up function _extract_modules_from_settings_gradle by 23% in PR #2015 (fix/gradle-maven-central-dependency) - #2017

Merged
claude[bot] merged 1 commit into
fix/gradle-maven-central-dependencyfrom
codeflash/optimize-pr2015-2026-04-07T11.40.43
Apr 7, 2026
Merged

claude[bot] merged 1 commit into
fix/gradle-maven-central-dependencyfrom
codeflash/optimize-pr2015-2026-04-07T11.40.43

Conversation

@codeflash-ai

@codeflash-ai codeflash-ai Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

⚡️ This pull request contains optimizations for PR #2015

If you approve this dependent PR, these changes will be merged into the original PR branch fix/gradle-maven-central-dependency.

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


📄 23% (0.23x) speedup for _extract_modules_from_settings_gradle in codeflash/languages/java/test_runner.py

⏱️ Runtime : 2.35 milliseconds 1.91 milliseconds (best of 52 runs)

📝 Explanation and details

The optimization pre-compiles three regex patterns at module load time (_INCLUDE_PATTERN, _LISTOF_PATTERN, _QUOTED_PATTERN) instead of recompiling them on every function call, eliminating the ~1 ms pattern-compilation overhead that line profiler shows dominated the original version (44.3% of total time in the first re.findall alone). The second major change replaces the O(n) if stripped not in modules list scan with a set-based if stripped not in seen check, which cuts the deduplication cost from ~288 ns to ~72 ns per check when the fallback listOf branch executes. Runtime improves from 2.35 ms to 1.91 ms (23% faster) with no behavioral regressions.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 51 Passed
🌀 Generated Regression Tests 65 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 🔘 None Found
📊 Tests Coverage 100.0%
⚙️ Click to see Existing Unit Tests
Test File::Test Function Original ⏱️ Optimized ⏱️ Speedup
test_languages/test_java/test_java_test_paths.py::TestExtractModulesFromSettingsGradle.test_leading_colon_stripped 7.79μs 5.47μs 42.3%✅
test_languages/test_java/test_java_test_paths.py::TestExtractModulesFromSettingsGradle.test_nested_gradle_modules 8.39μs 6.06μs 38.4%✅
test_languages/test_java/test_java_test_paths.py::TestExtractModulesFromSettingsGradle.test_simple_top_level_modules 9.35μs 6.92μs 35.0%✅
🌀 Click to see Generated Regression Tests
from codeflash.languages.java.test_runner import _extract_modules_from_settings_gradle


def test_basic_groovy_include_two_modules_and_colon_stripping():
    # A simple Groovy-style include with single quotes and a leading colon on one module.
    content = "include 'module-a', ':module-b'"
    # Expect both modules extracted and the leading colon removed from module-b.
    assert _extract_modules_from_settings_gradle(content) == ["module-a", "module-b"]  # 8.62μs -> 5.91μs (45.8% faster)


def test_basic_kotlin_include_parentheses_and_double_quotes():
    # Kotlin-style include with parentheses and double quotes, plus varied whitespace.
    content = '   include( "alpha",   "beta" )  '
    # Expect modules in the same order as in the include call.
    assert _extract_modules_from_settings_gradle(content) == ["alpha", "beta"]  # 8.71μs -> 6.24μs (39.5% faster)


def test_ignore_similar_words_and_only_match_include_keyword():
    # Ensure words like 'includedProjects' do not trigger a match.
    content = """
    // This should not be matched:
    includedProjects = ["x", "y"]
    # But this should:
    include("real-one")
    """
    # Only "real-one" should be returned; 'x' and 'y' come from 'includedProjects' which must be ignored.
    assert _extract_modules_from_settings_gradle(content) == ["real-one"]  # 12.7μs -> 9.94μs (27.8% faster)


def test_empty_input_returns_empty_list():
    # Empty settings file should yield no modules.
    assert _extract_modules_from_settings_gradle("") == []  # 3.43μs -> 1.67μs (105% faster)


def test_listOf_multiline_dynamic_kotlin_include_extraction():
    # Simulate a Kotlin settings.gradle.kts where modules are defined via listOf spanning multiple lines
    # and the include uses a dynamic toTypedArray() call that contains no inline string literals.
    content = """
    val allProjects = listOf(
        "mod-one",
        "mod-two",
        ":mod-three" // leading colon should be stripped
    )
    // dynamic include that has no inline quoted module names:
    include(*(allProjects + otherProjects).toTypedArray())
    """
    # Expect that listOf scanning picks up the three modules and strips the leading colon.
    assert _extract_modules_from_settings_gradle(content) == [
        "mod-one",
        "mod-two",
        "mod-three",
    ]  # 18.5μs -> 15.7μs (17.6% faster)


def test_deduplication_and_triggering_of_listOf_when_includes_have_paths_or_dots():
    # When initial include() yields modules that contain '/' or '.', the function will also
    # scan listOf(...) entries and add any new modules (avoiding duplicates).
    content = """
    // include that contains path-like and dotted names; these should trigger the secondary pass
    include("com.example:lib", ":android/app", "some/thing")
    // a listOf that contains some new modules and a duplicate ("com.example:lib" after stripping will differ,
    // but we include intentionally overlapping names to test deduplication by stripped name)
    val extras = listOf(
        "lib-extra",
        "android/app",   // note: contains slash, but we treat value literally when found in listOf
        ":com.example:lib", // leading colon present; lstrip will remove only the first colon
        "some/thing"    // duplicate by exact text (after stripping) should not be re-added
    )
    // dynamic include using the list variable
    include(*(extras).toTypedArray())
    """
    # The function strips only leading single ':' characters from literal names.
    # Expected modules sequence:
    # - From include(...): "com.example:lib" -> no leading ':' so preserved as-is,
    #                     ":android/app" -> leading ':' removed -> "android/app",
    #                     "some/thing" -> preserved.
    # - From listOf(...): "lib-extra" added,
    #                     "android/app" already present so skip,
    #                     ":com.example:lib" -> stripped -> "com.example:lib" already present so skip,
    #                     "some/thing" already present so skip.
    expected = ["com.example:lib", "android/app", "some/thing", "lib-extra"]
    assert _extract_modules_from_settings_gradle(content) == expected  # 39.7μs -> 36.0μs (10.4% faster)


def test_large_scale_include_many_modules():
    test_cases = [
        (
            10,
            [
                "simple-1",
                "simple-2",
                "simple-3",
                "simple-4",
                "simple-5",
                "simple-6",
                "simple-7",
                "simple-8",
                "simple-9",
                "simple-10",
            ],
        ),
        (50, ["pkg_" + str(i) for i in range(50)]),
        (100, ["mod-x-" + str(i).zfill(3) for i in range(100)]),
        (200, ["app_" + str(i).zfill(3) for i in range(200)]),
    ]

    for count, expected_modules in test_cases:
        modules_str = ", ".join(f'"{m}"' for m in expected_modules)
        content = "include(" + modules_str + ")"
        result = _extract_modules_from_settings_gradle(content)  # 137μs -> 130μs (5.69% faster)
        assert len(result) == count, f"Expected {count} modules, got {len(result)}"
        assert result == expected_modules, f"Modules mismatch for count {count}"
        if count >= 50:
            mid = count // 2
            assert result[mid] == expected_modules[mid], f"Module at midpoint mismatch for count {count}"


def test_large_scale_listOf_many_modules_extracted_from_dynamic_include():
    test_cases = [
        (10, "variant_a", "list_"),
        (50, "variant_b", "service_"),
        (100, "variant_c", "component_"),
        (200, "variant_d", "resource_"),
    ]

    for count, variant, prefix in test_cases:
        expected_modules = [f"{prefix}{i}" for i in range(count)]
        list_body = ",\n".join(f'"{m}"' for m in expected_modules)
        content = f"""
    // Variant: {variant}
    val all = listOf(
    {list_body}
    )
    include(*(all).toTypedArray())
    """
        result = _extract_modules_from_settings_gradle(content)  # 496μs -> 334μs (48.6% faster)
        assert len(result) == count, f"Expected {count} modules for {variant}, got {len(result)}"
        assert result == expected_modules, f"Modules mismatch for variant {variant}"
        if count >= 50:
            quarter = count // 4
            assert result[quarter] == expected_modules[quarter], f"Module at quarter point mismatch for {variant}"
# imports
from codeflash.languages.java.test_runner import _extract_modules_from_settings_gradle


def test_basic_kotlin_dsl_with_double_quotes():
    """Test extraction of modules using Kotlin DSL with double quotes."""
    content = 'include("module-a", "module-b")'
    result = _extract_modules_from_settings_gradle(content)  # 8.40μs -> 5.98μs (40.4% faster)
    assert result == ["module-a", "module-b"]


def test_basic_groovy_dsl_with_single_quotes():
    """Test extraction of modules using Groovy DSL with single quotes."""
    content = "include 'module-a', 'module-b'"
    result = _extract_modules_from_settings_gradle(content)  # 7.99μs -> 5.56μs (43.6% faster)
    assert result == ["module-a", "module-b"]


def test_single_module_kotlin_dsl():
    """Test extraction of a single module using Kotlin DSL."""
    content = 'include("app")'
    result = _extract_modules_from_settings_gradle(content)  # 6.99μs -> 4.76μs (46.9% faster)
    assert result == ["app"]


def test_single_module_groovy_dsl():
    """Test extraction of a single module using Groovy DSL."""
    content = "include 'app'"
    result = _extract_modules_from_settings_gradle(content)  # 6.72μs -> 4.64μs (44.9% faster)
    assert result == ["app"]


def test_modules_with_colon_prefix():
    """Test that colon prefix is stripped from module names."""
    content = 'include(":module-a", ":module-b")'
    result = _extract_modules_from_settings_gradle(content)  # 7.75μs -> 5.53μs (40.0% faster)
    assert result == ["module-a", "module-b"]


def test_mixed_colon_and_non_colon_modules():
    """Test extraction when some modules have colon prefix and some don't."""
    content = 'include(":app", "lib", ":features:ui")'
    result = _extract_modules_from_settings_gradle(content)  # 8.09μs -> 5.78μs (39.9% faster)
    assert result == ["app", "lib", "features:ui"]


def test_listof_kotlin_dsl():
    """Test extraction using listOf pattern in Kotlin DSL."""
    content = 'val modules = listOf("module-a", "module-b")'
    result = _extract_modules_from_settings_gradle(content)  # 8.97μs -> 6.89μs (30.1% faster)
    assert result == ["module-a", "module-b"]


def test_multiple_includes_in_single_file():
    """Test extraction when multiple include directives exist."""
    content = """
    include("app")
    include("lib1", "lib2")
    include 'lib3'
    """
    result = _extract_modules_from_settings_gradle(content)  # 11.1μs -> 8.31μs (33.0% faster)
    # The function should extract all modules from all include directives
    assert "app" in result
    assert "lib1" in result
    assert "lib2" in result
    assert "lib3" in result


def test_listof_multiline():
    """Test extraction using listOf spanning multiple lines."""
    content = """
    val modules = listOf(
        "module-a",
        "module-b",
        "module-c"
    )
    """
    result = _extract_modules_from_settings_gradle(content)  # 11.3μs -> 9.25μs (21.8% faster)
    assert "module-a" in result
    assert "module-b" in result
    assert "module-c" in result


def test_module_names_with_underscores():
    """Test extraction of module names containing underscores."""
    content = 'include("my_module", "test_lib")'
    result = _extract_modules_from_settings_gradle(content)  # 7.34μs -> 5.45μs (34.7% faster)
    assert result == ["my_module", "test_lib"]


def test_module_names_with_hyphens():
    """Test extraction of module names containing hyphens."""
    content = 'include("my-module", "test-lib")'
    result = _extract_modules_from_settings_gradle(content)  # 7.21μs -> 5.06μs (42.6% faster)
    assert result == ["my-module", "test-lib"]


def test_module_names_with_colons():
    """Test extraction of module names containing colons (nested modules)."""
    content = 'include("features:ui", "features:data")'
    result = _extract_modules_from_settings_gradle(content)  # 7.37μs -> 5.33μs (38.3% faster)
    assert result == ["features:ui", "features:data"]


def test_empty_string():
    """Test extraction from empty string should return empty list."""
    content = ""
    result = _extract_modules_from_settings_gradle(content)  # 3.27μs -> 1.51μs (116% faster)
    assert result == []


def test_no_include_directives():
    """Test extraction when no include directives are present."""
    content = "// Just some comments\nval x = 5"
    result = _extract_modules_from_settings_gradle(content)  # 5.26μs -> 3.63μs (45.1% faster)
    assert result == []


def test_include_with_no_modules():
    """Test extraction from include directive with significant structure but no quoted strings."""
    content = "include(some_variable)"
    result = _extract_modules_from_settings_gradle(content)  # 5.61μs -> 3.28μs (71.3% faster)
    assert result == []


def test_empty_include_directive_with_spaces():
    """Test extraction from include with non-trivial nested content but no quoted strings."""
    content = "include( someExpression , anotherVar )"
    result = _extract_modules_from_settings_gradle(content)  # 5.91μs -> 3.47μs (70.5% faster)
    assert result == []


def test_module_name_with_numbers():
    """Test extraction of module names containing numbers."""
    content = 'include("module1", "lib2", "app3")'
    result = _extract_modules_from_settings_gradle(content)  # 7.96μs -> 5.83μs (36.4% faster)
    assert result == ["module1", "lib2", "app3"]


def test_module_name_single_character():
    """Test extraction of single-character module names."""
    content = 'include("a", "b", "c")'
    result = _extract_modules_from_settings_gradle(content)  # 7.60μs -> 5.15μs (47.7% faster)
    assert result == ["a", "b", "c"]


def test_very_long_module_name():
    """Test extraction of very long module names."""
    long_name = "a" * 200
    content = f'include("{long_name}")'
    result = _extract_modules_from_settings_gradle(content)  # 9.92μs -> 8.05μs (23.1% faster)
    assert result == [long_name]


def test_include_with_trailing_whitespace():
    """Test extraction when include directive has trailing whitespace."""
    content = 'include("app")   \n'
    result = _extract_modules_from_settings_gradle(content)  # 6.69μs -> 4.72μs (41.8% faster)
    assert result == ["app"]


def test_include_with_comments_after():
    """Test extraction when include is followed by comment."""
    content = 'include("app") // This includes the app module'
    result = _extract_modules_from_settings_gradle(content)  # 9.10μs -> 6.55μs (38.8% faster)
    assert result == ["app"]


def test_include_with_leading_whitespace():
    """Test extraction when include has leading whitespace."""
    content = '    include("app")'
    result = _extract_modules_from_settings_gradle(content)  # 7.16μs -> 5.05μs (41.9% faster)
    assert result == ["app"]


def test_mixed_single_and_double_quotes_in_same_include():
    """Test extraction when include uses mixed quote styles."""
    content = "include(\"module-a\", 'module-b')"
    result = _extract_modules_from_settings_gradle(content)  # 7.63μs -> 5.28μs (44.6% faster)
    assert result == ["module-a", "module-b"]


def test_include_keyword_in_variable_name_not_matched():
    """Test that words like 'includedProjects' are not matched as include directives."""
    content = 'val includedProjects = listOf("app")\ninclude("lib")'
    result = _extract_modules_from_settings_gradle(content)  # 9.19μs -> 6.57μs (39.8% faster)
    assert "app" not in result
    assert "lib" in result


def test_listof_with_no_modules():
    """Test extraction from listOf with significant content but no quoted strings."""
    content = "val modules = listOf( someExpression , anotherVar )"
    result = _extract_modules_from_settings_gradle(content)  # 7.89μs -> 5.53μs (42.8% faster)
    assert result == []


def test_listof_with_trailing_comma():
    """Test extraction from listOf with trailing comma."""
    content = 'val modules = listOf("module-a", "module-b",)'
    result = _extract_modules_from_settings_gradle(content)  # 9.06μs -> 6.70μs (35.1% faster)
    assert "module-a" in result
    assert "module-b" in result


def test_module_names_all_caps():
    """Test extraction of uppercase module names."""
    content = 'include("APP", "LIB", "UTILS")'
    result = _extract_modules_from_settings_gradle(content)  # 7.69μs -> 5.52μs (39.4% faster)
    assert result == ["APP", "LIB", "UTILS"]


def test_module_names_mixed_case():
    """Test extraction of mixed-case module names."""
    content = 'include("MyApp", "LibUtils", "DataModels")'
    result = _extract_modules_from_settings_gradle(content)  # 7.73μs -> 5.54μs (39.6% faster)
    assert result == ["MyApp", "LibUtils", "DataModels"]


def test_multiple_colons_prefix():
    """Test that multiple leading colons are stripped."""
    content = 'include("::app")'
    result = _extract_modules_from_settings_gradle(content)  # 6.79μs -> 4.59μs (48.0% faster)
    # lstrip(":") removes all leading colons
    assert result == ["app"]


def test_colon_in_middle_preserved():
    """Test that colons in the middle of module names are preserved."""
    content = 'include(":features:ui:components")'
    result = _extract_modules_from_settings_gradle(content)  # 7.13μs -> 4.98μs (43.2% faster)
    assert result == ["features:ui:components"]


def test_whitespace_around_parentheses():
    """Test extraction with whitespace around parentheses."""
    content = 'include ( "app" , "lib" )'
    result = _extract_modules_from_settings_gradle(content)  # 7.22μs -> 5.10μs (41.6% faster)
    assert result == ["app", "lib"]


def test_newline_inside_include():
    """Test extraction when include directive spans multiple lines."""
    content = """include(
        "app",
        "lib"
    )"""
    result = _extract_modules_from_settings_gradle(content)  # 7.05μs -> 4.87μs (44.9% faster)
    assert "app" in result
    assert "lib" in result


def test_tab_characters_in_content():
    """Test extraction when content contains tab characters."""
    content = 'include(\t"app",\t"lib"\t)'
    result = _extract_modules_from_settings_gradle(content)  # 7.16μs -> 5.01μs (43.0% faster)
    assert result == ["app", "lib"]


def test_empty_string_between_quotes():
    """Test extraction when empty string appears between quotes."""
    content = 'include("app", "", "lib")'
    result = _extract_modules_from_settings_gradle(content)  # 7.58μs -> 5.40μs (40.4% faster)
    assert "" in result
    assert "app" in result
    assert "lib" in result


def test_space_in_module_name():
    """Test extraction when module name contains spaces."""
    content = 'include("app module")'
    result = _extract_modules_from_settings_gradle(content)  # 6.80μs -> 4.64μs (46.7% faster)
    assert result == ["app module"]


def test_special_characters_in_module_name():
    """Test extraction when module name contains special characters."""
    content = 'include("app-lib_2.0")'
    result = _extract_modules_from_settings_gradle(content)  # 7.27μs -> 4.94μs (47.3% faster)
    assert result == ["app-lib_2.0"]


def test_listof_with_single_module():
    """Test extraction from listOf with a single module."""
    content = 'val modules = listOf("app")'
    result = _extract_modules_from_settings_gradle(content)  # 7.68μs -> 5.49μs (40.0% faster)
    assert result == ["app"]


def test_nested_quotes_escaped():
    """Test extraction when quotes might be escaped in content."""
    content = 'include("module-a")\ninclude("module-b")'
    result = _extract_modules_from_settings_gradle(content)  # 9.15μs -> 6.41μs (42.7% faster)
    assert "module-a" in result
    assert "module-b" in result
    assert len(result) == 2


def test_duplicate_modules_in_listof():
    """Test extraction when listOf contains duplicate module names."""
    content = 'val modules = listOf("app", "app", "lib")'
    result = _extract_modules_from_settings_gradle(content)  # 9.02μs -> 6.92μs (30.3% faster)
    # The function should avoid adding duplicates if they already exist
    assert result.count("app") == 1  # Only one "app" should be added
    assert "lib" in result


def test_include_followed_by_listof():
    """Test extraction when both include and listOf are present in content."""
    content = """
    include("app")
    val modules = listOf("lib1", "lib2")
    """
    result = _extract_modules_from_settings_gradle(content)  # 9.37μs -> 7.04μs (33.0% faster)
    assert "app" in result
    assert "lib1" in result
    assert "lib2" in result
    assert len(result) == 3


def test_only_listof_when_include_empty():
    """Test that listOf is processed when include directive has no quoted strings."""
    content = """
    include(someVar)
    val modules = listOf("app", "lib")
    """
    result = _extract_modules_from_settings_gradle(content)  # 10.4μs -> 7.83μs (32.3% faster)
    assert "app" in result
    assert "lib" in result
    assert "someVar" not in result


def test_many_modules_in_single_include():
    """Test extraction with a large number of modules in one include directive."""
    # Create an include directive with 100 modules
    module_list = ", ".join([f'"module-{i}"' for i in range(100)])
    content = f"include({module_list})"
    result = _extract_modules_from_settings_gradle(content)  # 42.8μs -> 40.7μs (5.12% faster)
    # Verify all modules are extracted
    assert len(result) == 100
    assert "module-0" in result
    assert "module-50" in result
    assert "module-99" in result


def test_many_include_directives():
    """Test extraction with many separate include directives."""
    # Create 50 separate include directives
    lines = [f'include("module-{i}")' for i in range(50)]
    content = "\n".join(lines)
    result = _extract_modules_from_settings_gradle(content)  # 55.1μs -> 40.1μs (37.4% faster)
    # Verify all modules are extracted
    assert len(result) == 50
    assert "module-0" in result
    assert "module-25" in result
    assert "module-49" in result


def test_large_listof_multiline():
    """Test extraction from large listOf spanning many lines."""
    # Create a listOf with 100 modules
    module_list = ",\n        ".join([f'"module-{i}"' for i in range(100)])
    content = f"""
    val modules = listOf(
        {module_list}
    )
    """
    result = _extract_modules_from_settings_gradle(content)  # 152μs -> 119μs (27.7% faster)
    # Verify significant number of modules are extracted
    assert len(result) >= 95  # Allow some margin due to edge effects
    assert "module-0" in result
    assert "module-99" in result


def test_mixed_large_includes_and_listof():
    """Test extraction with both include directives and listOf in same file."""
    include_lines = [f'include("inc-{i}")' for i in range(25)]
    content_part1 = "\n".join(include_lines)

    module_list = ", ".join([f'"list-{i}"' for i in range(75)])
    content_part2 = f"val modules = listOf({module_list})"

    content = f"{content_part1}\n{content_part2}"
    result = _extract_modules_from_settings_gradle(content)  # 57.7μs -> 48.8μs (18.2% faster)
    assert "inc-0" in result
    assert "inc-24" in result
    assert any("inc-" in m for m in result)


def test_very_long_content_with_many_modules():
    """Test extraction from very long content with many modules."""
    # Create a long file with comments and many modules
    lines = ["// Android Gradle settings file"]
    for i in range(200):
        if i % 4 == 0:
            lines.append(f"// Comment about module-{i}")
        elif i % 4 == 1:
            lines.append(f'include("module-{i}")')
        elif i % 4 == 2:
            lines.append("")  # blank line
        else:
            lines.append(f"// Module-{i} is important")

    content = "\n".join(lines)
    result = _extract_modules_from_settings_gradle(content)  # 153μs -> 137μs (11.7% faster)
    # Verify extraction from long content
    assert len(result) > 40  # Should find multiple modules
    assert "module-1" in result
    assert "module-5" in result


def test_deeply_nested_module_names():
    """Test extraction of module names with deep nesting (many colons)."""
    # Create modules with deep nesting: a:b:c:d:e:f...
    nested_modules = [":".join([f"level{j}" for j in range(10)]) for i in range(50)]
    module_list = ", ".join([f'"{m}"' for m in nested_modules])
    content = f"include({module_list})"
    result = _extract_modules_from_settings_gradle(content)  # 77.3μs -> 74.6μs (3.52% faster)
    # Verify all deeply nested modules are extracted
    assert len(result) == 50
    # Check a deeply nested module
    assert "level0:level1:level2:level3:level4:level5:level6:level7:level8:level9" in result


def test_large_content_with_mixed_quotes():
    """Test extraction from large content with mixed quote styles."""
    lines = []
    for i in range(100):
        if i % 2 == 0:
            lines.append(f'include("module-{i}")')
        else:
            lines.append(f"include 'module-{i}'")

    content = "\n".join(lines)
    result = _extract_modules_from_settings_gradle(content)  # 102μs -> 73.7μs (38.9% faster)
    # All modules should be extracted regardless of quote style
    assert len(result) == 100
    assert "module-0" in result
    assert "module-1" in result
    assert "module-99" in result


def test_many_modules_with_colons():
    """Test extraction of many modules with colon prefixes."""
    # Create 150 modules with colon prefixes
    module_list = ", ".join([f'":module-{i}"' for i in range(150)])
    content = f"include({module_list})"
    result = _extract_modules_from_settings_gradle(content)  # 68.6μs -> 66.5μs (3.12% faster)
    # Verify all modules are extracted and colons are stripped
    assert len(result) == 150
    assert "module-0" in result
    assert "module-149" in result
    assert ":module-0" not in result  # Colons should be stripped


def test_repeated_extractions_same_content():
    """Test that repeated extractions of same content process both include and listOf."""
    content = """
    include("app", "lib1", "lib2")
    val modules = listOf("mod1", "mod2")
    """
    result = _extract_modules_from_settings_gradle(content)  # 10.1μs -> 8.01μs (26.5% faster)
    assert "app" in result
    assert "lib1" in result
    assert "lib2" in result
    assert "mod1" in result
    assert "mod2" in result


def test_scalability_with_extreme_line_count():
    """Test extraction from content with extreme number of lines."""
    # Create content with 1000 lines
    lines = ["// Comment"] * 500  # 500 comment lines
    lines.extend([f'include("module-{i}")' for i in range(100)])
    lines.extend(["// Another comment"] * 400)  # 400 more comment lines

    content = "\n".join(lines)
    result = _extract_modules_from_settings_gradle(content)  # 558μs -> 514μs (8.66% faster)
    # Should find all 100 modules despite many lines
    assert len(result) == 100
    assert "module-0" in result
    assert "module-99" in result

To edit these changes git checkout codeflash/optimize-pr2015-2026-04-07T11.40.43 and push.

Codeflash Static Badge

The optimization pre-compiles three regex patterns at module load time (`_INCLUDE_PATTERN`, `_LISTOF_PATTERN`, `_QUOTED_PATTERN`) instead of recompiling them on every function call, eliminating the ~1 ms pattern-compilation overhead that line profiler shows dominated the original version (44.3% of total time in the first `re.findall` alone). The second major change replaces the O(n) `if stripped not in modules` list scan with a set-based `if stripped not in seen` check, which cuts the deduplication cost from ~288 ns to ~72 ns per check when the fallback listOf branch executes. Runtime improves from 2.35 ms to 1.91 ms (23% faster) with no behavioral regressions.
@codeflash-ai codeflash-ai Bot added ⚡️ codeflash Optimization PR opened by Codeflash AI 🎯 Quality: High Optimization Quality according to Codeflash labels Apr 7, 2026
@claude

claude Bot commented Apr 7, 2026

Copy link
Copy Markdown
Contributor

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


PR Review Summary

Prek Checks

All checks passed (ruff format, ruff lint). mypy: no issues found.

Code Review

SMALL PR — 1 file changed, 18 lines.

This is a codeflash-ai[bot] optimization PR. The changes are correct and the speedup is credible:

  1. Pre-compiled regex patterns (_INCLUDE_PATTERN, _LISTOF_PATTERN, _QUOTED_PATTERN) — standard optimization; regex compilation happens once at module load time instead of on every call. No behavioral change.

  2. Set-based deduplication — replaces the O(n) if stripped not in modules list scan with a seen set, reducing deduplication cost significantly. The set is initialized with existing modules entries to preserve correct behavior.

Both changes are correct and the 23% speedup claim (2.35ms → 1.91ms) is credible: line profiler showed 44% of the original runtime was in regex compilation.

No bugs, security issues, or breaking changes found.

Duplicate Detection

No duplicates detected — the module-level constants are placed appropriately near the function they serve.

Other open optimization PRs

PR #2016 (_ensure_maven_central_repo, +26% speedup): CI failures on futurehouse-structure and unit-tests (windows-latest, 3.13) are pre-existing on the base branch — not caused by this PR. Left a comment on that PR. java-tracer-e2e still pending.


Last updated: 2026-04-07

@claude
claude Bot merged commit 2a2125b into fix/gradle-maven-central-dependency Apr 7, 2026
25 of 29 checks passed
@claude
claude Bot deleted the codeflash/optimize-pr2015-2026-04-07T11.40.43 branch April 7, 2026 14:58
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.

0 participants