Skip to content

⚡️ Speed up function is_build_output_dir by 144% in PR #1308 (js-suggester)#1310

Merged
KRRT7 merged 1 commit intojs-suggesterfrom
codeflash/optimize-pr1308-2026-02-03T12.05.29
Feb 3, 2026
Merged

⚡️ Speed up function is_build_output_dir by 144% in PR #1308 (js-suggester)#1310
KRRT7 merged 1 commit intojs-suggesterfrom
codeflash/optimize-pr1308-2026-02-03T12.05.29

Conversation

@codeflash-ai
Copy link
Contributor

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

⚡️ This pull request contains optimizations for PR #1308

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

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


📄 144% (1.44x) speedup for is_build_output_dir in codeflash/setup/detector.py

⏱️ Runtime : 503 microseconds 206 microseconds (best of 153 runs)

📝 Explanation and details

The optimized code achieves a 143% speedup (from 503μs to 206μs) through three key improvements:

Primary Optimizations

  1. Eliminated expensive string operations: The original code called path.as_posix().split("/") on every invocation, which:

    • Converts the Path object to a POSIX string representation
    • Allocates a new string
    • Splits it into a list

    The optimized version uses path.parts, which is a cached tuple property already available on Path objects, eliminating ~61% of the original runtime (line profiler shows as_posix().split() took 60.9% of execution time).

  2. Avoided set reconstruction: The original code recreated the build_dirs set on every function call. The optimized version pre-computes _BUILD_DIRS as a module-level frozenset, eliminating the 5.5% overhead from set creation.

  3. Leveraged efficient set operation: Instead of using a generator expression with any() that performs multiple in checks (33.6% of original time), the optimized code uses isdisjoint() - a built-in set method implemented in C that performs a single, optimized intersection check.

Why This Matters

The test results show consistent speedups across all scenarios:

  • Simple paths: 2-3x faster (e.g., Path("build"): 5.19μs → 2.34μs)
  • Deep nesting: 2-3x faster even with complex paths
  • Large-scale tests: 3-4x faster for paths with 100+ components (e.g., 100 regular dirs: 14.8μs → 4.26μs, 248% speedup)
  • Worst-case scenarios: The optimization shines brightest when checking long paths with no matches (21.7μs → 4.78μs, 355% speedup)

The optimization scales particularly well because isdisjoint() can short-circuit as soon as it finds a match, while path.parts access is essentially free after the first call. This makes the function highly efficient for both hot paths that check many paths repeatedly and deep directory structures common in real projects.

Correctness verification report:

Test Status
⚙️ Existing Unit Tests 🔘 None Found
🌀 Generated Regression Tests 86 Passed
⏪ Replay Tests 🔘 None Found
🔎 Concolic Coverage Tests 1 Passed
📊 Tests Coverage 100.0%
🌀 Click to see Generated Regression Tests
from pathlib import Path

# imports
import pytest  # used for our unit tests
from codeflash.setup.detector import is_build_output_dir

def test_basic_detect_simple_build_dir():
    # A straightforward path that ends with a known build directory should be True
    p = Path("project/build")
    codeflash_output = is_build_output_dir(p) # 5.33μs -> 2.30μs (131% faster)

def test_basic_no_build_dir():
    # A path with only source-like segments should be False
    p = Path("project/src/module")
    codeflash_output = is_build_output_dir(p) # 4.70μs -> 2.27μs (107% faster)

def test_basic_hidden_next_and_nuxt_dirs():
    # Hidden framework build dirs should be detected
    codeflash_output = is_build_output_dir(Path("site/.next/static")) # 5.15μs -> 2.25μs (128% faster)
    codeflash_output = is_build_output_dir(Path(".nuxt")) # 2.79μs -> 1.23μs (126% faster)

@pytest.mark.parametrize(
    "path_str, expected",
    [
        # segment contains 'build' as substring -> should NOT match (must be exact)
        ("project/mybuild", False),
        ("project/builds", False),
        ("project/outdated", False),  # 'out' is the build dir; 'outdated' is not
        ("project/output", False),    # 'output' not equal to 'out'
        # exact matches for small names should match
        ("project/out", True),
        ("dist", True),
    ],
)
def test_edge_substring_vs_exact_match(path_str, expected):
    # Ensure the function uses equality on segments, not substring matching
    codeflash_output = is_build_output_dir(Path(path_str)) # 29.2μs -> 13.7μs (113% faster)

def test_edge_leading_and_trailing_slashes():
    # Leading slash: absolute path whose final segment equals 'build'
    codeflash_output = is_build_output_dir(Path("/build")) # 5.49μs -> 2.20μs (149% faster)
    # Trailing slash: should behave the same as without trailing slash
    codeflash_output = is_build_output_dir(Path("/some/path/")) # 3.37μs -> 1.32μs (155% faster)

def test_edge_current_and_parent_paths_do_not_trigger():
    # '.' and '..' are not build output directories
    codeflash_output = is_build_output_dir(Path(".")) # 4.29μs -> 2.08μs (106% faster)
    codeflash_output = is_build_output_dir(Path("..")) # 2.46μs -> 1.23μs (100% faster)

def test_edge_multiple_build_segments():
    # If any segment equals a build dir, we should detect it (even if not last)
    codeflash_output = is_build_output_dir(Path("a/build/dist/binary")) # 5.17μs -> 2.29μs (125% faster)
    # Also detect when the build dir is not at the root of the provided path
    codeflash_output = is_build_output_dir(Path("root/subdir/out/other")) # 3.24μs -> 1.21μs (167% faster)

def test_edge_dot_prefixed_but_not_exact():
    # Dot-prefixed names that are not exact matches should not match
    codeflash_output = is_build_output_dir(Path("project/.build")) # 4.77μs -> 2.21μs (116% faster)
    codeflash_output = is_build_output_dir(Path("project/.nextbuild")) # 2.72μs -> 1.13μs (140% faster)

def test_edge_consecutive_slashes_handling():
    # Multiple consecutive slashes may produce empty parts when split;
    # the function should still find 'build' if present.
    # Creating a Path with consecutive slashes and verifying detection.
    p = Path("a//b///build")
    codeflash_output = is_build_output_dir(p) # 5.21μs -> 2.15μs (142% faster)

def test_edge_unicode_and_special_characters():
    # Non-ASCII parts should not interfere when a build dir is present
    codeflash_output = is_build_output_dir(Path("プロジェクト/build")) # 5.44μs -> 2.23μs (144% faster)
    # If non-ascii parts only, should be False
    codeflash_output = is_build_output_dir(Path("プロジェクト/ソース")) # 2.96μs -> 1.28μs (130% faster)

def test_large_scale_many_paths_alternating_build_and_src():
    # Create a moderate number of paths (500) alternating between build and non-build.
    # This checks the function's ability to handle many calls quickly and deterministically.
    n = 500  # well under 1000 as requested
    paths = []
    expected = []
    for i in range(n):
        if i % 2 == 0:
            # even: include a known build directory as a segment
            paths.append(Path(f"project_{i}/sub/build/module"))
            expected.append(True)
        else:
            # odd: similar structure but without a build directory
            paths.append(Path(f"project_{i}/sub/src/module"))
            expected.append(False)

    # Evaluate all and ensure the results match expectations
    results = [is_build_output_dir(p) for p in paths]

def test_more_large_scale_variety_under_limit():
    # Another larger-but-bounded test mixing many different segment positions,
    # ensuring no accidental global matching or position sensitivity.
    parts_pool = ["alpha", "build", "beta", ".next", "gamma", "dist", "delta", "out", "epsilon", "src"]
    # Build 300 different combinations mixing build and non-build segments
    constructed = []
    expected = []
    for i in range(300):
        # rotate through the pool to produce variety; include both build and non-build cases
        segs = [parts_pool[(i + j) % len(parts_pool)] for j in range(3)]
        constructed.append(Path("/".join(segs)))
        # If any segment is one of the build dirs set by the function, expected True
        expected.append(any(s in {"build", "dist", "out", ".next", ".nuxt"} for s in segs))

    results = [is_build_output_dir(p) for p in constructed]
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from pathlib import Path

# imports
import pytest
from codeflash.setup.detector import is_build_output_dir

def test_build_dir_at_root():
    """Test detection of 'build' directory at root level."""
    path = Path("build")
    codeflash_output = is_build_output_dir(path) # 5.19μs -> 2.34μs (121% faster)

def test_dist_dir_at_root():
    """Test detection of 'dist' directory at root level."""
    path = Path("dist")
    codeflash_output = is_build_output_dir(path) # 5.06μs -> 2.46μs (105% faster)

def test_out_dir_at_root():
    """Test detection of 'out' directory at root level."""
    path = Path("out")
    codeflash_output = is_build_output_dir(path) # 4.90μs -> 2.38μs (106% faster)

def test_next_dir_at_root():
    """Test detection of '.next' directory at root level."""
    path = Path(".next")
    codeflash_output = is_build_output_dir(path) # 4.87μs -> 2.45μs (98.4% faster)

def test_nuxt_dir_at_root():
    """Test detection of '.nuxt' directory at root level."""
    path = Path(".nuxt")
    codeflash_output = is_build_output_dir(path) # 4.82μs -> 2.38μs (102% faster)

def test_build_dir_nested_one_level():
    """Test detection of 'build' directory nested one level deep."""
    path = Path("src/build")
    codeflash_output = is_build_output_dir(path) # 5.29μs -> 2.46μs (115% faster)

def test_build_dir_nested_multiple_levels():
    """Test detection of 'build' directory nested multiple levels deep."""
    path = Path("project/src/build/output")
    codeflash_output = is_build_output_dir(path) # 5.34μs -> 2.48μs (115% faster)

def test_build_dir_in_middle():
    """Test detection of 'build' directory in the middle of a path."""
    path = Path("project/build/artifacts/file.o")
    codeflash_output = is_build_output_dir(path) # 5.39μs -> 2.31μs (133% faster)

def test_regular_src_directory():
    """Test that 'src' directory is not detected as build output."""
    path = Path("src")
    codeflash_output = is_build_output_dir(path) # 4.42μs -> 2.35μs (87.7% faster)

def test_regular_nested_directory():
    """Test that regular nested directories are not detected as build output."""
    path = Path("project/src/components/button.py")
    codeflash_output = is_build_output_dir(path) # 4.92μs -> 2.46μs (99.6% faster)

def test_directory_with_build_in_name_but_not_exact_match():
    """Test that directory names containing 'build' but not exactly matching are not detected."""
    path = Path("buildstuff")
    codeflash_output = is_build_output_dir(path) # 4.38μs -> 2.16μs (102% faster)

def test_directory_with_dist_prefix():
    """Test that directory names starting with 'dist' but not exactly 'dist' are not detected."""
    path = Path("distribution")
    codeflash_output = is_build_output_dir(path) # 4.29μs -> 2.27μs (89.3% faster)

def test_multiple_build_dirs_in_path():
    """Test detection when multiple build directories are in the path."""
    path = Path("build/dist/out")
    codeflash_output = is_build_output_dir(path) # 5.16μs -> 2.33μs (121% faster)

def test_next_dir_with_prefix_no_match():
    """Test that '.nextjs' is not matched (only '.next' should match)."""
    path = Path(".nextjs")
    codeflash_output = is_build_output_dir(path) # 4.38μs -> 2.19μs (99.5% faster)

def test_nuxt_dir_with_suffix_no_match():
    """Test that '.nuxt-build' is not matched (only '.nuxt' should match)."""
    path = Path(".nuxt-build")
    codeflash_output = is_build_output_dir(path) # 4.34μs -> 2.29μs (89.1% faster)

def test_empty_path():
    """Test behavior with an empty path."""
    path = Path("")
    codeflash_output = is_build_output_dir(path) # 4.17μs -> 2.30μs (80.9% faster)

def test_dot_slash_prefix():
    """Test detection with './' prefix in path."""
    path = Path("./build")
    codeflash_output = is_build_output_dir(path) # 4.71μs -> 2.33μs (102% faster)

def test_dot_slash_with_nested_build():
    """Test detection with './' prefix and nested build directory."""
    path = Path("./src/build/output")
    codeflash_output = is_build_output_dir(path) # 5.27μs -> 2.35μs (125% faster)

def test_double_slash_in_path():
    """Test path with double slashes."""
    path = Path("src//build//file")
    codeflash_output = is_build_output_dir(path) # 5.30μs -> 2.42μs (119% faster)

def test_trailing_slash():
    """Test path with trailing slash."""
    path = Path("project/build/")
    codeflash_output = is_build_output_dir(path) # 5.26μs -> 2.34μs (124% faster)

def test_build_dir_case_sensitive_lowercase():
    """Test that lowercase 'build' is correctly identified."""
    path = Path("project/build")
    codeflash_output = is_build_output_dir(path) # 5.19μs -> 2.33μs (123% faster)

def test_build_dir_case_sensitive_uppercase():
    """Test that uppercase 'BUILD' is not matched (case-sensitive check)."""
    path = Path("project/BUILD")
    codeflash_output = is_build_output_dir(path) # 4.75μs -> 2.40μs (98.3% faster)

def test_build_dir_case_sensitive_mixed_case():
    """Test that mixed case 'Build' is not matched (case-sensitive check)."""
    path = Path("project/Build")
    codeflash_output = is_build_output_dir(path) # 4.58μs -> 2.39μs (91.2% faster)

def test_dist_uppercase_not_matched():
    """Test that uppercase 'DIST' is not matched (case-sensitive)."""
    path = Path("DIST")
    codeflash_output = is_build_output_dir(path) # 4.52μs -> 2.32μs (94.4% faster)

def test_out_uppercase_not_matched():
    """Test that uppercase 'OUT' is not matched (case-sensitive)."""
    path = Path("OUT")
    codeflash_output = is_build_output_dir(path) # 4.37μs -> 2.23μs (96.3% faster)

def test_single_character_path():
    """Test behavior with single character path."""
    path = Path("a")
    codeflash_output = is_build_output_dir(path) # 4.30μs -> 2.33μs (84.1% faster)

def test_only_dots_path():
    """Test path with only dots (current directory reference)."""
    path = Path(".")
    codeflash_output = is_build_output_dir(path) # 4.38μs -> 2.30μs (90.0% faster)

def test_only_double_dots_path():
    """Test path with only double dots (parent directory reference)."""
    path = Path("..")
    codeflash_output = is_build_output_dir(path) # 4.24μs -> 2.34μs (80.8% faster)

def test_absolute_path_with_build():
    """Test absolute path containing build directory."""
    path = Path("/home/user/project/build/output")
    codeflash_output = is_build_output_dir(path) # 6.25μs -> 2.54μs (147% faster)

def test_absolute_path_without_build():
    """Test absolute path without build directory."""
    path = Path("/home/user/project/src")
    codeflash_output = is_build_output_dir(path) # 5.43μs -> 2.54μs (113% faster)

def test_windows_style_path_with_build():
    """Test Windows-style path (Path normalizes to POSIX)."""
    # Path.as_posix() converts Windows paths to POSIX format
    path = Path("C:\\Users\\project\\build\\file")
    codeflash_output = is_build_output_dir(path) # 4.41μs -> 2.29μs (92.1% faster)

def test_windows_style_path_without_build():
    """Test Windows-style path without build directory."""
    path = Path("C:\\Users\\project\\src\\file")
    codeflash_output = is_build_output_dir(path) # 4.21μs -> 2.27μs (85.0% faster)

def test_very_deep_nesting_with_build_at_end():
    """Test very deep path with build directory at the end."""
    path = Path("a/b/c/d/e/f/g/h/i/j/build")
    codeflash_output = is_build_output_dir(path) # 5.74μs -> 2.52μs (127% faster)

def test_very_deep_nesting_with_build_in_middle():
    """Test very deep path with build directory in the middle."""
    path = Path("a/b/c/d/build/e/f/g/h/i/j")
    codeflash_output = is_build_output_dir(path) # 5.57μs -> 2.43μs (129% faster)

def test_special_characters_in_non_build_dir():
    """Test path with special characters but no build directories."""
    path = Path("project-name/src_files/component-123")
    codeflash_output = is_build_output_dir(path) # 4.96μs -> 2.39μs (107% faster)

def test_hyphenated_directory_similar_to_build_dirs():
    """Test hyphenated directories that resemble build dirs but don't match."""
    path = Path("my-build-output/project")
    codeflash_output = is_build_output_dir(path) # 4.66μs -> 2.38μs (95.4% faster)

def test_numeric_directory_names():
    """Test path with numeric directory names."""
    path = Path("project/123/456/src")
    codeflash_output = is_build_output_dir(path) # 4.76μs -> 2.40μs (97.8% faster)

def test_build_like_prefix_in_filename():
    """Test that build-like names in filename (not directory) don't trigger match."""
    path = Path("src/buildscript.py")
    codeflash_output = is_build_output_dir(path) # 4.81μs -> 2.40μs (100% faster)

def test_dist_as_part_of_directory_name():
    """Test that 'dist' as part of a larger directory name doesn't match."""
    path = Path("project/distribution")
    codeflash_output = is_build_output_dir(path) # 4.58μs -> 2.35μs (94.5% faster)

def test_out_as_part_of_directory_name():
    """Test that 'out' as part of a larger directory name doesn't match."""
    path = Path("project/output")
    codeflash_output = is_build_output_dir(path) # 4.74μs -> 2.36μs (100% faster)

def test_single_dot_next_directory():
    """Test that '.next' at root is correctly identified."""
    path = Path(".next")
    codeflash_output = is_build_output_dir(path) # 4.88μs -> 2.32μs (110% faster)

def test_single_dot_nuxt_directory():
    """Test that '.nuxt' at root is correctly identified."""
    path = Path(".nuxt")
    codeflash_output = is_build_output_dir(path) # 4.75μs -> 2.20μs (115% faster)

def test_dot_next_in_middle_of_path():
    """Test '.next' directory in the middle of a path."""
    path = Path("nextjs-project/.next/build-output")
    codeflash_output = is_build_output_dir(path) # 5.35μs -> 2.29μs (133% faster)

def test_dot_nuxt_in_middle_of_path():
    """Test '.nuxt' directory in the middle of a path."""
    path = Path("nuxt-project/.nuxt/dist")
    codeflash_output = is_build_output_dir(path) # 5.26μs -> 2.37μs (122% faster)

def test_next_without_dot_prefix():
    """Test that 'next' without dot prefix is not matched."""
    path = Path("next")
    codeflash_output = is_build_output_dir(path) # 4.27μs -> 2.29μs (86.8% faster)

def test_nuxt_without_dot_prefix():
    """Test that 'nuxt' without dot prefix is not matched."""
    path = Path("nuxt")
    codeflash_output = is_build_output_dir(path) # 4.29μs -> 2.35μs (82.2% faster)

def test_many_regular_directories_no_build():
    """Test path with many regular directories but no build directories."""
    # Create a path with many regular directory components
    components = ["dir" + str(i) for i in range(100)]
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 14.9μs -> 4.32μs (245% faster)

def test_many_directories_with_build_at_end():
    """Test performance with many regular directories followed by build."""
    # Create a path with many regular directory components and build at end
    components = ["project" + str(i) for i in range(100)]
    components.append("build")
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 15.2μs -> 4.09μs (272% faster)

def test_many_directories_with_build_at_start():
    """Test performance with build directory at the start of many components."""
    # Create a path with build directory first, followed by many components
    components = ["build"] + ["subdir" + str(i) for i in range(100)]
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 9.51μs -> 2.56μs (271% faster)

def test_many_directories_with_dist_in_middle():
    """Test performance with dist directory in the middle of many components."""
    # Create a path with dist directory in the middle
    components = ["project" + str(i) for i in range(50)]
    components.append("dist")
    components.extend(["output" + str(i) for i in range(50)])
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 12.7μs -> 3.45μs (270% faster)

def test_alternating_build_and_regular_dirs():
    """Test path with alternating build and regular directories."""
    # Create alternating pattern: src/build/src/build/...
    components = []
    for i in range(50):
        if i % 2 == 0:
            components.append("src" + str(i))
        else:
            components.append("build")
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 7.16μs -> 2.52μs (184% faster)

def test_all_build_dir_types_in_single_path():
    """Test path containing all types of build directories."""
    # Path contains all recognized build directories
    path = Path("build/dist/out/.next/.nuxt")
    codeflash_output = is_build_output_dir(path) # 5.14μs -> 2.28μs (125% faster)

def test_path_with_hundred_regular_dirs():
    """Test performance with 100 regular directories (no build dirs)."""
    # Create a long path with only regular directories
    components = ["regular" + str(i) for i in range(100)]
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 14.8μs -> 4.26μs (248% faster)

def test_path_with_hundred_dirs_build_at_fifty():
    """Test performance with build directory at position 50 out of 100."""
    # Create path where build is at position 50
    components = ["dir" + str(i) for i in range(50)]
    components.append("build")
    components.extend(["dir" + str(i) for i in range(50, 100)])
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 12.2μs -> 3.59μs (241% faster)

def test_multiple_out_directories_in_path():
    """Test performance when the same build directory appears multiple times."""
    # Path with multiple 'out' directories
    path = Path("project/out/src/out/build/out/data")
    codeflash_output = is_build_output_dir(path) # 5.40μs -> 2.41μs (124% faster)

def test_very_long_single_directory_name():
    """Test with a very long single directory name that doesn't match build dirs."""
    # Create a very long directory name
    long_name = "a" * 500
    path = Path(long_name)
    codeflash_output = is_build_output_dir(path) # 4.64μs -> 2.29μs (102% faster)

def test_very_long_directory_name_containing_build():
    """Test with very long directory name that includes 'build'."""
    # Create a path with build as one of the components in a long name
    long_name = "prefix" + "a" * 500 + "/build/suffix"
    path = Path(long_name)
    codeflash_output = is_build_output_dir(path) # 5.79μs -> 2.38μs (143% faster)

def test_unicode_directory_names_no_build():
    """Test path with unicode characters but no build directories."""
    # Use unicode characters in directory names
    path = Path("プロジェクト/src/コンポーネント")
    codeflash_output = is_build_output_dir(path) # 5.33μs -> 2.44μs (119% faster)

def test_unicode_directory_names_with_build():
    """Test path with unicode characters and a build directory."""
    # Mix unicode characters with recognized build directory
    path = Path("プロジェクト/build/出力")
    codeflash_output = is_build_output_dir(path) # 5.52μs -> 2.40μs (130% faster)

def test_repeated_build_directory_100_times():
    """Test path with 'build' directory repeated 100 times (stress test)."""
    # Create path with build repeated many times
    components = ["build"] * 100
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 8.97μs -> 2.45μs (265% faster)

def test_all_build_types_repeated():
    """Test path with all build directory types repeated multiple times."""
    # Create pattern: build/dist/out/.next/.nuxt repeated 10 times
    build_types = ["build", "dist", "out", ".next", ".nuxt"]
    components = build_types * 10
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 6.79μs -> 2.40μs (182% faster)

def test_performance_no_matches_large_path():
    """Test performance when checking a large path with no matching directories."""
    # Create a very large path with only non-matching directories
    components = ["safe" + str(i % 10) for i in range(200)]
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 21.7μs -> 4.78μs (355% faster)

def test_performance_match_at_boundary():
    """Test performance when build directory is at the very end of large path."""
    # Create path with build at the very end
    components = ["safe" + str(i) for i in range(200)]
    components.append("dist")
    path = Path("/".join(components))
    codeflash_output = is_build_output_dir(path) # 27.7μs -> 5.86μs (372% faster)
# codeflash_output is used to check that the output of the original code is the same as that of the optimized code.
from codeflash.setup.detector import is_build_output_dir
from pathlib import Path

def test_is_build_output_dir():
    is_build_output_dir(Path())
🔎 Click to see Concolic Coverage Tests

To edit these changes git checkout codeflash/optimize-pr1308-2026-02-03T12.05.29 and push.

Codeflash Static Badge

The optimized code achieves a **143% speedup** (from 503μs to 206μs) through three key improvements:

## Primary Optimizations

1. **Eliminated expensive string operations**: The original code called `path.as_posix().split("/")` on every invocation, which:
   - Converts the Path object to a POSIX string representation
   - Allocates a new string
   - Splits it into a list
   
   The optimized version uses `path.parts`, which is a cached tuple property already available on Path objects, eliminating ~61% of the original runtime (line profiler shows `as_posix().split()` took 60.9% of execution time).

2. **Avoided set reconstruction**: The original code recreated the `build_dirs` set on every function call. The optimized version pre-computes `_BUILD_DIRS` as a module-level `frozenset`, eliminating the 5.5% overhead from set creation.

3. **Leveraged efficient set operation**: Instead of using a generator expression with `any()` that performs multiple `in` checks (33.6% of original time), the optimized code uses `isdisjoint()` - a built-in set method implemented in C that performs a single, optimized intersection check.

## Why This Matters

The test results show consistent speedups across all scenarios:
- **Simple paths**: 2-3x faster (e.g., `Path("build")`: 5.19μs → 2.34μs)
- **Deep nesting**: 2-3x faster even with complex paths
- **Large-scale tests**: 3-4x faster for paths with 100+ components (e.g., 100 regular dirs: 14.8μs → 4.26μs, **248% speedup**)
- **Worst-case scenarios**: The optimization shines brightest when checking long paths with no matches (21.7μs → 4.78μs, **355% speedup**)

The optimization scales particularly well because `isdisjoint()` can short-circuit as soon as it finds a match, while `path.parts` access is essentially free after the first call. This makes the function highly efficient for both hot paths that check many paths repeatedly and deep directory structures common in real projects.
@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 KRRT7 merged commit 2833e07 into js-suggester Feb 3, 2026
17 of 27 checks passed
@KRRT7 KRRT7 deleted the codeflash/optimize-pr1308-2026-02-03T12.05.29 branch February 3, 2026 12:17
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