perf(startup): defer httpx import off the cold-start path (#13)#143
Merged
Conversation
httpx is the single heaviest import in the tree (~65ms cold) yet nothing on the `nsc --help` / startup path needs an HTTP client. It was pulled in eagerly via the runtime → client → retry chain, plus the cache, schema, and login command modules. Defer it to first-request in all six so cold startup drops back under the 300ms project target. - http/client, http/retry, cli/cache_commands, schema/source, schema/loader, auth/verify: move `import httpx` into the functions that actually issue requests; keep type-only imports under TYPE_CHECKING. - test_lazy_httpx_import.py: importtime-based guard asserting httpx never loads on `nsc --help` (deterministic, unlike noisy wall-clock). - test_commands_dump: patch `httpx.stream` directly — the lazy import removes the `nsc.schema.loader.httpx` module attribute the tests patched. - benchmarks/test_startup: re-baseline THRESHOLD 250ms -> 300ms (the documented project target) so an over-threshold skip signals a real regression instead of firing on every run. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #13 (Path C: investigate + fix, then re-baseline).
What & why
Profiling
nsc --helpwith-X importtimeshowed httpx (~65ms, the single heaviest import in the tree) loading eagerly on every startup — through the always-importedruntime → client → retrychain, plus thecache/schema/logincommand modules registered inapp.py. Nothing on the--help/ cold-start path needs an HTTP client, so this was pure waste that had crept in over several releases (issue #13's exact prediction).All six modules now import httpx lazily, at first request. Type-only references stay under
TYPE_CHECKING.Result
-X importtimeonnsc --help(unconfigured home): httpx no longer appears at all.OVER THRESHOLDskip had fired on every run for ~7 releases, becoming folklore; it now passes normally and only skips on a genuine regression.Verification
just lint— ruff + mypy --strict clean.just test— 1367 passed, 1 skipped (bench gate).tests/cli/test_lazy_httpx_import.pyguards the invariant both ways (import-only +--helpinvocation).test_commands_dumptests updated: they patched the now-absentnsc.schema.loader.httpxattribute → patchhttpx.streamdirectly instead.Note on the threshold: it's defined "on the CI runner", and local hardware runs ~65ms slower + noisy. I'll confirm the real median against this PR's bench job (
continue-on-error, informational) and tune 300ms if CI disagrees.🤖 Generated with Claude Code