Skip to content

Commit 6f3f884

Browse files
committed
perf: defer module-level imports in main.py into main()
Move cli, console, env_utils, checkpoint, config_parser, and version_check imports from module level into main(). These imports trigger the full dependency chain (cfapi, models, PrComment, libcst, requests, Rich) costing ~500ms on every CLI invocation — even for simple commands that dont need most of these modules. Also moves paneled_text import into print_codeflash_banner() and passes process_pyproject_config as parameter to _handle_config_loading to avoid a module-level reference.
1 parent 9e69239 commit 6f3f884

1 file changed

Lines changed: 14 additions & 11 deletions

File tree

codeflash/main.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,27 @@
88

99
import os
1010
import sys
11-
from pathlib import Path
12-
from typing import TYPE_CHECKING
11+
from typing import TYPE_CHECKING, Callable
1312

1413
if "--subagent" in sys.argv:
1514
os.environ["CODEFLASH_SUBAGENT_MODE"] = "true"
1615
import warnings
1716

1817
warnings.filterwarnings("ignore")
1918

20-
from codeflash.cli_cmds.cli import parse_args, process_pyproject_config
21-
from codeflash.cli_cmds.console import paneled_text
22-
from codeflash.code_utils import env_utils
23-
from codeflash.code_utils.checkpoint import ask_should_use_checkpoint_get_functions
24-
from codeflash.code_utils.config_parser import parse_config_file
25-
from codeflash.code_utils.version_check import check_for_newer_minor_version
26-
2719
if TYPE_CHECKING:
2820
from argparse import Namespace
2921

3022

3123
def main() -> None:
3224
"""Entry point for the codeflash command-line interface."""
25+
from pathlib import Path
26+
27+
from codeflash.cli_cmds.cli import parse_args, process_pyproject_config
28+
from codeflash.code_utils import env_utils
29+
from codeflash.code_utils.checkpoint import ask_should_use_checkpoint_get_functions
30+
from codeflash.code_utils.config_parser import parse_config_file
31+
from codeflash.code_utils.version_check import check_for_newer_minor_version
3332
from codeflash.telemetry import posthog_cf
3433
from codeflash.telemetry.sentry import init_sentry
3534

@@ -89,7 +88,7 @@ def main() -> None:
8988
ask_run_end_to_end_test(args)
9089
else:
9190
# Check for first-run experience (no config exists)
92-
loaded_args = _handle_config_loading(args)
91+
loaded_args = _handle_config_loading(args, process_pyproject_config)
9392
if loaded_args is None:
9493
sys.exit(0)
9594
args = loaded_args
@@ -105,14 +104,17 @@ def main() -> None:
105104
optimizer.run_with_args(args)
106105

107106

108-
def _handle_config_loading(args: Namespace) -> Namespace | None:
107+
def _handle_config_loading(
108+
args: Namespace, process_pyproject_config: Callable[[Namespace], Namespace]
109+
) -> Namespace | None:
109110
"""Handle config loading with first-run experience support.
110111
111112
If no config exists and not in CI, triggers the first-run experience.
112113
Otherwise, loads config normally.
113114
114115
Args:
115116
args: CLI args namespace.
117+
process_pyproject_config: Config processing function.
116118
117119
Returns:
118120
Updated args with config loaded, or None if user cancelled first-run.
@@ -157,6 +159,7 @@ def print_codeflash_banner() -> None:
157159
Renders the Codeflash ASCII logo inside a non-expanding panel titled with
158160
https://codeflash.ai, using bold gold text for visual emphasis.
159161
"""
162+
from codeflash.cli_cmds.console import paneled_text
160163
from codeflash.cli_cmds.console_constants import CODEFLASH_LOGO
161164

162165
paneled_text(

0 commit comments

Comments
 (0)