From 30ccfc536c18e76fd0457f9cd97d27a021b444d8 Mon Sep 17 00:00:00 2001 From: Ian Duffy Date: Fri, 21 Aug 2026 23:57:14 +0100 Subject: [PATCH 1/2] perf(no-ticket): skip the SDK import on custom-domain cache hits get_custom_domains() imported the cloudsmith_api SDK (~70ms) on every call. Import the API modules after the cache check, so only a cache miss pays that cost. Co-Authored-By: Claude Fable 5 --- .../test_credential_helper_install.py | 2 +- .../cli/tests/test_startup_imports.py | 21 ++++++++++++------- .../credential_helpers/custom_domains.py | 9 +++++--- 3 files changed, 21 insertions(+), 11 deletions(-) diff --git a/cloudsmith_cli/cli/tests/commands/test_credential_helper_install.py b/cloudsmith_cli/cli/tests/commands/test_credential_helper_install.py index bc14fabd..fd1c0a0f 100644 --- a/cloudsmith_cli/cli/tests/commands/test_credential_helper_install.py +++ b/cloudsmith_cli/cli/tests/commands/test_credential_helper_install.py @@ -810,7 +810,7 @@ def _fake_list(*_a, **_kw): ] with patch( - "cloudsmith_cli.credential_helpers.custom_domains.list_custom_domains", + "cloudsmith_cli.core.api.orgs.list_custom_domains", _fake_list, ): result = get_custom_domains( diff --git a/cloudsmith_cli/cli/tests/test_startup_imports.py b/cloudsmith_cli/cli/tests/test_startup_imports.py index 9cd997ea..3eb1dcaf 100644 --- a/cloudsmith_cli/cli/tests/test_startup_imports.py +++ b/cloudsmith_cli/cli/tests/test_startup_imports.py @@ -12,10 +12,10 @@ HEAVY_PREFIXES = ("mcp", "httpx", "cloudsmith_api", "requests") -def modules_loaded_by_cli_import(): +def modules_loaded_by_import(module_name="cloudsmith_cli.cli.commands.main"): code = ( "import json, sys\n" - "import cloudsmith_cli.cli.commands.main\n" + f"import {module_name}\n" "print(json.dumps(sorted(sys.modules)))\n" ) result = subprocess.run( @@ -27,14 +27,16 @@ def modules_loaded_by_cli_import(): return json.loads(result.stdout) -def test_cli_import_does_not_load_heavy_modules(): - modules = modules_loaded_by_cli_import() - heavy = [ +def heavy_modules_in(modules): + return [ name for name in modules if any(name == p or name.startswith(p + ".") for p in HEAVY_PREFIXES) ] - assert heavy == [] + + +def test_cli_import_does_not_load_heavy_modules(): + assert heavy_modules_in(modules_loaded_by_import()) == [] def test_cli_import_does_not_load_command_modules(): @@ -42,7 +44,12 @@ def test_cli_import_does_not_load_command_modules(): allowed = {package + "main", package + "registry"} loaded = [ name - for name in modules_loaded_by_cli_import() + for name in modules_loaded_by_import() if name.startswith(package) and name not in allowed ] assert loaded == [] + + +def test_docker_helper_import_does_not_load_heavy_modules(): + modules = modules_loaded_by_import("cloudsmith_cli.credential_helpers.docker") + assert heavy_modules_in(modules) == [] diff --git a/cloudsmith_cli/credential_helpers/custom_domains.py b/cloudsmith_cli/credential_helpers/custom_domains.py index cec75ee6..6dd7c27b 100644 --- a/cloudsmith_cli/credential_helpers/custom_domains.py +++ b/cloudsmith_cli/credential_helpers/custom_domains.py @@ -19,9 +19,6 @@ from pathlib import Path from ..cli.config import get_default_config_path -from ..core.api.exceptions import ApiException -from ..core.api.init import initialise_api -from ..core.api.orgs import list_custom_domains from ..core.cache_utils import atomic_write_json from ..core.credentials.models import CredentialResult from .default_domains import DomainType, domain_type_from_server @@ -305,6 +302,12 @@ def get_custom_domains( logger.debug("Fetching custom domains from API for %s", org) + # The API modules pull in the cloudsmith_api SDK (~70ms). Import them + # here so that a cache hit skips that cost. + from ..core.api.exceptions import ApiException + from ..core.api.init import initialise_api + from ..core.api.orgs import list_custom_domains + if configure_api: initialise_api(host=api_host, credential=credential) From a8485349460b5a921b790c1e787c3040c0e9667e Mon Sep 17 00:00:00 2001 From: Ian Duffy Date: Sat, 22 Aug 2026 00:04:03 +0100 Subject: [PATCH 2/2] chore(no-ticket): drop the import-cost comment Co-Authored-By: Claude Fable 5 --- cloudsmith_cli/credential_helpers/custom_domains.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/cloudsmith_cli/credential_helpers/custom_domains.py b/cloudsmith_cli/credential_helpers/custom_domains.py index 6dd7c27b..1cdb53bc 100644 --- a/cloudsmith_cli/credential_helpers/custom_domains.py +++ b/cloudsmith_cli/credential_helpers/custom_domains.py @@ -302,8 +302,6 @@ def get_custom_domains( logger.debug("Fetching custom domains from API for %s", org) - # The API modules pull in the cloudsmith_api SDK (~70ms). Import them - # here so that a cache hit skips that cost. from ..core.api.exceptions import ApiException from ..core.api.init import initialise_api from ..core.api.orgs import list_custom_domains