Skip to content

gh-118761: Improve import time of cmd module #130056

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

Merged
merged 4 commits into from
Feb 17, 2025

Conversation

donBarbos
Copy link
Contributor

@donBarbos donBarbos commented Feb 12, 2025

I use lazy import for inspect module (called 1 time).
And I suggest getting rid of import string. This will improve import time as much as possible (benchmarks without string module separately in second comment).

CPython configure flags:

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

Benchmarks:

Running: pipx install tuna && ./python -X importtime -c 'import cmd' 2> import.log && tuna import.log

Total import time: 0.023s -> 0.013s = x1.77 as fast

main branch PR branch
Screenshot from 2025-02-13 01-59-59 Screenshot from 2025-02-13 01-54-43

cmd import time: 0.016s -> 0.007s = x2.29 as fast

main branch PR branch
Screenshot from 2025-02-13 02-00-06 Screenshot from 2025-02-13 01-54-50

hyperfine: 23.7ms -> 15.0ms = x1.58 as fast

Main branch:

$ hyperfine --warmup 11 "./python -c 'import cmd'"
Benchmark 1: ./python -c 'import cmd'
  Time (mean ± σ):      23.7 ms ±   0.3 ms    [User: 20.3 ms, System: 3.3 ms]
  Range (min … max):    23.1 ms …  25.7 ms    123 runs

PR branch:

$ hyperfine --warmup 11 "./python -c 'import cmd'"
Benchmark 1: ./python -c 'import cmd'
  Time (mean ± σ):      15.0 ms ±   0.3 ms    [User: 12.6 ms, System: 2.4 ms]
  Range (min … max):    14.4 ms …  16.0 ms    189 runs

@donBarbos
Copy link
Contributor Author

I also suggest removing import string and making the following one change:

# from this:
IDENTCHARS = string.ascii_letters + string.digits + '_'
# to this:
IDENTCHARS = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'
              'abcdefghijklmnopqrstuvwxyz'
              '0123456789'
              '_')

That's all!

Benchmarks for cmd module without import string

comparing with the main branch here are the results:

Total import time: 0.023s -> 0.008s = x2.88 as fast

main branch PR branch
Screenshot from 2025-02-13 01-59-59 Screenshot from 2025-02-13 01-53-26

cmd import time: 0.016s -> 0.001s = x16 as fast (or more :)

main branch PR branch
Screenshot from 2025-02-13 02-00-06 Screenshot from 2025-02-13 01-53-34

hyperfine: 23.7ms -> 9.0ms = x2.64 as fast

$ hyperfine --warmup 11 "./python -c 'import cmd'"
Benchmark 1: ./python -c 'import cmd'
  Time (mean ± σ):       9.0 ms ±   0.6 ms    [User: 7.6 ms, System: 1.4 ms]
  Range (min … max):     8.4 ms …  13.0 ms    288 runs

@gpshead
Copy link
Member

gpshead commented Feb 17, 2025

Go ahead and remove the import string as described.

@donBarbos donBarbos requested a review from gpshead February 17, 2025 19:39
@gpshead gpshead enabled auto-merge (squash) February 17, 2025 20:03
@gpshead gpshead merged commit 99d9656 into python:main Feb 17, 2025
39 checks passed
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.

2 participants