FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-118761: Improve import time of `cmd` module by donbarbos · Pull Request #130056 · python/cpython · GitHub

/ cpython Public

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

Merged
gpshead merged 4 commits into
python:mainfrom
donbarbos:improve-cmd-imports
Feb 17, 2025
Merged

gh-118761: Improve import time of cmd module#130056
gpshead merged 4 commits into
python:mainfrom
donbarbos:improve-cmd-imports

Conversation

donbarbos commented Feb 12, 2025
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

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

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

main branch PR branch

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

Copy link
Copy Markdown
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

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

main branch PR branch

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 commented Feb 17, 2025

Copy link
Copy Markdown
Member

Go ahead and remove the import string as described.

donbarbos requested a review from gpshead February 17, 2025 19:39
gpshead enabled auto-merge (squash) February 17, 2025 20:03
gpshead merged commit 99d9656 into python:main Feb 17, 2025
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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


Back | FazBrowse Home | New Git URL