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

gh-118761: Improve import time of `pprint` by hugovk · Pull Request #122725 · python/cpython · GitHub

/ cpython Public

gh-118761: Improve import time of pprint - #122725

Merged
hugovk merged 5 commits into
python:mainfrom
hugovk:pprint-defer-imports
Aug 7, 2024
Merged

gh-118761: Improve import time of pprint#122725
hugovk merged 5 commits into
python:mainfrom
hugovk:pprint-defer-imports

Conversation

hugovk commented Aug 6, 2024
edited
Loading

Copy link
Copy Markdown
Member

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

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

main PR

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 commented Aug 6, 2024

Copy link
Copy Markdown
Member Author

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

AA-Turner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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

Comment thread Lib/pprint.py Outdated
Comment thread Lib/pprint.py Outdated
hugovk and others added 2 commits August 7, 2024 19:30
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
hugovk merged commit 42d9bec into python:main Aug 7, 2024
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
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
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