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

gh-154675: Use lazy imports in json package by brittanyrey · Pull Request #156801 · python/cpython · GitHub

Repository navigation

gh-154675: Use lazy imports in json package - #156801

Closed
brittanyrey wants to merge 2 commits into
python:mainfrom
brittanyrey:b-perf-json-lazy-imports
Closed

brittanyrey wants to merge 2 commits into
python:mainfrom
brittanyrey:b-perf-json-lazy-imports

Conversation

brittanyrey commented Sep 1, 2026 •
edited
Loading

Copy link
Copy Markdown
Contributor

summary

Apply lazy imports to json package to lift nested imports and optimize the modules.

perf

before after
python -c "import json.tool" 41.09 ms 21.24 ms (−48%)
python -c "import json" 20.98 ms 20.81 ms (−0.8%, neutral)
python -m json.tool f.json 46.06 ms 45.87 ms (−0.4%, neutral)

`argparse` and `_colorize` are only needed once `main()` runs, so import
them lazily. Also hoist the nested `warnings._deprecated` import in
`json.__getattr__` up to a module-level `lazy from`.

`python -c "import json.tool"` drops from 41.1 ms to 21.2 ms (-48%):
interleaved A/B, median of 9 rounds of min-of-25 runs, same binary with
`Lib/json` swapped via `PYTHONPATH`; an unrelated `import csv` control
moved +0.2%.

Running the CLI is unchanged -- `main()` always builds an `ArgumentParser`
and always calls `can_colorize()`, so both modules still get imported.

Copy link
Copy Markdown
Contributor

Are there any use cases where the improved import time matters? Users importing json.tool will run main() and that will trigger imports of the lazy packages.

johnslavik commented Sep 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

Are there any use cases where the improved import time matters? Users importing json.tool will run main() and that will trigger imports of the lazy packages.

Don't think so. And json.tool has no import-only use cases. Maybe drop this and news entry and leave only the json lazy import?

picnixz commented Sep 2, 2026 •
edited
Loading

Copy link
Copy Markdown
Member

AFAIR, the PEP explicitly said that we do not want to mechanically convert everything to lazy imports. Considering the noise for json itself I wonder about this change. json.tool is likely to be used as a CLI and there is no reason to do "import json.tool" as the public API is empty IIRC

picnixz added the pending The issue will be closed if no feedback is provided label Sep 2, 2026
…q7vZ2.rst

Co-authored-by: Pieter Eendebak <pieter.eendebak@gmail.com>

Copy link
Copy Markdown
Contributor Author

Solid call out. Dug in and I completely see what you mean...argparse and colorize will immediately be reified, so this is quite a wash.
I'm happy to abandon this.

Copy link
Copy Markdown
Member

I'll close then 👍🏽

johnslavik closed this Sep 2, 2026
brittanyrey deleted the b-perf-json-lazy-imports branch September 2, 2026 20:08
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

awaiting core review pending The issue will be closed if no feedback is provided

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants


Back | FazBrowse Home | New Git URL