perf: defer pint/argos imports in the risk and experiment CLIs (#935) - #1077
Open
ilayfalach wants to merge 1 commit into
Open
ilayfalach wants to merge 1 commit into
ilayfalach wants to merge 1 commit into
Conversation
…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>
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 #935.
Two CLIs imported pint just to print
--help. Everything else was already fast.--helphera-riskassessmenthera-experimenthera-project(baseline, untouched)Minimum of 5 warm runs. The before column was measured the same way, with the change stashed.
What changed
hera/riskassessment/__init__.pyimportedAgentandRiskToolkiteagerly, which pulls pint throughunitHandler.riskassessment/CLI.pyonly needslogging,jsonandos. Now lazy via PEP 562__getattr__— the same pattern already used inhera/utils/__init__.py.hera/measurements/experiment/CLI.pyimportedargos,pandasanddataToolkitat module level; each is used in one function, so they moved to call time. Four imports that were unused entirely are removed (toolkitHometwice,requests,zipfile).Test
hera/tests/unit/test_cli_import_cost.pyasserts pint and argos stay out ofsys.modulesafter 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