Skip to content

Commit 2fc528e

Browse files
committed
perf: defer heavy imports in env_utils and shell_utils
Defer console, formatter, code_utils, registry, and lsp.helpers imports from module level into the functions that use them. Inline is_LSP_enabled (a one-liner env var check) to avoid importing lsp.helpers on the happy path of get_codeflash_api_key. auth status: 237ms → 160ms on Azure Standard_D4s_v5.
1 parent 992e91a commit 2fc528e

2 files changed

Lines changed: 19 additions & 8 deletions

File tree

codeflash/code_utils/env_utils.py

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,16 @@
99
from pathlib import Path
1010
from typing import Any, Optional
1111

12-
from codeflash.cli_cmds.console import logger
13-
from codeflash.code_utils.code_utils import exit_with_message
14-
from codeflash.code_utils.formatter import format_code
1512
from codeflash.code_utils.shell_utils import read_api_key_from_shell_config, save_api_key_to_rc
16-
from codeflash.languages.registry import get_language_support_by_common_formatters
17-
from codeflash.lsp.helpers import is_LSP_enabled
1813

1914

2015
def check_formatter_installed(
2116
formatter_cmds: list[str], exit_on_failure: bool = True, language: str = "python"
2217
) -> bool:
18+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
19+
from codeflash.code_utils.formatter import format_code # noqa: PLC0415
20+
from codeflash.languages.registry import get_language_support_by_common_formatters # noqa: PLC0415
21+
2322
if not formatter_cmds or formatter_cmds[0] == "disabled":
2423
return True
2524
first_cmd = formatter_cmds[0]
@@ -69,6 +68,8 @@ def check_formatter_installed(
6968

7069
@lru_cache(maxsize=1)
7170
def get_codeflash_api_key() -> str:
71+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
72+
7273
# Check environment variable first
7374
env_api_key = os.environ.get("CODEFLASH_API_KEY")
7475
shell_api_key = read_api_key_from_shell_config()
@@ -78,7 +79,7 @@ def get_codeflash_api_key() -> str:
7879
# If we have an env var but it's not in shell config, save it for persistence
7980
if env_api_key and not shell_api_key:
8081
try:
81-
from codeflash.either import is_successful
82+
from codeflash.either import is_successful # noqa: PLC0415
8283

8384
logger.debug("env_utils.py:get_codeflash_api_key - Saving API key from environment to shell config")
8485
result = save_api_key_to_rc(env_api_key)
@@ -96,7 +97,8 @@ def get_codeflash_api_key() -> str:
9697
# Prefer the shell configuration over environment variables for lsp,
9798
# as the API key may change in the RC file during lsp runtime. Since the LSP client (extension) can restart
9899
# within the same process, the environment variable could become outdated.
99-
api_key = shell_api_key or env_api_key if is_LSP_enabled() else env_api_key or shell_api_key
100+
is_lsp = os.getenv("CODEFLASH_LSP", default="false").lower() == "true"
101+
api_key = shell_api_key or env_api_key if is_lsp else env_api_key or shell_api_key
100102

101103
api_secret_docs_message = "For more information, refer to the documentation at [https://docs.codeflash.ai/optimizing-with-codeflash/codeflash-github-actions#manual-setup]." # noqa
102104
if not api_key:
@@ -106,6 +108,8 @@ def get_codeflash_api_key() -> str:
106108
f"{api_secret_docs_message}"
107109
)
108110
if is_repo_a_fork():
111+
from codeflash.code_utils.code_utils import exit_with_message # noqa: PLC0415
112+
109113
msg = (
110114
"Codeflash API key not detected in your environment. It appears you're running Codeflash from a GitHub fork.\n"
111115
"For external contributors, please ensure you've added your own API key to your fork's repository secrets and set it as the CODEFLASH_API_KEY environment variable.\n"
@@ -124,6 +128,8 @@ def get_codeflash_api_key() -> str:
124128

125129

126130
def ensure_codeflash_api_key() -> bool:
131+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
132+
127133
try:
128134
get_codeflash_api_key()
129135
except OSError:

codeflash/code_utils/shell_utils.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from pathlib import Path
99
from typing import TYPE_CHECKING, Optional
1010

11-
from codeflash.cli_cmds.console import logger
1211
from codeflash.code_utils.compat import LF
1312
from codeflash.either import Failure, Success
1413

@@ -41,6 +40,8 @@ def is_powershell() -> bool:
4140
2. COMSPEC pointing to powershell.exe
4241
3. TERM_PROGRAM indicating Windows Terminal (often uses PowerShell)
4342
"""
43+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
44+
4445
if os.name != "nt":
4546
return False
4647

@@ -72,6 +73,8 @@ def is_powershell() -> bool:
7273

7374
def read_api_key_from_shell_config() -> Optional[str]:
7475
"""Read API key from shell configuration file."""
76+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
77+
7578
shell_rc_path = get_shell_rc_path()
7679
# Ensure shell_rc_path is a Path object for consistent handling
7780
if not isinstance(shell_rc_path, Path):
@@ -127,6 +130,8 @@ def get_api_key_export_line(api_key: str) -> str:
127130

128131
def save_api_key_to_rc(api_key: str) -> Result[str, str]:
129132
"""Save API key to the appropriate shell configuration file."""
133+
from codeflash.cli_cmds.console import logger # noqa: PLC0415
134+
130135
shell_rc_path = get_shell_rc_path()
131136
# Ensure shell_rc_path is a Path object for consistent handling
132137
if not isinstance(shell_rc_path, Path):

0 commit comments

Comments
 (0)