Skip to content

perf(cli): cut the CPU time of the startup-import row by half #3550

Closed

Description

@dan-petty

Problem

The startup-imports row of devops ci (#2164) starts 55 fresh interpreters. Over three runs on one host it used 57 to 71 s of CPU time (53 to 64 s user). The 57 pytest tests that it replaces used 76 to 129 s. #2369 set the goal at half of the old CPU time, by a cut of the import of devops_cli.main or of the work of the help. The row moves the cost and runs four probes at a time, so it does not reach the goal.

The cost split of #2369: a bare interpreter costs 0.03 s, and import devops_cli.entry costs 0.25 to 0.36 s. The rest is the work of main and of the help of each command group.

Proposed solution

  • Measure the import of devops_cli.main and the work of the help of each command group with tests/gate/startup_imports.py (it prints the CPU time and, for a failed run, the ten slowest imports).
  • Cut the largest import or the largest piece of work in the help, so that the CPU time of the row is at most half of 76 s.
  • Keep the four module limits, the control and -X importtime.

Existing tool considered

python -X importtime and the row itself measure the cost. Nothing is built.

Acceptance criteria

  • The row startup-imports uses at most 38 s of CPU time (user plus system) over three runs on one host.
  • The limits and the control are as before.

Alternatives check of 2026-11-07: not-applicable.
Class: perf, no search.
Tool: none found.

Proposed design

Problem statement

The startup-imports row of devops ci starts 55 fresh interpreters and uses 57 to 71 s of CPU time. The old 57 pytest tests used 76 to 129 s. The issue sets the goal at 38 s, half of the 76 s lower bound. The row reports the cost but does not cut it.

Acceptance criteria

Requirement: Startup import CPU budget

The startup-imports row SHALL use at most 38.0 s of child CPU time on each of three consecutive runs on one host.

Scenario: Three consecutive runs

  • WHEN A developer runs the startup-imports row of devops ci three times on the same host.
  • THEN Each run prints a child CPU time of at most 38.0 s and exits 0.

Requirement: Lazy command help

The root help SHALL import no command implementation module.

Scenario: Root help loads no command implementation

  • WHEN A fresh interpreter runs devops_cli.entry.main(["--help"]).
  • THEN No module whose name starts with devops_cli.commands. is in sys.modules.

Requirement: Complete command help

The root help SHALL list every command name that _COMMAND_SPECS holds.

Scenario: Root help lists all commands

  • WHEN A fresh interpreter runs devops_cli.entry.main(["--help"]).
  • THEN The output contains each command name from _COMMAND_SPECS.

Requirement: Startup import limits

The four startup-import module limits SHALL stay at 550, 75, 1100 and 250.

Scenario: Limits unchanged

  • WHEN A test reads the four DEFAULT_STARTUP_IMPORT_* constants.
  • THEN The values are 550, 75, 1100 and 250.

Requirement: Heavy import control

The control probe MUST still fail a help run that imports pydantic_ai.

Scenario: Control catches a heavy import

  • WHEN The control runs --help after import pydantic_ai.
  • THEN control_problems returns an empty list for a probe that loads pydantic_ai and more than 550 modules.

Requirement: Importtime flag

Each help probe SHALL run under python -X importtime.

Scenario: Importtime remains

  • WHEN A test reads the argument list that probe_startup_imports passes to subprocess.run.
  • THEN The argument list contains -X and importtime.

Existing Tool Considered

No open, maintained tool covers this. The alternatives check returned not-applicable for a perf item and named no tool. python -X importtime and tests/gate/startup_imports.py measure the cost but do not cut it. The change builds no new tool; it changes the existing CLI and probe.

Key questions

  • Where does the row report its CPU time?: tests/gate/startup_imports.py reads resource.getrusage(resource.RUSAGE_CHILDREN) after all probes and prints user plus system child CPU time. [sources: tests/gate/startup_imports.py]
  • Which probes does the row run for the help?: help_argvs() returns --version, the root --help, each command in _COMMAND_SPECS, and every argv in the budget and telemetry tables. Each probe starts a fresh interpreter, imports devops_cli.entry, calls entry.main, and runs under -X importtime. [sources: tests/gate/startup_imports.py, src/devops_cli/entry.py:28]
  • How should the change cut the CPU time?: Make the command registry in src/devops_cli/main.py lazy. Keep each command name and its help text in data, and import the command implementation module only when the command runs. The help probes then import command metadata only. (best practice) [sources: tests/gate/startup_imports.py, src/devops_cli/main.py, src/devops_cli/entry.py:28]
    • Chosen: Lazy command registry. It cuts the import of command implementation modules from every help probe and keeps all probes and limits.
    • Rejected: Fewer help probes. It reduces coverage of command groups and hides a regression in an untested help.
    • Rejected: More parallel probes. It cuts wall time, not CPU time, and the row already runs four probes at a time.
  • What CPU budget should the row enforce?: Use 38.0 s of child CPU time, half of the 76 s lower bound of the old pytest suite. Add CPU_BUDGET_SECONDS = 38.0 to tests/gate/startup_imports.py and make main return 1 when the measured child CPU time is greater. (best practice) [sources: tests/gate/startup_imports.py]
    • Chosen: Enforce 38.0 s. It is the issue's goal and makes the row fail when the cost returns.
    • Rejected: Report only. A report does not stop the row from returning above the goal.
    • Rejected: Enforce wall time. The issue sets CPU time as the metric; wall time depends on host load.
  • Should the change reduce the number of help probes to reach the budget?: No. Keep every help argv, the four module limits, the control and -X importtime. Cut the per-probe import cost with the lazy registry. (best practice) [sources: tests/gate/startup_imports.py]
    • Chosen: Keep every probe. It keeps coverage of every command group and follows the issue's constraint.
    • Rejected: Sample command groups. It hides a regression in an untested command group.

Fit

Fits in one pull request.

Suspected block

None.

Dependencies

None.

Activity

  1. added
    source/agent
    type/infra
    scope/cli
    status/new
    value/medium
    effort/medium
    on Nov 4, 2026
  2. dan-petty commented on Nov 4, 2026

    @dan-petty
    OwnerAuthor

    Intake placed this item to the backlog: intake puts every new item in the backlog. A person plans the milestones.

    • Type: type/infra. The perf prefix of the title gives the type.
    • Scope: scope/cli. The (cli) scope of the title gives the scope.
    • Priority: P2-Medium. The row misses its goal by a large margin but nothing is broken, so it is a medium-priority optimization.
    • Value: Medium. Saving 19 to 33 s of CI CPU time per run has real cost and speed value for contributors.
    • Effort: Medium. It needs measuring the import chain, finding the largest slice, and refactoring it while keeping the limits intact.
    • Related: perf(ci): the startup-import tests start 56 interpreters and take 204 s of the suite's 2,423 s #2369, which asks for a different change. Both target the startup-imports row CPU time, but #2369 cut the number of interpreter starts while this item cuts the per-start import and help cost.
  3. added
    priority/p2-medium
    on Nov 4, 2026
  4. devops-cli-service commented on Nov 6, 2026

    @devops-cli-service
    Contributor

    Refine cannot set this item to Ready at c1191e2 until a person answers the questions below. Reply in a comment or edit the body, and the next refine round looks at the item again.

    • The proposal has no problem statement, no acceptance criterion with its requirement, scenario, WHEN and THEN, or no answer to the existing-tool question. State the problem, the requirements with their scenarios, and the open tool that covers the need or the cost of building it.
  5. devops-cli-service commented on Nov 7, 2026

    @devops-cli-service
    Contributor

    Alternatives check: not-applicable

    Class: perf. The check makes no search and no model call for an item of this class.

    Tool: none found

    Checked on 2026-11-07. A person decides what this means for the item. The check changes no label and closes no issue.

  6. added
    status/ready
    and removed
    status/new
    on Nov 13, 2026
  7. devops-cli-service commented on Nov 13, 2026

    @devops-cli-service
    Contributor

    Status set to Ready at 4ef7c6f (see proposed design in issue body).

  8. added
    owner/service
    on Nov 15, 2026
  9. 21 remaining items

  10. added
    status/planned
    on Nov 15, 2026
  11. devops-cli-service commented on Nov 15, 2026

    @devops-cli-service
    Contributor

    Cleared the stop at scoped checks by the rule digest: the runner image changed since the stop, from sha256:98de20a7edd09701e7b226a1931ada4763199253d241daf1151b0fe6b79f2dc5 to sha256:014636e1131ac1400a333cf938c841f3ffff1b3a7deb8b63b92cb8920cd31745, and the class ci_infra of the stop is one that a new attempt can fix. The item is Planned again, and the Service takes it in the next round. The Service clears at most 1 for each class and 3 for each item by digest. A stop of another class, a new stop on this image, and a stop over a limit wait for a comment implement: retry.

  12. added
    status/in-review
    and removed
    status/planned
    on Nov 16, 2026
  13. removed
    status/in-review
    on Nov 16, 2026
  14. devops-cli-service commented on Nov 16, 2026

    @devops-cli-service
    Contributor

    Live since 2026-11-16 03:02 UTC

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    effort/mediumpriority/p2-mediumscope/clisource/agenttype/infravalue/medium

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions