Skip to content
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

gh-118761: Improve import time of pprint #122725

Merged
merged 5 commits into from
Aug 7, 2024

Conversation

hugovk
Copy link
Member

@hugovk hugovk commented Aug 6, 2024

Improves import time by up to seven times. Measured with a PGO and LTO non-debug build on macOS.

  • The slowest import in pprint is dataclasses (taking 0.0012s / 55.2% of import time), and used in only two functions.

  • After deferring dataclasses, the next is re (0.005s / 30.1% of the new import time), only used in one function.

  • After deferring re, the next are collections (0.002s / 14.8%) and types (0.000s / 2.4%). I left these, as their times are low and they're less straightforward to defer.

-X importtime

Running: pip install tuna && ./python.exe -X importtime -c 'import pprint' 2> import.log && tuna import.log

total import time: 0.022s -> 0.011s = x2 as fast

main PR
image image

pprint import time: 0.014s -> 0.002s = x7 as fast

main PR
image image

hyperfine: 19.2 ms -> 10.4 ms = x1.8 as fast

main

hyperfine --warmup 8 "./python.exe -c 'import pprint'"
Benchmark 1: ./python.exe -c 'import pprint'
  Time (mean ± σ):      19.2 ms ±   0.7 ms    [User: 15.8 ms, System: 2.8 ms]
  Range (min … max):    18.1 ms …  21.3 ms    135 runs

PR

hyperfine --warmup 8 "./python.exe -c 'import pprint'"
Benchmark 1: ./python.exe -c 'import pprint'
  Time (mean ± σ):      10.4 ms ±   0.4 ms    [User: 8.1 ms, System: 1.8 ms]
  Range (min … max):     9.8 ms …  12.4 ms    212 runs

Also add some PEP 8 whitespace to remove some distracting IDE warnings.

@hugovk
Copy link
Member Author

hugovk commented Aug 6, 2024

By the way, what's the reason for importing with underscores?

import collections as _collections
import sys as _sys
import types as _types
from io import StringIO as _StringIO

Copy link
Member

@AA-Turner AA-Turner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine -- I wouldn't change the whitespace personally but up to you.

collections you could do by hardcoding the repr and adding a test that they're the same? I agree probably not worth it, though.

A

Lib/pprint.py Outdated Show resolved Hide resolved
Lib/pprint.py Outdated Show resolved Hide resolved
hugovk and others added 2 commits August 7, 2024 19:30
@hugovk hugovk merged commit 42d9bec into python:main Aug 7, 2024
34 checks passed
@hugovk hugovk deleted the pprint-defer-imports branch August 7, 2024 19:46
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
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