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 — Defer import of json and platform by danielhollas · Pull Request #503 · python/importlib_metadata

Conversation

@danielhollas

@danielhollas danielhollas commented Aug 27, 2024

Copy link
Copy Markdown
Contributor

Staged on top of #502

platform and json modules take together around 1.5ms to import, around 10% of total import time. Since they are both used in a single place it probably makes sense to inline their import.

After this PR, this is an output of python -Ximporttime -c "import importlib_metadata" visualized with tuna using cpython main branch.

image

Sorry, something went wrong.

Uh oh!

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

All reactions
danielhollas added 3 commits August 27, 2024 15:31
@danielhollas
Defer import of zipp
76eebe0
@danielhollas
Defer json import
e0b4f09
@danielhollas
Defer platform import
8436e81
@danielhollas danielhollas mentioned this pull request Aug 27, 2024
Improve import time of various stdlib modules python/cpython#118761
Closed
danielhollas
danielhollas commented Aug 27, 2024
View reviewed changes
Comment thread importlib_metadata/_compat.py Resolved

Uh oh!

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

@jaraco

jaraco commented Sep 11, 2024

Copy link
Copy Markdown
Member

Attempting to disentangle the performance impacts of json and platform:

 importlib_metadata main 🐚 .tox/py/bin/python -Ximporttime -c "import importlib_metadata" 2>1 | grep -E r'platform|\sjson$|importlib_metadata$'
import time:        89 |        800 |   json
import time:       221 |        251 |     platform
import time:       636 |      13692 | importlib_metadata
 importlib_metadata main 🐚 800/13692
0.05842827928717499
 importlib_metadata main 🐚 251/13692
0.018331872626351154

I see json accounts for ~6% of the startup and platform under 2% (on my machine under Python 3.12).

All reactions

Sorry, something went wrong.

Uh oh!

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

jaraco
jaraco reviewed Sep 11, 2024
View reviewed changes
Comment thread importlib_metadata/_compat.py Resolved

Uh oh!

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

jaraco added 3 commits September 11, 2024 10:13
@jaraco
Revert "Defer platform import"
18eb2da
This reverts commit 8436e81.
@jaraco
Add comment to protect the deferred import.
3f78dc1
@jaraco
Add news fragment.
2a3f50d
@jaraco
jaraco merged commit 8b909f9 into python:main Sep 11, 2024
@danielhollas
danielhollas deleted the defer-json branch September 11, 2024 14:27
@danielhollas danielhollas changed the title Defer import of json and zipp Defer import of json and platform Sep 11, 2024
@yashuatla yashuatla mentioned this pull request Jun 22, 2025
Feature implementation from commits 2beb8b0..b34810b yashuatla/importlib_metadata#3
Open
@jaraco jaraco mentioned this pull request Oct 31, 2026
Restore the deferred imports to module scope with PEP 810 lazy imports #554
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@jaraco jaraco jaraco left review comments

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

@danielhollas @jaraco
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.