GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-118761: Improve import time of `pprint` by hugovk · Pull Request #122725 · python/cpython

Conversation

@hugovk

@hugovk hugovk commented Aug 6, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

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
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.

  • Issue: Improve import time of various stdlib modules #118761

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
hugovk added 3 commits August 6, 2024 10:05
@hugovk
Improve import time of pprint
c9533e6
@hugovk
Add blurb
f65cb1e
@hugovk
PEP 8: add whitespace
40f6d2e
@bedevere-app bedevere-app Bot mentioned this pull request Aug 6, 2024
Improve import time of various stdlib modules #118761
Closed
@bedevere-app bedevere-app Bot added the awaiting core review label Aug 6, 2024
@hugovk

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
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

AA-Turner
AA-Turner approved these changes Aug 7, 2024
View reviewed changes

@AA-Turner AA-Turner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this 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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread Lib/pprint.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/pprint.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Aug 7, 2024
hugovk and others added 2 commits August 7, 2024 19:30
@hugovk @AA-Turner
Apply suggestions from code review
07cffca
Co-authored-by: Adam Turner <[email protected]>
@hugovk @AA-Turner
Apply suggestions from code review
397f05b
Co-authored-by: Adam Turner <[email protected]>
@hugovk
hugovk merged commit 42d9bec into python:main Aug 7, 2024
@hugovk
hugovk deleted the pprint-defer-imports branch August 7, 2024 19:46
@bedevere-app bedevere-app Bot removed the awaiting merge label Aug 7, 2024
blhsing pushed a commit to blhsing/cpython that referenced this pull request Aug 22, 2024
@hugovk @AA-Turner @blhsing
pythongh-118761: Improve import time of pprint (python#122725)
0549dc7
Co-authored-by: Adam Turner <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@AA-Turner AA-Turner AA-Turner approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@hugovk @AA-Turner
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.