Skip to content

⚡ perf(import): defer unused SBOM modules - #29

Merged
gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/lazy-imports
Nov 18, 2026
Merged

gaborbernat merged 3 commits into
tox-dev:mainfrom
gaborbernat:perf/lazy-imports

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Importing python-sbom or opening its CLI help previously loaded modules for SBOM generation, validation, and build backends that those paths did not use. In a paired 60-run Python 3.15 hyperfine benchmark, package import measured 37.7 → 7.5 ms.

The public API, CLI, and backend modules declare imports eligible for Python 3.15 lazy loading, so generation, validation, and build paths load their dependencies when used. Earlier Python versions ignore these declarations. flake8-lazy keeps them aligned with the imports. The Hatch entry point still checks for hatchling when imported, since callers rely on that dependency error appearing immediately.

Benchmarks

Each workload and Python version has one hyperfine 2.0.0 --warmup 5 --runs 60 --metrics time_wall_clock:ms,memory_peak_resident:MiB invocation comparing the PR with its merge base. Raw output reports wall time and peak resident memory. Both commands used the same interpreter and dependencies in a tox environment; PYTHONPATH selected the source tree. Python 3.14 ignores the lazy declarations.

Python 3.15

Package import

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     37.7 ms  ±     1.7 ms      35.1 ms  …    41.7 ms
  Memory        16.9 MiB ±     0.6 MiB     15.1 MiB …    17.6 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time      7.5 ms  ±     0.7 ms       6.9 ms  …    11.3 ms           -80.0%  (5.0x faster)
  Memory         9.3 MiB ±     0.3 MiB      8.5 MiB …     9.8 MiB          -44.9%  (1.8x less)

CLI help

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     50.5 ms  ±     2.5 ms      46.5 ms  …    58.9 ms
  Memory        19.2 MiB ±     0.4 MiB     18.1 MiB …    20.0 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     27.6 ms  ±     1.2 ms      25.6 ms  …    31.0 ms           -45.4%  (1.8x faster)
  Memory        14.9 MiB ±     0.4 MiB     13.5 MiB …    15.5 MiB          -22.2%

Python 3.14

Package import

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     38.1 ms  ±     1.4 ms      35.6 ms  …    41.2 ms
  Memory        18.1 MiB ±     0.5 MiB     16.3 MiB …    19.0 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     38.3 ms  ±     1.4 ms      35.5 ms  …    41.9 ms            +0.5%
  Memory        18.1 MiB ±     0.6 MiB     16.4 MiB …    18.8 MiB           -0.4%

CLI help

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     43.3 ms  ±     1.7 ms      40.5 ms  …    48.0 ms
  Memory        19.5 MiB ±     0.5 MiB     18.3 MiB …    20.3 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     43.0 ms  ±     1.6 ms      40.0 ms  …    47.5 ms            -0.8%
  Memory        19.3 MiB ±     0.5 MiB     18.4 MiB …    20.3 MiB           -1.1%

Import-time profile

Lifeguard scanned the source tree with --python-version 3.15. I also imported 59 modules separately with python -X importtime.

These are one-run cumulative import timings for the modules with the largest reductions:

Module Before After
python_sbom.setuptools 379.1 ms 0.7 ms
python_sbom._validate._schemas 238.4 ms 29.4 ms
python_sbom._cli._archive 114.7 ms 0.7 ms

The Python 3.15 package import diagrams use python -X importtime -c "import python_sbom" 2> import.log and tuna.

Before

Before import tree

After

After import tree

Loading the public package and CLI help imports much of the build and
validation stack on Python 3.15. Defer modules until their functionality
is used, while keeping the Hatch hook's dependency check eager.
@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@read-the-docs-community

read-the-docs-community Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Documentation build overview

📚 python-sbom | 🛠️ Build #35977700 | 📁 Comparing 17e36b8 against latest (7eeb903)

  🔍 Preview build  

1 file changed
± changelog.html

@gaborbernat
gaborbernat marked this pull request as draft November 18, 2026 07:46
@gaborbernat
gaborbernat marked this pull request as ready for review November 18, 2026 08:45
@gaborbernat
gaborbernat merged commit b54c40c into tox-dev:main Nov 18, 2026
37 checks passed
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