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 — Improve import time of various stdlib modules · Issue #109653 · python/cpython

Improve import time of various stdlib modules #109653

New issue
Copy link
New issue
Copy link
Closed
#118697
Closed
Improve import time of various stdlib modules#109653
#118697
Copy link
Labels
3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-typingtype-featureA feature request or enhancementA feature request or enhancement

Description

@AlexWaygood
AlexWaygood
opened on Sep 21, 2023
Issue body actions

Feature or enhancement

Proposal:

As noted in https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173, typing isn't the slowest stdlib module in terms of import time, but neither is it one of the quickest. We should speed it up, if possible.

Links to previous discussion of this feature:

https://discuss.python.org/t/deferred-computation-evalution-for-toplevels-imports-and-dataclasses/34173

Linked PRs

  • gh-109653: typing.py: improve import time by creating soft-deprecated members on demand #109651
  • gh-109653: Improve enum import time by avoiding import of functools #109789
  • gh-109653: Remove unused imports in the Lib/ directory #109803
  • gh-109653: Avoid a top-level import of types in functools #109804
  • gh-109653: Just import recursive_repr in dataclasses #109822
  • gh-109653: Improve the import time of email.utils #109824
  • GH-109653: Defer import of importlib.metadata._adapters #109829
  • gh-109653: Fix py312 regression in the import time of random #110221
  • [3.12] gh-109653: Fix py312 regression in the import time of random (GH-110221) #110247
  • gh-109653: Defer importing warnings in several modules #110286
  • gh-109653: Improve import time of logging by lazy loading traceback #112995
  • gh-109653: Speedup import of threading module #114509
  • gh-109653: Improve import time of importlib.metadata / email.utils #114664
  • gh-109653: Reduce import overhead of uuid module on Linux #115160
  • gh-109653: What's new: Note improved import times for several stdlib modules in Python 3.13 #118697
  • gh-154675: Improve import performance of tkinter library with lazy imports #148409
Reacted by Daniel Hollas, Ofek Lev, Erlend E. Aasland, Hugo van Kemenade, Pathompong Kwangtong and Brett Holman
Reacted by Ofek Lev, Hugo van Kemenade, Pathompong Kwangtong and EDM115
Reacted by Daniel Hollas, Ofek Lev, Erlend E. Aasland, Hugo van Kemenade and Pathompong Kwangtong

Activity

Next
  1. AlexWaygood
    added
    type-featureA feature request or enhancementA feature request or enhancement
    performancePerformance or resource usagePerformance or resource usage
    topic-typing
    3.13only security fixesonly security fixes
    on Sep 21, 2023
  2. AlexWaygood
    added a commit that references this issue on Sep 23, 2023

    gh-109653: `typing.py`: improve import time by creating soft-deprecat…

    e8be0c9
  3. AlexWaygood
    changed the title [-]Improve import time of `typing.py`[/-] [+]Improve import time of various stdlib modules[/+] on Sep 23, 2023
  4. AlexWaygood commented on Sep 23, 2023

    @AlexWaygood
    AlexWaygood
    on Sep 23, 2023
    MemberAuthor
    More actions

    Retitling this issue to reflect a change of scope: part of the reason for the slow import time of typing is the slow import time of some of the modules it imports. There may be low-hanging fruit in some other stdlib modules as well.

  5. AlexWaygood
    added a commit that references this issue on Sep 23, 2023

    pythongh-109653: Improve `enum` import time by avoiding import of `fu…

    de366dc
  6. hugovk commented on Sep 23, 2023

    @hugovk
    hugovk
    on Sep 23, 2023
    Member
    More actions

    An idea: review the output of a linter for "unused" imports.

    Some may be false positives due to needing an import's side effects, but I expect there will be some that are no longer needed.

    Reacted by Alex Waygood, Itamar Oren, Jelle Zijlstra and Eric V. Smith
  7. itamaro commented on Sep 23, 2023

    @itamaro
    itamaro
    on Sep 23, 2023
    Contributor
    More actions

    Some may be false positives due to needing an import's side effects ...

    How do you feel about making an intentional effort to get rid of such reliance on side effects in the stdlib?
    Explicit is better than implicit :)

    Reacted by Hugo van Kemenade
  8. AlexWaygood commented on Sep 23, 2023

    @AlexWaygood
    AlexWaygood
    on Sep 23, 2023
    MemberAuthor
    More actions

    An idea: review the output of a linter for "unused" imports.

    FWIW, I can only find one in the stdlib that I'm 100% confident with, using ruff/pycln:

    cpython/Lib/asyncio/tasks.py

    Line 20 in e8be0c9

    import warnings

    I think this is because Victor went through them all fairly recently, in #105411. There are a fair few in Lib/test/, though. I'll file a PR in a bit.

    Reacted by Itamar Oren
    Reacted by Hugo van Kemenade
  9. ethanfurman
    added a commit that references this issue on Sep 23, 2023

    gh-109653: Improve `enum` import time by avoiding import of `functool…

    51863b7
  10. sobolevn commented on Sep 24, 2023

    @sobolevn
    sobolevn
    on Sep 24, 2023
    Member
    More actions

    This is also interesting:

    • dataclasses copies recursive_repr to avoid a dependency:

      cpython/Lib/dataclasses.py

      Lines 248 to 266 in 51863b7

      # This function's logic is copied from "recursive_repr" function in
      # reprlib module to avoid dependency.
      def _recursive_repr(user_function):
      # Decorator to make a repr function return "..." for a recursive
      # call.
      repr_running = set()
      @functools.wraps(user_function)
      def wrapper(self):
      key = id(self), _thread.get_ident()
      if key in repr_running:
      return '...'
      repr_running.add(key)
      try:
      result = user_function(self)
      finally:
      repr_running.discard(key)
      return result
      return wrapper
    • But, it imports functools:

      cpython/Lib/dataclasses.py

      Line 7 in 51863b7

      import functools
    • And it imports reprlib.recursive_repr:

      cpython/Lib/functools.py

      Line 20 in 51863b7

      from reprlib import recursive_repr
    Reacted by Alex Waygood
  11. sobolevn commented on Sep 24, 2023

    @sobolevn
    sobolevn
    on Sep 24, 2023
    Member
    More actions

    One more interesting case:

    • functools says that import types is deferred to singledispatch():

      cpython/Lib/functools.py

      Line 19 in 51863b7

      # import types, weakref # Deferred to single_dispatch()
    • But several lines below types module is imported:

      cpython/Lib/functools.py

      Line 22 in 51863b7

      from types import GenericAlias
    Reacted by Alex Waygood
  12. AlexWaygood
    added a commit that references this issue on Sep 24, 2023

    pythongh-109653: Avoid a top-level import of `types` in `functools`

    46cf001
  13. 43 remaining items

  14. AlexWaygood
    added a commit that references this issue on May 7, 2024

    pythongh-109653: What's new: Note improved import times for several s…

    a204e08
  15. AlexWaygood
    closed this as completedin #118697on May 7, 2024
  16. AlexWaygood
    added a commit that references this issue on May 7, 2024

    gh-109653: What's new: Note improved import times for several stdlib …

    44a9f3d
  17. layday commented on May 8, 2024

    @layday
    layday
    on May 8, 2024
    More actions

    I believe there are futher improvements to be made here - how would you like to handle these? For example, importlib.metadata imports zipfile at the top for a function that won't be called in the vast majority of cases. It also imports importlib.abc, which in turn imports importlib.resources, to subclass an ABC with a single, non-abstract method - I assume redefining the method in importlib.metadata would be harmless.

  18. AlexWaygood commented on May 8, 2024

    @AlexWaygood
    AlexWaygood
    on May 8, 2024
    MemberAuthor
    More actions

    I believe there are futher improvements to be made here - how would you like to handle these? For example, importlib.metadata imports zipfile at the top for a function that won't be called in the vast majority of cases. It also imports importlib.abc, which in turn imports importlib.resources, to subclass an ABC with a single, non-abstract method - I assume redefining the method in importlib.metadata would be harmless.

    Sure -- I felt like the low-hanging fruit had been tackled here, and this issue hadn't seen much activity in quite a while, so I felt like it was a good time to close. But more improvements are obviously always welcome. Maybe open a new issue if you have some more targeted improvements you want to propose?

    Reacted by layday and Hugo van Kemenade
  19. ofek commented on May 8, 2024

    @ofek
    ofek
    on May 8, 2024
    SponsorContributor
    More actions

    Please link the new issue here so we all can start following that as well!

  20. layday commented on May 8, 2024

    @layday
    layday
    on May 8, 2024
    More actions

    #118761

    Reacted by Daniel Hollas and Ofek Lev
  21. SonicField
    added a commit that references this issue on May 8, 2024

    pythongh-109653: What's new: Note improved import times for several s…

    fd73502
  22. Glyphack
    added 7 commits that reference this issue on Sep 2, 2024

    pythongh-109653: `typing.py`: improve import time by creating soft-de…

    7bba761

    pythongh-109653: Improve `enum` import time by avoiding import of `fu…

    77ec380

    pythongh-109653: Remove unused imports in the `Lib/` directory (pytho…

    4638f8f

    pythongh-109653: Avoid a top-level import of `types` in `functools` (p…

    f8e689e

    pythongh-109653: Reduce the import time of `random` by 60% (python#11…

    c35196c

    pythongh-109653: Defer importing `warnings` in several modules (pytho…

    c4f17c5

    pythongh-109653: Improve the import time of `email.utils` (python#109824

    7867907
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13only security fixesonly security fixesperformancePerformance or resource usagePerformance or resource usagetopic-typingtype-featureA feature request or enhancementA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.