Skip to content

Commit a8c0041

Browse files
committed
perf: skip telemetry/banner for auth and compare commands
Restructure main() command dispatch so auth and compare exit early without loading telemetry (sentry, posthog), version_check, or the banner. Defer cmd_auth.py imports into functions. auth status: ~1000ms → 237ms (4.2x) compare --help: ~297ms → 38ms (7.9x)
1 parent 05a7641 commit a8c0041

2 files changed

Lines changed: 36 additions & 29 deletions

File tree

codeflash/cli_cmds/cmd_auth.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
import os
44

5-
import click
6-
7-
from codeflash.cli_cmds.console import console
8-
from codeflash.cli_cmds.oauth_handler import perform_oauth_signin
9-
from codeflash.code_utils.env_utils import get_codeflash_api_key
10-
from codeflash.code_utils.shell_utils import save_api_key_to_rc
11-
from codeflash.either import is_successful
12-
135

146
def auth_login() -> None:
157
"""Perform OAuth login and save the API key."""
8+
import click
9+
10+
from codeflash.cli_cmds.console import console
11+
from codeflash.cli_cmds.oauth_handler import perform_oauth_signin
12+
from codeflash.code_utils.env_utils import get_codeflash_api_key
13+
from codeflash.code_utils.shell_utils import save_api_key_to_rc
14+
from codeflash.either import is_successful
15+
1616
try:
1717
existing_api_key = get_codeflash_api_key()
1818
except OSError:
@@ -41,6 +41,9 @@ def auth_login() -> None:
4141

4242
def auth_status() -> None:
4343
"""Check and display current authentication status."""
44+
from codeflash.cli_cmds.console import console
45+
from codeflash.code_utils.env_utils import get_codeflash_api_key
46+
4447
try:
4548
api_key = get_codeflash_api_key()
4649
except OSError:

codeflash/main.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,29 @@ def main() -> None:
3333

3434
args = parse_args()
3535

36-
# Heavy imports deferred until after parse_args() so --help exits fast
36+
# Auth commands skip banner, telemetry, and version check entirely
37+
if args.command == "auth":
38+
from codeflash.cli_cmds.cmd_auth import auth_login, auth_status
39+
40+
if args.auth_command == "login":
41+
auth_login()
42+
elif args.auth_command == "status":
43+
auth_status()
44+
else:
45+
from codeflash.code_utils.code_utils import exit_with_message
46+
47+
exit_with_message("Usage: codeflash auth {login,status}", error_on_exit=True)
48+
return
49+
50+
# Compare command only needs its own imports
51+
if args.command == "compare":
52+
print_codeflash_banner()
53+
from codeflash.cli_cmds.cmd_compare import run_compare
54+
55+
run_compare(args)
56+
return
57+
58+
# All other commands need the full stack
3759
from pathlib import Path
3860

3961
from codeflash.cli_cmds.cli import process_pyproject_config
@@ -44,10 +66,7 @@ def main() -> None:
4466
from codeflash.telemetry import posthog_cf
4567
from codeflash.telemetry.sentry import init_sentry
4668

47-
if args.command != "auth":
48-
print_codeflash_banner()
49-
50-
# Check for newer version for all commands
69+
print_codeflash_banner()
5170
check_for_newer_minor_version()
5271

5372
if args.command:
@@ -58,18 +77,7 @@ def main() -> None:
5877
init_sentry(enabled=not disable_telemetry, exclude_errors=True)
5978
posthog_cf.initialize_posthog(enabled=not disable_telemetry)
6079

61-
if args.command == "auth":
62-
from codeflash.cli_cmds.cmd_auth import auth_login, auth_status
63-
64-
if args.auth_command == "login":
65-
auth_login()
66-
elif args.auth_command == "status":
67-
auth_status()
68-
else:
69-
from codeflash.code_utils.code_utils import exit_with_message
70-
71-
exit_with_message("Usage: codeflash auth {login,status}", error_on_exit=True)
72-
elif args.command == "init":
80+
if args.command == "init":
7381
from codeflash.cli_cmds.cmd_init import init_codeflash
7482

7583
init_codeflash()
@@ -81,10 +89,6 @@ def main() -> None:
8189
from codeflash.cli_cmds.extension import install_vscode_extension
8290

8391
install_vscode_extension()
84-
elif args.command == "compare":
85-
from codeflash.cli_cmds.cmd_compare import run_compare
86-
87-
run_compare(args)
8892
elif args.command == "optimize":
8993
from codeflash.tracer import main as tracer_main
9094

0 commit comments

Comments
 (0)