Skip to content

perf(cli): load command code only after argument parsing - #947

Merged
kvlonge merged 2 commits into
mainfrom
perf/lean-cli-startup
Oct 5, 2026
Merged

kvlonge merged 2 commits into
mainfrom
perf/lean-cli-startup

Conversation

@kvlonge

@kvlonge kvlonge commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Why

Every sqb invocation imported about 930 modules before parsing arguments, so sqb --version and every --help took ~0.8 s of CPU, and every command paid that before doing any work. Stage 2 of CHI-482 (CHI-499); it also lets the planned no-change compile fast path answer before loading the compiler.

Changes

  • Parse arguments first; load dispatch, error handling, compute logs and observability only after a command is parsed.
  • Defer the warehouse validation, adapter resolution, the provider base class and rich to where they are used; move the compile request models and warehouse scope context to light modules.
  • Fresh-interpreter guard that fails if warehouse drivers, Dagster, dbt, pydantic-settings or rich load at startup, and if compiler or model packages load for --version/--help.

Verification

  • CPU (5 runs, fresh processes): --version and --help 0.77 → 0.12 s; startup before the compile handler ~0.75 → ~0.35–0.45 s; warm compile unchanged.
  • Byte-identical help for 43 commands, identical output and exit codes for 34 bad-argument cases and 17 --json cases.
  • One independent review; make test, make check-ci.

The CLI entry imported the dispatch, error-projection, compute-log and
observability layers before parsing arguments, which pulled in compiler,
planner, adapter and output models plus pydantic-settings. `sqb --version`
and every `--help` paid about 0.7 s of CPU for roughly 930 modules.

Parsing now loads only the parser and its type modules; the dispatch layers
load after a command is parsed. The dispatch path itself no longer imports
heavy models for annotations: the compile request models move beside the
CLI entry models, the warehouse scope context moves out of the warehouse
resolution module, provider and output annotations use lazy type aliases,
and the provider base class, pydantic and rich load where they are used.

Help text, parse errors, exit codes and JSON output are unchanged. A
fresh-process test guards that version, help, parse errors and a plain
compile do not load warehouse drivers, integrations, pydantic-settings or
rich, and that parsing alone loads no compiler or pydantic modules.
@kvlonge
kvlonge enabled auto-merge (squash) October 5, 2026 14:30
@kvlonge
kvlonge merged commit 8fced25 into main Oct 5, 2026
30 checks passed
@kvlonge
kvlonge deleted the perf/lean-cli-startup branch October 5, 2026 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant