Skip to content

perf: defer pint/argos imports in the risk and experiment CLIs (#935) - #1077

Open
ilayfalach wants to merge 1 commit into
masterfrom
ISSUE935
Open

ilayfalach wants to merge 1 commit into
masterfrom
ISSUE935

Conversation

@ilayfalach

Copy link
Copy Markdown
Collaborator

Closes #935.

Two CLIs imported pint just to print --help. Everything else was already fast.

--help before after
hera-riskassessment 4847 ms 54 ms
hera-experiment 2622 ms 99 ms
hera-project (baseline, untouched) 135 ms 70 ms

Minimum of 5 warm runs. The before column was measured the same way, with the change stashed.

What changed

  • hera/riskassessment/__init__.py imported Agent and RiskToolkit eagerly, which pulls pint through unitHandler. riskassessment/CLI.py only needs logging, json and os. Now lazy via PEP 562 __getattr__ — the same pattern already used in hera/utils/__init__.py.
  • hera/measurements/experiment/CLI.py imported argos, pandas and dataToolkit at module level; each is used in one function, so they moved to call time. Four imports that were unused entirely are removed (toolkitHome twice, requests, zipfile).

Test

hera/tests/unit/test_cli_import_cost.py asserts pint and argos stay out of sys.modules after importing each CLI module. It fails on the pre-change code.

No overlap with the tests/batch* stack — neither file is touched by any of its 157 commits.

🤖 Generated with Claude Code

…935)

`hera-riskassessment --help` took 4.8s and `hera-experiment --help` 2.6s,
against 135ms for the other CLIs. Both paid for imports they never used at
argparse-wiring time:

- hera/riskassessment/__init__.py eagerly imported Agent/RiskToolkit/etc,
  pulling pint through hera.utils.unitHandler. hera/riskassessment/CLI.py
  itself needs nothing but logging/json/os. Made the package init lazy with
  PEP 562 __getattr__, the same pattern already used in hera.utils.
- hera/measurements/experiment/CLI.py imported argos, pandas and dataToolkit
  (pint) at module level although each is used inside a single function;
  moved them to call time, matching the experimentHome imports already done
  that way in the same file. Dropped four imports that were unused entirely
  (toolkitHome twice, requests, zipfile).

Measured (min of 5 warm runs, `--help`):
  hera-riskassessment  4847ms -> 54ms
  hera-experiment      2622ms -> 99ms
  hera-project (unchanged baseline)  135ms -> 70ms

Guarded by hera/tests/unit/test_cli_import_cost.py, which asserts pint and
argos stay out of sys.modules after importing each CLI module; it fails on
the pre-change code.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.

fix import time and CLI delays

1 participant