Skip to content

Commit 8c63e8d

Browse files
⚡️ Speed up function get_dependency_manager_installation_string by 127% in PR #208 (bump-gha-uv-version)
Here's an optimized version of your program with reduced runtime, specifically targeting the bottlenecks. ### **Optimizations performed:** 1. **Constant Folding:** The sys.version_info fetch and string formatting for the Python version is only necessary for the non-UV case and does not need to be constructed unless used. 2. **Precomputed Templates:** The output strings are constants and can be stored as such, to avoid reconstructing them on each call. 3. **Avoid Unnecessary Formatting:** For the setup-python path, the version string is constant across invocations of the same interpreter, so can use lazy-static initialization. 4. **Reduced Function Overhead:** Restructured code to minimize code execution paths and avoid unnecessary work. --- --- **Summary of changes:** - Only creates formatted strings once per interpreter lifetime, so on repeated heavy calls, time and allocations are minimized. - `sys.version_info` is not re-accessed on every call. - Preserved all logical comments; comments were added only where code was optimized. This rewrite should significantly improve per-call performance of `get_dependency_manager_installation_string()`.
1 parent b461001 commit 8c63e8d

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

codeflash/cli_cmds/cmd_init.py

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ def collect_setup_info() -> SetupInfo:
239239
else:
240240
apologize_and_exit()
241241
else:
242-
tests_root = Path(curdir) / Path(cast(str, tests_root_answer))
242+
tests_root = Path(curdir) / Path(cast("str", tests_root_answer))
243243
tests_root = tests_root.relative_to(curdir)
244244
ph("cli-tests-root-provided")
245245

@@ -302,7 +302,7 @@ def collect_setup_info() -> SetupInfo:
302302
elif benchmarks_answer == no_benchmarks_option:
303303
benchmarks_root = None
304304
else:
305-
benchmarks_root = tests_root / Path(cast(str, benchmarks_answer))
305+
benchmarks_root = tests_root / Path(cast("str", benchmarks_answer))
306306

307307
# TODO: Implement other benchmark framework options
308308
# if benchmarks_root:
@@ -354,9 +354,9 @@ def collect_setup_info() -> SetupInfo:
354354
module_root=str(module_root),
355355
tests_root=str(tests_root),
356356
benchmarks_root=str(benchmarks_root) if benchmarks_root else None,
357-
test_framework=cast(str, test_framework),
357+
test_framework=cast("str", test_framework),
358358
ignore_paths=ignore_paths,
359-
formatter=cast(str, formatter),
359+
formatter=cast("str", formatter),
360360
git_remote=str(git_remote),
361361
)
362362

@@ -466,7 +466,7 @@ def check_for_toml_or_setup_file() -> str | None:
466466
click.echo("⏩️ Skipping pyproject.toml creation.")
467467
apologize_and_exit()
468468
click.echo()
469-
return cast(str, project_name)
469+
return cast("str", project_name)
470470

471471

472472
def install_github_actions(override_formatter_check: bool = False) -> None:
@@ -619,17 +619,10 @@ def get_dependency_installation_commands(dep_manager: DependencyManager) -> tupl
619619

620620

621621
def get_dependency_manager_installation_string(dep_manager: DependencyManager) -> str:
622-
py_version = sys.version_info
623-
python_version_string = f"'{py_version.major}.{py_version.minor}'"
622+
# Return precomputed string to avoid repeated formatting and runtime cost
624623
if dep_manager == DependencyManager.UV:
625-
return """name: 🐍 Setup UV
626-
uses: astral-sh/setup-uv@v6
627-
with:
628-
enable-cache: true"""
629-
return f"""name: 🐍 Set up Python
630-
uses: actions/setup-python@v5
631-
with:
632-
python-version: {python_version_string}"""
624+
return _UV_INSTALL_STR
625+
return _SETUP_PYTHON_INSTALL_STR
633626

634627

635628
def get_github_action_working_directory(toml_path: Path, git_root: Path) -> str:
@@ -966,3 +959,16 @@ def ask_for_telemetry() -> bool:
966959
)
967960

968961
return enable_telemetry
962+
963+
964+
_UV_INSTALL_STR = """name: 🐍 Setup UV
965+
uses: astral-sh/setup-uv@v6
966+
with:
967+
enable-cache: true"""
968+
969+
_PYTHON_VERSION_STRING = f"'{sys.version_info.major}.{sys.version_info.minor}'"
970+
971+
_SETUP_PYTHON_INSTALL_STR = f"""name: 🐍 Set up Python
972+
uses: actions/setup-python@v5
973+
with:
974+
python-version: {_PYTHON_VERSION_STRING}"""

0 commit comments

Comments
 (0)