Skip to content

Defer docutils imports until help is rendered - #10551

Open
Adityaj0 wants to merge 1 commit into
aws:v2from
Adityaj0:perf/lazy-doc-imports
Open

Defer docutils imports until help is rendered#10551
Adityaj0 wants to merge 1 commit into
aws:v2from
Adityaj0:perf/lazy-doc-imports

Conversation

@Adityaj0

@Adityaj0 Adityaj0 commented Aug 13, 2026

Copy link
Copy Markdown

Split out of #10545 at @aemous's request — this is half 1 of 2. The other half is #10552 (s3 filter precompilation); the two are independent and touch disjoint files.

awscli/help.py imported docutils.core and the html4css1/manpage writers at module scope, and awscli/topictags.py imported docutils.core. Because awscli/customizations/commands.py subclasses HelpCommand, that whole chain was pulled in during customization registration on every CLI invocation — including pygments and PIL via the docutils rst directives — even for commands that never render help.

Every use is inside a method, so the imports move into them.

Measured with 25 subprocess runs of aws --version per arm, alternating between arms three times to control for machine drift:

before   min 246-255ms
after    min 232-240ms

About 14ms (~6%) off every invocation. I'm quoting min-of-25 rather than the mean because run-to-run noise on this machine is ±20ms, which is larger than the effect — sequential (non-interleaved) runs initially suggested a bigger win than the interleaved A/B supports.

Checked by hand that help still renders: aws help, aws s3 help, aws ec2 describe-instances help, aws help topics, and TopicTagDB.scan still parses topic source files.

Testing

  • tests/unit/test_help.py, tests/unit/test_topictags.py, tests/unit/test_clidriver.py, tests/functional/docs: 11967 passed, 3 skipped.
  • ruff output on the touched files is unchanged from baseline.

I did not run the full tests/functional suite locally.


By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

``awscli.help`` imported ``docutils.core`` and the html4css1/manpage
writers at module scope, and ``awscli.topictags`` imported
``docutils.core``.  Because ``awscli.customizations.commands`` subclasses
``HelpCommand``, that chain was pulled in during customization
registration on every CLI invocation -- including ``pygments`` and
``PIL`` via the docutils rst directives -- even for commands that never
render help.  All of the uses are inside methods, so the imports move
into them.

Measured with 25 subprocess runs of ``aws --version`` per arm,
alternating between arms three times to control for drift:

    before  min 246-255ms
    after   min 232-240ms

so roughly 14ms (~6%) off every invocation.  ``aws help``,
``aws s3 help``, ``aws ec2 describe-instances help`` and
``aws help topics`` were checked by hand, and ``TopicTagDB.scan`` still
parses topic files.

Co-Authored-By: Claude Opus 5 <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.

1 participant