Skip to content

⚡ perf(import): defer directive helpers - #417

Merged
gaborbernat merged 1 commit into
mainfrom
perf/lazy-imports
Nov 19, 2026
Merged

gaborbernat merged 1 commit into
mainfrom
perf/lazy-imports

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Loading the Sphinx directive now takes less time and memory on Python 3.15. In a paired 60-run benchmark, importing the directive module fell from 83.8 to 70.4 ms and peak memory from 35.9 to 31.8 MiB. Python 3.14 showed no material change.

The extension keeps the imports needed to register its directive eager and defers helpers used only while rendering CLI documentation. Its package root is already light; the new lazy declaration there covers only the version module. Lifeguard and flake8-lazy guided the module review. The sys.modules access and logger initialization remain eager.

Benchmarks

Each block is one paired 60-run hyperfine invocation with --warmup 5 --metrics time_wall_clock:ms,memory_peak_resident:MiB. Both commands used the same tox interpreter and dependencies, with PYTHONPATH selecting the merge-base or PR source tree. The small package-root differences are below the run-to-run variation.

Python 3.15

Package root (import sphinx_argparse_cli)

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     11.9 ms  ±     0.6 ms      11.3 ms  …    13.8 ms
  Memory        11.5 MiB ±     0.0 MiB     11.5 MiB …    11.6 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     11.9 ms  ±     0.6 ms      11.1 ms  …    14.2 ms            -0.7%
  Memory        11.5 MiB ±     0.0 MiB     11.3 MiB …    11.6 MiB           -0.1%

Directive module (import sphinx_argparse_cli._logic)

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     83.8 ms  ±     2.7 ms      79.0 ms  …    91.4 ms
  Memory        35.9 MiB ±     0.1 MiB     35.7 MiB …    36.2 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     70.4 ms  ±     4.4 ms      65.4 ms  …    95.9 ms           -16.0%
  Memory        31.8 MiB ±     0.1 MiB     31.8 MiB …    32.0 MiB          -11.3%

Python 3.14

Package root (import sphinx_argparse_cli)

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     10.1 ms  ±     1.0 ms       9.2 ms  …    13.3 ms
  Memory        12.3 MiB ±     0.1 MiB     12.1 MiB …    12.5 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     10.4 ms  ±     1.5 ms       9.0 ms  …    13.9 ms            +2.5%
  Memory        12.3 MiB ±     0.1 MiB     12.1 MiB …    12.5 MiB            0.0%

Directive module (import sphinx_argparse_cli._logic)

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     79.8 ms  ±     2.9 ms      76.2 ms  …    90.9 ms
  Memory        34.3 MiB ±     0.1 MiB     34.1 MiB …    34.4 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     80.0 ms  ±     3.3 ms      74.5 ms  …    89.8 ms            +0.3%
  Memory        34.3 MiB ±     0.1 MiB     34.1 MiB …    34.5 MiB            0.0%

Import profile

These Python 3.15 diagrams come from python -X importtime -c 'import sphinx_argparse_cli._logic' and tuna.

Before

Before directive import tree

After

After directive import tree

@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@gaborbernat
gaborbernat marked this pull request as ready for review November 18, 2026 22:28
@gaborbernat
gaborbernat merged commit c446704 into main Nov 19, 2026
20 checks passed
@gaborbernat
gaborbernat deleted the perf/lazy-imports branch November 19, 2026 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant