Skip to content

⚡ perf(import): defer server helpers - #330

Merged
gaborbernat merged 2 commits into
mainfrom
perf/lazy-imports
Nov 18, 2026
Merged

gaborbernat merged 2 commits into
mainfrom
perf/lazy-imports

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

Importing devpi_process previously loaded socket, subprocess, and logging helpers before callers used them. In a paired 60-run Python 3.15 hyperfine benchmark, package import measured 23.4 → 11.0 ms.

The public module declares imports eligible for lazy loading, with flake8-lazy checking the list. Server helpers load when the constructor or methods first need them, moving that import cost to server creation or use.

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. The Python 3.14 package import difference is inconclusive under host load.

Python 3.15

Package import

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     23.4 ms  ±     1.1 ms      21.6 ms  …    26.5 ms
  Memory        17.4 MiB ±     0.5 MiB     16.5 MiB …    18.2 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     11.0 ms  ±     0.7 ms       9.9 ms  …    12.6 ms           -52.9%  (2.1x faster)
  Memory        10.4 MiB ±     0.4 MiB      9.4 MiB …    11.0 MiB          -40.5%  (1.7x less)

Python 3.14

Package import

Benchmark 1: Before (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     28.3 ms  ±     1.3 ms      26.1 ms  …    32.8 ms
  Memory        19.2 MiB ±     0.5 MiB     17.9 MiB …    19.9 MiB
 
Benchmark 2: After (60 runs)
                mean     ±       σ          min     …     max
  Wall Time     29.1 ms  ±     1.7 ms      26.2 ms  …    34.9 ms            +2.7%
  Memory        19.1 MiB ±     0.5 MiB     17.5 MiB …    19.9 MiB           -0.4%

Import-time profile

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

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

Before

Before import tree

After

After import tree

@gaborbernat gaborbernat added the enhancement New feature or request label Nov 18, 2026
@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 marked this pull request as draft November 18, 2026 18:27
@gaborbernat
gaborbernat marked this pull request as ready for review November 18, 2026 20:24
@gaborbernat
gaborbernat merged commit 04e2a39 into main Nov 18, 2026
19 checks passed
@gaborbernat
gaborbernat deleted the perf/lazy-imports branch November 18, 2026 22:01
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