Skip to content

Conversation

@KRRT7
Copy link
Contributor

@KRRT7 KRRT7 commented May 27, 2025

User description

defer optimizer
defer git imports
libcst.matchers is heavy
defer jedi
discover_unit_tests.py


PR Type

Enhancement


Description

  • Lazy-load heavy dependencies inside functions

  • Remove top-level git, jedi, and libcst.matchers imports

  • Defer benchmarking modules imports until necessary

  • Improve CLI startup time by deferring imports


Changes walkthrough 📝

Relevant files
Enhancement
cli.py
Lazy-load Git and GitHub imports                                                 

codeflash/cli_cmds/cli.py

  • Removed top-level git and GitHub utils imports
  • Moved git_utils and github_utils imports inside functions
  • Added conditional git import in parsing logic
  • +29/-21 
    code_extractor.py
    Defer `libcst.matchers` import                                                     

    codeflash/code_utils/code_extractor.py

  • Removed module-level libcst.matchers import
  • Added runtime import of libcst.matchers in transformer
  • +2/-1     
    code_context_extractor.py
    Lazy-load `jedi` inside context extractor                               

    codeflash/context/code_context_extractor.py

  • Removed top-level jedi imports
  • Added TYPE_CHECKING guard for Name type
  • Deferred jedi imports inside functions
  • +8/-2     
    discover_unit_tests.py
    Defer `jedi` import in test discovery                                       

    codeflash/discovery/discover_unit_tests.py

  • Removed module-level jedi import
  • Added deferred jedi import in test processing
  • +2/-1     
    optimizer.py
    Lazy-load optimizer dependencies                                                 

    codeflash/optimization/optimizer.py

  • Removed top-level benchmarking and analysis imports
  • Deferred imports for checkpoint and code replacers
  • Lazy-loaded benchmarking modules inside run method
  • Moved discovery and static-analysis imports into methods
  • +20/-12 

    Need help?
  • Type /help how to ... in the comments thread for any questions about PR-Agent usage.
  • Check out the documentation for more information.
  • defer optimizer
    defer git imports
    libcst.matchers is heavy
    defer jedi
    discover_unit_tests.py
    @github-actions
    Copy link

    PR Reviewer Guide 🔍

    Here are some key observations to aid the review process:

    ⏱️ Estimated effort to review: 3 🔵🔵🔵⚪⚪
    🧪 No relevant tests
    🔒 No security concerns identified
    ⚡ Recommended focus areas for review

    Dynamic Git Import

    Dynamic import of git and related utilities may raise runtime errors if the module is unavailable. Consider adding exception handling or fallback messaging to ensure graceful failures.

    if env_utils.get_pr_number() is not None:
        import git
    
        from codeflash.code_utils.git_utils import get_repo_owner_and_name
        from codeflash.code_utils.github_utils import get_github_secrets_page_url, require_github_app_or_exit
    
        assert env_utils.ensure_codeflash_api_key(), (
            "Codeflash API key not found. When running in a Github Actions Context, provide the "
    Repeated Jedi Imports

    Deferred jedi imports inside functions may incur repeated overhead on each call. Consider caching the jedi module after the first import or consolidating the import to reduce repeated overhead.

    import jedi
    
    # Use jedi to find function to optimize
    CST Matcher Import

    Importing libcst.matchers inside leave_ImportFrom will execute on every CST node visit, potentially impacting performance. Consider moving this import to a class-level scope or caching the matcher alias.

    import libcst.matchers as m
    
    if (

    @github-actions
    Copy link

    PR Code Suggestions ✨

    Explore these optional code suggestions:

    CategorySuggestion                                                                                                                                    Impact
    Possible issue
    Unnest PR-number validation block

    The PR-number validation block is wrongly nested under the --benchmark branch and
    will only run when benchmarks are enabled. Dedent this block so it runs
    unconditionally in process_pyproject_config.

    codeflash/cli_cmds/cli.py [137-164]

     if args.benchmark:
    -    assert args.benchmarks_root is not None, "--benchmarks-root must be specified when running with --benchmark"
    -    ...
    -    assert Path(args.benchmarks_root).resolve().is_relative_to(Path(args.tests_root).resolve()), (
    -        f"--benchmarks-root {args.benchmarks_root} must be a subdirectory of --tests-root {args.tests_root}"
    +    ...  # benchmark-only checks
    +
    +if env_utils.get_pr_number() is not None:
    +    import git
    +    from codeflash.code_utils.git_utils import get_repo_owner_and_name
    +    from codeflash.code_utils.github_utils import get_github_secrets_page_url, require_github_app_or_exit
    +
    +    assert env_utils.ensure_codeflash_api_key(), (
    +        "Codeflash API key not found. ..."
         )
    -    if env_utils.get_pr_number() is not None:
    -        import git
    +    repo = git.Repo(search_parent_directories=True)
    +    owner, repo_name = get_repo_owner_and_name(repo)
    +    require_github_app_or_exit(owner, repo_name)
     
    -        from codeflash.code_utils.git_utils import get_repo_owner_and_name
    -        from codeflash.code_utils.github_utils import get_github_secrets_page_url, require_github_app_or_exit
    -
    -        assert env_utils.ensure_codeflash_api_key(), (
    -            "Codeflash API key not found. ..."
    -        )
    -        repo = git.Repo(search_parent_directories=True)
    -        owner, repo_name = get_repo_owner_and_name(repo)
    -        require_github_app_or_exit(owner, repo_name)
    -
    Suggestion importance[1-10]: 9

    __

    Why: The PR-number validation (env_utils.get_pr_number()) is incorrectly nested under if args.benchmark, so it only runs during benchmarking; moving it out ensures it always executes and fixes the logic.

    High
    Add missing git_utils imports

    The calls to check_running_in_git_repo and confirm_proceeding_with_no_git_repo are
    used below but not imported in this scope. Include these in the git_utils import.

    codeflash/cli_cmds/cli.py [191-196]

     def handle_optimize_all_arg_parsing(args: Namespace) -> Namespace:
         if hasattr(args, "all"):
             import git
     
    -        from codeflash.code_utils.git_utils import check_and_push_branch, get_repo_owner_and_name
    +        from codeflash.code_utils.git_utils import (
    +            check_running_in_git_repo,
    +            confirm_proceeding_with_no_git_repo,
    +            check_and_push_branch,
    +            get_repo_owner_and_name,
    +        )
             from codeflash.code_utils.github_utils import require_github_app_or_exit
    Suggestion importance[1-10]: 7

    __

    Why: The functions check_running_in_git_repo and confirm_proceeding_with_no_git_repo are used later in handle_optimize_all_arg_parsing but not imported, leading to potential NameError; adding them ensures the code runs correctly.

    Medium

    @misrasaurabh1 misrasaurabh1 self-requested a review May 30, 2025 05:10
    @KRRT7 KRRT7 enabled auto-merge May 30, 2025 19:50
    @KRRT7 KRRT7 merged commit ba6a1c0 into main May 30, 2025
    16 checks passed
    @KRRT7 KRRT7 deleted the deferred-imports branch May 30, 2025 20:02
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    3 participants