Skip to content

gh-118761: Improve import time by lazy import of traceback #129811

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

donBarbos
Copy link
Contributor

@donBarbos donBarbos commented Feb 7, 2025

TODO:

  • Improve import time modules by lazy import traceback
  • Remove unused import fcntl

CPython configure flags:

./configure --enable-optimizations --with-lto --enable-loadable-sqlite-extensions

Hyperfine benchmark for import traceback:

$ hyperfine --warmup 11 "./python -c 'import traceback'"
Benchmark 1: ./python -c 'import traceback'
  Time (mean ± σ):      17.0 ms ±   0.5 ms    [User: 14.2 ms, System: 2.7 ms]
  Range (min … max):    16.5 ms …  22.7 ms    167 runs

Hyperfine benchmarks for updated modules:

import _pyrepl experiments (no diff)

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import _pyrepl'"
Benchmark 1: ./python -c 'import _pyrepl'
  Time (mean ± σ):       9.2 ms ±   0.4 ms    [User: 7.3 ms, System: 1.8 ms]
  Range (min … max):     8.6 ms …  14.7 ms    3000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import _pyrepl'"
Benchmark 1: ./python -c 'import _pyrepl'
  Time (mean ± σ):       9.3 ms ±   0.3 ms    [User: 7.4 ms, System: 1.8 ms]
  Range (min … max):     8.7 ms …  14.7 ms    3000 runs

import code experiments

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import code'"
Benchmark 1: ./python -c 'import code'
  Time (mean ± σ):      10.0 ms ±   0.5 ms    [User: 8.2 ms, System: 1.8 ms]
  Range (min … max):     9.3 ms …  15.8 ms    3000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import code'"
Benchmark 1: ./python -c 'import code'
  Time (mean ± σ):      17.8 ms ±   0.8 ms    [User: 14.9 ms, System: 2.9 ms]
  Range (min … max):    17.0 ms …  28.7 ms    3000 runs

import doctest experiments (no diff)

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 "./python -c 'import doctest'"
Benchmark 1: ./python -c 'import doctest'
  Time (mean ± σ):      34.8 ms ±   0.3 ms    [User: 30.2 ms, System: 4.6 ms]
  Range (min … max):    34.2 ms …  35.7 ms    84 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 "./python -c 'import doctest'"
Benchmark 1: ./python -c 'import doctest'
  Time (mean ± σ):      35.6 ms ±   0.5 ms    [User: 30.8 ms, System: 4.8 ms]
  Range (min … max):    34.9 ms …  37.9 ms    81 runs

import logging experiments (no diff)

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 2000 "./python -c 'import logging'"
Benchmark 1: ./python -c 'import logging'
  Time (mean ± σ):      20.9 ms ±   0.7 ms    [User: 17.7 ms, System: 3.1 ms]
  Range (min … max):    20.1 ms …  32.7 ms    2000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 --runs 2000 "./python -c 'import logging'"
Benchmark 1: ./python -c 'import logging'
  Time (mean ± σ):      20.7 ms ±   0.5 ms    [User: 17.7 ms, System: 3.0 ms]
  Range (min … max):    20.0 ms …  30.8 ms    2000 runs

import pdb experiments (no diff)

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 2000 "./python -c 'import pdb'"
Benchmark 1: ./python -c 'import pdb'
  Time (mean ± σ):      29.3 ms ±   0.9 ms    [User: 25.0 ms, System: 4.2 ms]
  Range (min … max):    28.2 ms …  37.9 ms    2000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 --runs 2000 "./python -c 'import pdb'"
Benchmark 1: ./python -c 'import pdb'
  Time (mean ± σ):      30.1 ms ±   1.3 ms    [User: 25.9 ms, System: 4.2 ms]
  Range (min … max):    28.8 ms …  44.2 ms    2000 runs

import py_compile experiments

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 2000 "./python -c 'import py_compile'"
Benchmark 1: ./python -c 'import py_compile'
  Time (mean ± σ):      11.7 ms ±   0.5 ms    [User: 9.6 ms, System: 2.0 ms]
  Range (min … max):    10.9 ms …  19.5 ms    2000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 "./python -c 'import py_compile'"
Benchmark 1: ./python -c 'import py_compile'
  Time (mean ± σ):      18.2 ms ±   0.5 ms    [User: 15.4 ms, System: 2.7 ms]
  Range (min … max):    17.4 ms …  21.0 ms    159 runs

import pydoc experiments

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 "./python -c 'import pydoc'"
Benchmark 1: ./python -c 'import pydoc'
  Time (mean ± σ):      32.6 ms ±   0.4 ms    [User: 28.4 ms, System: 4.2 ms]
  Range (min … max):    31.8 ms …  34.3 ms    87 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 "./python -c 'import pydoc'"
Benchmark 1: ./python -c 'import pydoc'
  Time (mean ± σ):      34.3 ms ±   0.3 ms    [User: 30.2 ms, System: 4.1 ms]
  Range (min … max):    33.5 ms …  35.2 ms    84 runs

import unittest experiments

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 "./python -c 'import unittest'"
Benchmark 1: ./python -c 'import unittest'
  Time (mean ± σ):      21.9 ms ±   0.3 ms    [User: 18.4 ms, System: 3.4 ms]
  Range (min … max):    21.3 ms …  22.8 ms    133 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 "./python -c 'import unittest'"
Benchmark 1: ./python -c 'import unittest'
  Time (mean ± σ):      23.3 ms ±   0.3 ms    [User: 19.5 ms, System: 3.7 ms]
  Range (min … max):    22.6 ms …  24.3 ms    123 runs

import xmlrpc experiments (no diff)

➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import xmlrpc'"
Benchmark 1: ./python -c 'import xmlrpc'
  Time (mean ± σ):       9.1 ms ±   0.4 ms    [User: 7.3 ms, System: 1.8 ms]
  Range (min … max):     8.7 ms …  14.6 ms    3000 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 --runs 3000 "./python -c 'import xmlrpc'"
Benchmark 1: ./python -c 'import xmlrpc'
  Time (mean ± σ):       9.1 ms ±   0.4 ms    [User: 7.4 ms, System: 1.7 ms]
  Range (min … max):     8.6 ms …  19.6 ms    3000 runs
➜  cpython git:(lazy-imports-traceback) ✗ hyperfine --warmup 11 "./python -c 'import xmlrpc'"
Benchmark 1: ./python -c 'import xmlrpc'
  Time (mean ± σ):       8.8 ms ±   0.6 ms    [User: 7.3 ms, System: 1.5 ms]
  Range (min … max):     8.3 ms …  14.5 ms    277 runs
➜  cpython git:(3d3a4beefe) ✗ hyperfine --warmup 11 "./python -c 'import xmlrpc'"
Benchmark 1: ./python -c 'import xmlrpc'
  Time (mean ± σ):       8.7 ms ±   0.2 ms    [User: 7.4 ms, System: 1.4 ms]
  Range (min … max):     8.2 ms …   9.8 ms    303 runs

@picnixz
Copy link
Member

picnixz commented Feb 7, 2025

traceback is generally used in conjunction with other debugging modules, so I'm not sure it's worth the change. I'd like to mention that I also benchmark the import times using -X importtime as they don't suffer from startup time. Now, if we don't see improvements overall, I don't think we should actually do the change. I only changed modules for which import time is reduced by at least 20% (using -X importtime) or where we gain something noticeable on hyperfine. Most of your results show <10% improvements or no changes at all, so I don't think we need to do this. We definitely don't want to make all imports as lazy IMO.

…kNOEA.rst

Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
@AA-Turner
Copy link
Member

Please see my comment in your other PR, and also Benedikt's above.

A

@picnixz picnixz changed the title gh-118761: Improve import time by lazy import of traceback gh-118761: Improve import time by lazy import of traceback Feb 8, 2025
@donBarbos
Copy link
Contributor Author

donBarbos commented Feb 16, 2025

Ok, i realized that this PR was a mistake and i will improve import time of each module separately. Maybe i will send PR for same module but without some other imports

@donBarbos donBarbos closed this Feb 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants